Make WordPress Core


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

Backport r33469 and r33470 to 3.8.
See #33106.

File:
1 edited

Legend:

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

    r33388 r33524  
    249249    $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
    250250
    251     // Strip newlines from all elements.
    252     $pee = wp_replace_in_html_tags( $pee, array( "\n" => " " ) );
     251    // Find newlines in all elements and add placeholders.
     252    $pee = wp_replace_in_html_tags( $pee, array( "\n" => " <!-- wpnl --> " ) );
    253253
    254254    if ( strpos($pee, '<object') !== false ) {
     
    282282        $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee);
    283283
     284    // Restore newlines in all elements.
     285    $pee = str_replace( " <!-- wpnl --> ", "\n", $pee );
     286
    284287    return $pee;
     288}
     289
     290/**
     291 * Separate HTML elements and comments from the text.
     292 *
     293 * @since 4.2.4
     294 *
     295 * @param string $input The text which has to be formatted.
     296 * @return array The formatted text.
     297 */
     298function wp_html_split( $input ) {
     299    static $regex;
     300
     301    if ( ! isset( $regex ) ) {
     302        $comments =
     303              '!'           // Start of comment, after the <.
     304            . '(?:'         // Unroll the loop: Consume everything until --> is found.
     305            .     '-(?!->)' // Dash not followed by end of comment.
     306            .     '[^\-]*+' // Consume non-dashes.
     307            . ')*+'         // Loop possessively.
     308            . '(?:-->)?';   // End of comment. If not found, match all input.
     309
     310        $cdata =
     311              '!\[CDATA\['  // Start of comment, after the <.
     312            . '[^\]]*+'     // Consume non-].
     313            . '(?:'         // Unroll the loop: Consume everything until ]]> is found.
     314            .     '](?!]>)' // One ] not followed by end of comment.
     315            .     '[^\]]*+' // Consume non-].
     316            . ')*+'         // Loop possessively.
     317            . '(?:]]>)?';   // End of comment. If not found, match all input.
     318
     319        $regex =
     320              '/('              // Capture the entire match.
     321            .     '<'           // Find start of element.
     322            .     '(?(?=!--)'   // Is this a comment?
     323            .         $comments // Find end of comment.
     324            .     '|'
     325            .         '(?(?=!\[CDATA\[)' // Is this a comment?
     326            .             $cdata // Find end of comment.
     327            .         '|'
     328            .             '[^>]*>?' // Find end of element. If not found, match all input.
     329            .         ')'
     330            .     ')'
     331            . ')/s';
     332    }
     333
     334    return preg_split( $regex, $input, -1, PREG_SPLIT_DELIM_CAPTURE );
    285335}
    286336
     
    296346function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
    297347    // Find all elements.
    298     $comments =
    299           '!'           // Start of comment, after the <.
    300         . '(?:'         // Unroll the loop: Consume everything until --> is found.
    301         .     '-(?!->)' // Dash not followed by end of comment.
    302         .     '[^\-]*+' // Consume non-dashes.
    303         . ')*+'         // Loop possessively.
    304         . '(?:-->)?';   // End of comment. If not found, match all input.
    305 
    306     $regex =
    307           '/('              // Capture the entire match.
    308         .     '<'           // Find start of element.
    309         .     '(?(?=!--)'   // Is this a comment?
    310         .         $comments // Find end of comment.
    311         .     '|'
    312         .         '[^>]*>?' // Find end of element. If not found, match all input.
    313         .     ')'
    314         . ')/s';
    315 
    316     $textarr = preg_split( $regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE );
     348    $textarr = wp_html_split( $haystack );
    317349    $changed = false;
    318350
Note: See TracChangeset for help on using the changeset viewer.