Make WordPress Core

Changeset 33524


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

Backport r33469 and r33470 to 3.8.
See #33106.

Location:
branches/3.8
Files:
5 edited

Legend:

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

    r33388 r33524  
    281281         */
    282282        function autoembed( $content ) {
    283                 // Strip newlines from all elements.
    284                 $content = wp_replace_in_html_tags( $content, array( "\n" => " " ) );
     283                // Replace line breaks from all HTML elements with placeholders.
     284                $content = wp_replace_in_html_tags( $content, array( "\n" => '<!-- wp-line-break -->' ) );
    285285
    286286                // Find URLs that are on their own line.
    287                 return preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array( $this, 'autoembed_callback' ), $content );
     287                $content = preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array( $this, 'autoembed_callback' ), $content );
     288
     289                // Put the line breaks back.
     290                return str_replace( '<!-- wp-line-break -->', "\n", $content );
    288291        }
    289292
  • 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
  • branches/3.8/src/wp-includes/shortcodes.php

    r33388 r33524  
    320320       
    321321        $pattern = get_shortcode_regex();
    322 
    323         $comment_regex =
    324                   '!'           // Start of comment, after the <.
    325                 . '(?:'         // Unroll the loop: Consume everything until --> is found.
    326                 .     '-(?!->)' // Dash not followed by end of comment.
    327                 .     '[^\-]*+' // Consume non-dashes.
    328                 . ')*+'         // Loop possessively.
    329                 . '(?:-->)?';   // End of comment. If not found, match all input.
    330 
    331         $regex =
    332                   '/('                   // Capture the entire match.
    333                 .     '<'                // Find start of element.
    334                 .     '(?(?=!--)'        // Is this a comment?
    335                 .         $comment_regex // Find end of comment.
    336                 .     '|'
    337                 .         '[^>]*>?'      // Find end of element. If not found, match all input.
    338                 .     ')'
    339                 . ')/s';
    340 
    341         $textarr = preg_split( $regex, $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
     322        $textarr = wp_html_split( $content );
    342323
    343324        foreach ( $textarr as &$element ) {
    344                 if ( '<' !== $element[0] ) {
     325                if ( '' == $element || '<' !== $element[0] ) {
    345326                        continue;
    346327                }
     
    357338                }
    358339
    359                 if ( $ignore_html || '<!--' === substr( $element, 0, 4 ) ) {
     340                if ( $ignore_html || '<!--' === substr( $element, 0, 4 ) || '<![CDATA[' === substr( $element, 0, 9 ) ) {
    360341                        // Encode all [ and ] chars.
    361342                        $element = strtr( $element, $trans );
  • branches/3.8/tests/phpunit/tests/formatting/Autop.php

    r25002 r33524  
    9999                $this->assertEquals( "<p>$str</p>", trim( wpautop( $str ) ) );
    100100        }
     101
     102        /**
     103         * Do not allow newlines within HTML elements to become mangled.
     104         *
     105         * @ticket 33106
     106         * @dataProvider data_element_sanity
     107         */
     108        function test_element_sanity( $input, $output ) {
     109                return $this->assertEquals( $output, wpautop( $input ) );
     110        }
     111
     112        function data_element_sanity() {
     113                return array(
     114                        array(
     115                                "Hello <a\nhref='world'>",
     116                                "<p>Hello <a\nhref='world'></p>\n",
     117                        ),
     118                        array(
     119                                "Hello <!-- a\nhref='world' -->",
     120                                "<p>Hello <!-- a\nhref='world' --></p>\n",
     121                        ),
     122/* Block elements inside comments will fail this test in all versions, it's not a regression.
     123                        array(
     124                                "Hello <!-- <hr> a\nhref='world' -->",
     125                                "<p>Hello <!-- <hr> a\nhref='world' --></p>\n",
     126                        ),
     127                        array(
     128                                "Hello <![CDATA[ <hr> a\nhttps://youtu.be/jgz0uSaOZbE\n ]]>",
     129                                "<p>Hello <![CDATA[ <hr> a\nhttps://youtu.be/jgz0uSaOZbE\n ]]></p>\n",
     130                        ),
     131*/
     132                        array(
     133                                "Hello <![CDATA[ a\nhttps://youtu.be/jgz0uSaOZbE\n ]]>",
     134                                "<p>Hello <![CDATA[ a\nhttps://youtu.be/jgz0uSaOZbE\n ]]></p>\n",
     135                        ),
     136                        array(
     137                                "Hello <![CDATA[ <!-- a\nhttps://youtu.be/jgz0uSaOZbE\n a\n9 ]]> -->",
     138                                "<p>Hello <![CDATA[ <!-- a\nhttps://youtu.be/jgz0uSaOZbE\n a\n9 ]]> --></p>\n",
     139                        ),
     140                        array(
     141                                "Hello <![CDATA[ <!-- a\nhttps://youtu.be/jgz0uSaOZbE\n a\n9 --> a\n9 ]]>",
     142                                "<p>Hello <![CDATA[ <!-- a\nhttps://youtu.be/jgz0uSaOZbE\n a\n9 --> a\n9 ]]></p>\n",
     143                        ),
     144                );
     145        }
     146
    101147}
  • branches/3.8/tests/phpunit/tests/media.php

    r25409 r33524  
    347347                $this->assertEquals( $contents, $matches );
    348348        }
     349       
     350        /**
     351         * @ticket 33016
     352         */
     353        function test_multiline_cdata() {
     354                global $wp_embed;
     355
     356                $content = <<<EOF
     357<script>// <![CDATA[
     358_my_function('data');
     359// ]]>
     360</script>
     361EOF;
     362
     363                $result = $wp_embed->autoembed( $content );
     364                $this->assertEquals( $content, $result );
     365        }
     366
     367        /**
     368         * @ticket 33016
     369         */
     370        function test_multiline_comment() {
     371                global $wp_embed;
     372
     373                $content = <<<EOF
     374<script><!--
     375my_function();
     376// --> </script>
     377EOF;
     378
     379                $result = $wp_embed->autoembed( $content );
     380                $this->assertEquals( $content, $result );
     381        }
     382
     383
     384        /**
     385         * @ticket 33016
     386         */
     387        function test_multiline_comment_with_embeds() {
     388                $content = <<<EOF
     389Start.
     390[embed]http://www.youtube.com/embed/TEST01YRHA0[/embed]
     391<script><!--
     392my_function();
     393// --> </script>
     394http://www.youtube.com/embed/TEST02YRHA0
     395[embed]http://www.example.com/embed/TEST03YRHA0[/embed]
     396http://www.example.com/embed/TEST04YRHA0
     397Stop.
     398EOF;
     399
     400                $expected = <<<EOF
     401<p>Start.<br />
     402<a href="http://www.youtube.com/embed/TEST01YRHA0">http://www.youtube.com/embed/TEST01YRHA0</a><br />
     403<script><!--
     404my_function();
     405// --> </script></p>
     406<p>http://www.youtube.com/embed/TEST02YRHA0</p>
     407<p><a href="http://www.example.com/embed/TEST03YRHA0">http://www.example.com/embed/TEST03YRHA0</a></p>
     408<p>http://www.example.com/embed/TEST04YRHA0</p>
     409<p>Stop.</p>
     410
     411EOF;
     412
     413                $result = apply_filters( 'the_content', $content );
     414                $this->assertEquals( $expected, $result );
     415        }
     416
     417        /**
     418         * @ticket 33016
     419         */
     420        function filter_wp_embed_shortcode_custom( $content, $url ) {
     421                if ( 'https://www.example.com/?video=1' == $url ) {
     422                        $content = '@embed URL was replaced@';
     423                }
     424                return $content;
     425        }
     426
     427        /**
     428         * @ticket 33016
     429         */
     430        function test_oembed_explicit_media_link() {
     431                global $wp_embed;
     432                add_filter( 'embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10, 2 );
     433
     434                $content = <<<EOF
     435https://www.example.com/?video=1
     436EOF;
     437
     438                $expected = <<<EOF
     439
     440@embed URL was replaced@
     441
     442EOF;
     443
     444                $result = $wp_embed->autoembed( $content );
     445                $this->assertEquals( $expected, $result );
     446
     447                $content = <<<EOF
     448<a href="https://www.example.com/?video=1">https://www.example.com/?video=1</a>
     449<script>// <![CDATA[
     450_my_function('data');
     451myvar = 'Hello world
     452https://www.example.com/?video=1
     453do not break this';
     454// ]]>
     455</script>
     456EOF;
     457
     458                $result = $wp_embed->autoembed( $content );
     459                $this->assertEquals( $content, $result );
     460
     461                remove_filter( 'embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10 );
     462        }
     463
    349464}
Note: See TracChangeset for help on using the changeset viewer.