Make WordPress Core


Ignore:
Timestamp:
07/31/2015 01:43:11 AM (11 years ago)
Author:
azaozz
Message:

Backport r33469 and r33470 to 4.0.
See #33106.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/4.0/src/wp-includes/formatting.php

    r33381 r33522  
    406406        $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
    407407
    408         // Strip newlines from all elements.
    409         $pee = wp_replace_in_html_tags( $pee, array( "\n" => " " ) );
     408        // Find newlines in all elements and add placeholders.
     409        $pee = wp_replace_in_html_tags( $pee, array( "\n" => " <!-- wpnl --> " ) );
    410410
    411411        if ( strpos( $pee, '<option' ) !== false ) {
     
    460460                $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee);
    461461
     462        // Restore newlines in all elements.
     463        $pee = str_replace( " <!-- wpnl --> ", "\n", $pee );
     464
    462465        return $pee;
     466}
     467
     468/**
     469 * Separate HTML elements and comments from the text.
     470 *
     471 * @since 4.2.4
     472 *
     473 * @param string $input The text which has to be formatted.
     474 * @return array The formatted text.
     475 */
     476function wp_html_split( $input ) {
     477        static $regex;
     478
     479        if ( ! isset( $regex ) ) {
     480                $comments =
     481                          '!'           // Start of comment, after the <.
     482                        . '(?:'         // Unroll the loop: Consume everything until --> is found.
     483                        .     '-(?!->)' // Dash not followed by end of comment.
     484                        .     '[^\-]*+' // Consume non-dashes.
     485                        . ')*+'         // Loop possessively.
     486                        . '(?:-->)?';   // End of comment. If not found, match all input.
     487
     488                $cdata =
     489                          '!\[CDATA\['  // Start of comment, after the <.
     490                        . '[^\]]*+'     // Consume non-].
     491                        . '(?:'         // Unroll the loop: Consume everything until ]]> is found.
     492                        .     '](?!]>)' // One ] not followed by end of comment.
     493                        .     '[^\]]*+' // Consume non-].
     494                        . ')*+'         // Loop possessively.
     495                        . '(?:]]>)?';   // End of comment. If not found, match all input.
     496
     497                $regex =
     498                          '/('              // Capture the entire match.
     499                        .     '<'           // Find start of element.
     500                        .     '(?(?=!--)'   // Is this a comment?
     501                        .         $comments // Find end of comment.
     502                        .     '|'
     503                        .         '(?(?=!\[CDATA\[)' // Is this a comment?
     504                        .             $cdata // Find end of comment.
     505                        .         '|'
     506                        .             '[^>]*>?' // Find end of element. If not found, match all input.
     507                        .         ')'
     508                        .     ')'
     509                        . ')/s';
     510        }
     511
     512        return preg_split( $regex, $input, -1, PREG_SPLIT_DELIM_CAPTURE );
    463513}
    464514
     
    474524function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
    475525        // Find all elements.
    476         $comments =
    477                   '!'           // Start of comment, after the <.
    478                 . '(?:'         // Unroll the loop: Consume everything until --> is found.
    479                 .     '-(?!->)' // Dash not followed by end of comment.
    480                 .     '[^\-]*+' // Consume non-dashes.
    481                 . ')*+'         // Loop possessively.
    482                 . '(?:-->)?';   // End of comment. If not found, match all input.
    483 
    484         $regex =
    485                   '/('              // Capture the entire match.
    486                 .     '<'           // Find start of element.
    487                 .     '(?(?=!--)'   // Is this a comment?
    488                 .         $comments // Find end of comment.
    489                 .     '|'
    490                 .         '[^>]*>?' // Find end of element. If not found, match all input.
    491                 .     ')'
    492                 . ')/s';
    493 
    494         $textarr = preg_split( $regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE );
     526        $textarr = wp_html_split( $haystack );
    495527        $changed = false;
    496528
Note: See TracChangeset for help on using the changeset viewer.