Make WordPress Core

Changeset 33521


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

Backport r33469 and r33470 to 4.1.
See #33106.

Location:
branches/4.1
Files:
5 edited

Legend:

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

    r33380 r33521  
    313313         */
    314314        public function autoembed( $content ) {
    315                 // Strip newlines from all elements.
    316                 $content = wp_replace_in_html_tags( $content, array( "\n" => " " ) );
     315                // Replace line breaks from all HTML elements with placeholders.
     316                $content = wp_replace_in_html_tags( $content, array( "\n" => '<!-- wp-line-break -->' ) );
    317317
    318318                // Find URLs that are on their own line.
    319                 return preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array( $this, 'autoembed_callback' ), $content );
     319                $content = preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array( $this, 'autoembed_callback' ), $content );
     320
     321                // Put the line breaks back.
     322                return str_replace( '<!-- wp-line-break -->', "\n", $content );
    320323        }
    321324
  • 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
  • branches/4.1/src/wp-includes/shortcodes.php

    r33380 r33521  
    326326       
    327327        $pattern = get_shortcode_regex();
    328 
    329         $comment_regex =
    330                   '!'           // Start of comment, after the <.
    331                 . '(?:'         // Unroll the loop: Consume everything until --> is found.
    332                 .     '-(?!->)' // Dash not followed by end of comment.
    333                 .     '[^\-]*+' // Consume non-dashes.
    334                 . ')*+'         // Loop possessively.
    335                 . '(?:-->)?';   // End of comment. If not found, match all input.
    336 
    337         $regex =
    338                   '/('                   // Capture the entire match.
    339                 .     '<'                // Find start of element.
    340                 .     '(?(?=!--)'        // Is this a comment?
    341                 .         $comment_regex // Find end of comment.
    342                 .     '|'
    343                 .         '[^>]*>?'      // Find end of element. If not found, match all input.
    344                 .     ')'
    345                 . ')/s';
    346 
    347         $textarr = preg_split( $regex, $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
     328        $textarr = wp_html_split( $content );
    348329
    349330        foreach ( $textarr as &$element ) {
    350                 if ( '<' !== $element[0] ) {
     331                if ( '' == $element || '<' !== $element[0] ) {
    351332                        continue;
    352333                }
    … …  
    363344                }
    364345
    365                 if ( $ignore_html || '<!--' === substr( $element, 0, 4 ) ) {
     346                if ( $ignore_html || '<!--' === substr( $element, 0, 4 ) || '<![CDATA[' === substr( $element, 0, 9 ) ) {
    366347                        // Encode all [ and ] chars.
    367348                        $element = strtr( $element, $trans );
  • branches/4.1/tests/phpunit/tests/formatting/Autop.php

    r29788 r33521  
    401401                $this->assertEquals( $expected, trim( wpautop( $content ) ) );
    402402        }
     403
     404        /**
     405         * Do not allow newlines within HTML elements to become mangled.
     406         *
     407         * @ticket 33106
     408         * @dataProvider data_element_sanity
     409         */
     410        function test_element_sanity( $input, $output ) {
     411                return $this->assertEquals( $output, wpautop( $input ) );
     412        }
     413
     414        function data_element_sanity() {
     415                return array(
     416                        array(
     417                                "Hello <a\nhref='world'>",
     418                                "<p>Hello <a\nhref='world'></p>\n",
     419                        ),
     420                        array(
     421                                "Hello <!-- a\nhref='world' -->",
     422                                "<p>Hello <!-- a\nhref='world' --></p>\n",
     423                        ),
     424/* Block elements inside comments will fail this test in all versions, it's not a regression.
     425                        array(
     426                                "Hello <!-- <hr> a\nhref='world' -->",
     427                                "<p>Hello <!-- <hr> a\nhref='world' --></p>\n",
     428                        ),
     429                        array(
     430                                "Hello <![CDATA[ <hr> a\nhttps://youtu.be/jgz0uSaOZbE\n ]]>",
     431                                "<p>Hello <![CDATA[ <hr> a\nhttps://youtu.be/jgz0uSaOZbE\n ]]></p>\n",
     432                        ),
     433*/
     434                        array(
     435                                "Hello <![CDATA[ a\nhttps://youtu.be/jgz0uSaOZbE\n ]]>",
     436                                "<p>Hello <![CDATA[ a\nhttps://youtu.be/jgz0uSaOZbE\n ]]></p>\n",
     437                        ),
     438                        array(
     439                                "Hello <![CDATA[ <!-- a\nhttps://youtu.be/jgz0uSaOZbE\n a\n9 ]]> -->",
     440                                "<p>Hello <![CDATA[ <!-- a\nhttps://youtu.be/jgz0uSaOZbE\n a\n9 ]]> --></p>\n",
     441                        ),
     442                        array(
     443                                "Hello <![CDATA[ <!-- a\nhttps://youtu.be/jgz0uSaOZbE\n a\n9 --> a\n9 ]]>",
     444                                "<p>Hello <![CDATA[ <!-- a\nhttps://youtu.be/jgz0uSaOZbE\n a\n9 --> a\n9 ]]></p>\n",
     445                        ),
     446                );
     447        }
     448
    403449}
  • branches/4.1/tests/phpunit/tests/media.php

    r33374 r33521  
    468468        }
    469469
     470        /**
     471         * @ticket 33016
     472         */
     473        function test_multiline_cdata() {
     474                global $wp_embed;
     475
     476                $content = <<<EOF
     477<script>// <![CDATA[
     478_my_function('data');
     479// ]]>
     480</script>
     481EOF;
     482
     483                $result = $wp_embed->autoembed( $content );
     484                $this->assertEquals( $content, $result );
     485        }
     486
     487        /**
     488         * @ticket 33016
     489         */
     490        function test_multiline_comment() {
     491                global $wp_embed;
     492
     493                $content = <<<EOF
     494<script><!--
     495my_function();
     496// --> </script>
     497EOF;
     498
     499                $result = $wp_embed->autoembed( $content );
     500                $this->assertEquals( $content, $result );
     501        }
     502
     503
     504        /**
     505         * @ticket 33016
     506         */
     507        function test_multiline_comment_with_embeds() {
     508                $content = <<<EOF
     509Start.
     510[embed]http://www.youtube.com/embed/TEST01YRHA0[/embed]
     511<script><!--
     512my_function();
     513// --> </script>
     514http://www.youtube.com/embed/TEST02YRHA0
     515[embed]http://www.example.com/embed/TEST03YRHA0[/embed]
     516http://www.example.com/embed/TEST04YRHA0
     517Stop.
     518EOF;
     519
     520                $expected = <<<EOF
     521<p>Start.</p>
     522<p>https://youtube.com/watch?v=TEST01YRHA0</p>
     523<p><script><!--
     524my_function();
     525// --> </script></p>
     526<p>https://youtube.com/watch?v=TEST02YRHA0</p>
     527<p><a href="http://www.example.com/embed/TEST03YRHA0">http://www.example.com/embed/TEST03YRHA0</a></p>
     528<p>http://www.example.com/embed/TEST04YRHA0</p>
     529<p>Stop.</p>
     530
     531EOF;
     532
     533                $result = apply_filters( 'the_content', $content );
     534                $this->assertEquals( $expected, $result );
     535        }
     536
     537        /**
     538         * @ticket 33016
     539         */
     540        function filter_wp_embed_shortcode_custom( $content, $url ) {
     541                if ( 'https://www.example.com/?video=1' == $url ) {
     542                        $content = '@embed URL was replaced@';
     543                }
     544                return $content;
     545        }
     546
     547        /**
     548         * @ticket 33016
     549         */
     550        function test_oembed_explicit_media_link() {
     551                global $wp_embed;
     552                add_filter( 'embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10, 2 );
     553
     554                $content = <<<EOF
     555https://www.example.com/?video=1
     556EOF;
     557
     558                $expected = <<<EOF
     559
     560@embed URL was replaced@
     561
     562EOF;
     563
     564                $result = $wp_embed->autoembed( $content );
     565                $this->assertEquals( $expected, $result );
     566
     567                $content = <<<EOF
     568<a href="https://www.example.com/?video=1">https://www.example.com/?video=1</a>
     569<script>// <![CDATA[
     570_my_function('data');
     571myvar = 'Hello world
     572https://www.example.com/?video=1
     573do not break this';
     574// ]]>
     575</script>
     576EOF;
     577
     578                $result = $wp_embed->autoembed( $content );
     579                $this->assertEquals( $content, $result );
     580
     581                remove_filter( 'embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10 );
     582        }
    470583}
Note: See TracChangeset for help on using the changeset viewer.