WordPress.org

Make WordPress Core


Ignore:
Timestamp:
07/31/2015 01:42:39 AM (5 years ago)
Author:
azaozz
Message:

Backport r33469 and r33470 to 4.1.
See #33106.

File:
1 edited

Legend:

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

    r33380 r33521  
    411411    $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
    412412
    413     // Strip newlines from all elements.
    414     $pee = wp_replace_in_html_tags( $pee, array( "\n" => " " ) );
     413    // Find newlines in all elements and add placeholders.
     414    $pee = wp_replace_in_html_tags( $pee, array( "\n" => " <!-- wpnl --> " ) );
    415415
    416416    if ( strpos( $pee, '<option' ) !== false ) {
     
    465465        $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee);
    466466
     467    // Restore newlines in all elements.
     468    $pee = str_replace( " <!-- wpnl --> ", "\n", $pee );
     469
    467470    return $pee;
     471}
     472
     473/**
     474 * Separate HTML elements and comments from the text.
     475 *
     476 * @since 4.2.4
     477 *
     478 * @param string $input The text which has to be formatted.
     479 * @return array The formatted text.
     480 */
     481function wp_html_split( $input ) {
     482    static $regex;
     483
     484    if ( ! isset( $regex ) ) {
     485        $comments =
     486              '!'           // Start of comment, after the <.
     487            . '(?:'         // Unroll the loop: Consume everything until --> is found.
     488            .     '-(?!->)' // Dash not followed by end of comment.
     489            .     '[^\-]*+' // Consume non-dashes.
     490            . ')*+'         // Loop possessively.
     491            . '(?:-->)?';   // End of comment. If not found, match all input.
     492
     493        $cdata =
     494              '!\[CDATA\['  // Start of comment, after the <.
     495            . '[^\]]*+'     // Consume non-].
     496            . '(?:'         // Unroll the loop: Consume everything until ]]> is found.
     497            .     '](?!]>)' // One ] not followed by end of comment.
     498            .     '[^\]]*+' // Consume non-].
     499            . ')*+'         // Loop possessively.
     500            . '(?:]]>)?';   // End of comment. If not found, match all input.
     501
     502        $regex =
     503              '/('              // Capture the entire match.
     504            .     '<'           // Find start of element.
     505            .     '(?(?=!--)'   // Is this a comment?
     506            .         $comments // Find end of comment.
     507            .     '|'
     508            .         '(?(?=!\[CDATA\[)' // Is this a comment?
     509            .             $cdata // Find end of comment.
     510            .         '|'
     511            .             '[^>]*>?' // Find end of element. If not found, match all input.
     512            .         ')'
     513            .     ')'
     514            . ')/s';
     515    }
     516
     517    return preg_split( $regex, $input, -1, PREG_SPLIT_DELIM_CAPTURE );
    468518}
    469519
     
    479529function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
    480530    // Find all elements.
    481     $comments =
    482           '!'           // Start of comment, after the <.
    483         . '(?:'         // Unroll the loop: Consume everything until --> is found.
    484         .     '-(?!->)' // Dash not followed by end of comment.
    485         .     '[^\-]*+' // Consume non-dashes.
    486         . ')*+'         // Loop possessively.
    487         . '(?:-->)?';   // End of comment. If not found, match all input.
    488 
    489     $regex =
    490           '/('              // Capture the entire match.
    491         .     '<'           // Find start of element.
    492         .     '(?(?=!--)'   // Is this a comment?
    493         .         $comments // Find end of comment.
    494         .     '|'
    495         .         '[^>]*>?' // Find end of element. If not found, match all input.
    496         .     ')'
    497         . ')/s';
    498 
    499     $textarr = preg_split( $regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE );
     531    $textarr = wp_html_split( $haystack );
    500532    $changed = false;
    501533
Note: See TracChangeset for help on using the changeset viewer.