WordPress.org

Make WordPress Core


Ignore:
Timestamp:
07/31/2015 01:45:34 AM (6 years ago)
Author:
azaozz
Message:

Backport r33469 and r33470 to 3.7.
See #33106.

File:
1 edited

Legend:

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

    r33389 r33525  
    235235    $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
    236236
    237     // Strip newlines from all elements.
    238     $pee = wp_replace_in_html_tags( $pee, array( "\n" => " " ) );
     237    // Find newlines in all elements and add placeholders.
     238    $pee = wp_replace_in_html_tags( $pee, array( "\n" => " <!-- wpnl --> " ) );
    239239
    240240    if ( strpos($pee, '<object') !== false ) {
     
    268268        $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee);
    269269
     270    // Restore newlines in all elements.
     271    $pee = str_replace( " <!-- wpnl --> ", "\n", $pee );
     272
    270273    return $pee;
     274}
     275
     276/**
     277 * Separate HTML elements and comments from the text.
     278 *
     279 * @since 4.2.4
     280 *
     281 * @param string $input The text which has to be formatted.
     282 * @return array The formatted text.
     283 */
     284function wp_html_split( $input ) {
     285    static $regex;
     286
     287    if ( ! isset( $regex ) ) {
     288        $comments =
     289              '!'           // Start of comment, after the <.
     290            . '(?:'         // Unroll the loop: Consume everything until --> is found.
     291            .     '-(?!->)' // Dash not followed by end of comment.
     292            .     '[^\-]*+' // Consume non-dashes.
     293            . ')*+'         // Loop possessively.
     294            . '(?:-->)?';   // End of comment. If not found, match all input.
     295
     296        $cdata =
     297              '!\[CDATA\['  // Start of comment, after the <.
     298            . '[^\]]*+'     // Consume non-].
     299            . '(?:'         // Unroll the loop: Consume everything until ]]> is found.
     300            .     '](?!]>)' // One ] not followed by end of comment.
     301            .     '[^\]]*+' // Consume non-].
     302            . ')*+'         // Loop possessively.
     303            . '(?:]]>)?';   // End of comment. If not found, match all input.
     304
     305        $regex =
     306              '/('              // Capture the entire match.
     307            .     '<'           // Find start of element.
     308            .     '(?(?=!--)'   // Is this a comment?
     309            .         $comments // Find end of comment.
     310            .     '|'
     311            .         '(?(?=!\[CDATA\[)' // Is this a comment?
     312            .             $cdata // Find end of comment.
     313            .         '|'
     314            .             '[^>]*>?' // Find end of element. If not found, match all input.
     315            .         ')'
     316            .     ')'
     317            . ')/s';
     318    }
     319
     320    return preg_split( $regex, $input, -1, PREG_SPLIT_DELIM_CAPTURE );
    271321}
    272322
     
    282332function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
    283333    // Find all elements.
    284     $comments =
    285           '!'           // Start of comment, after the <.
    286         . '(?:'         // Unroll the loop: Consume everything until --> is found.
    287         .     '-(?!->)' // Dash not followed by end of comment.
    288         .     '[^\-]*+' // Consume non-dashes.
    289         . ')*+'         // Loop possessively.
    290         . '(?:-->)?';   // End of comment. If not found, match all input.
    291 
    292     $regex =
    293           '/('              // Capture the entire match.
    294         .     '<'           // Find start of element.
    295         .     '(?(?=!--)'   // Is this a comment?
    296         .         $comments // Find end of comment.
    297         .     '|'
    298         .         '[^>]*>?' // Find end of element. If not found, match all input.
    299         .     ')'
    300         . ')/s';
    301 
    302     $textarr = preg_split( $regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE );
     334    $textarr = wp_html_split( $haystack );
    303335    $changed = false;
    304336
Note: See TracChangeset for help on using the changeset viewer.