Make WordPress Core

Changeset 33523


Ignore:
Timestamp:
07/31/2015 01:44:04 AM (10 years ago)
Author:
azaozz
Message:

Backport r33469 and r33470 to 3.9.
See #33106.

Location:
branches/3.9
Files:
5 edited

Legend:

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

    r33386 r33523  
    292292     */
    293293    function autoembed( $content ) {
    294         // Strip newlines from all elements.
    295         $content = wp_replace_in_html_tags( $content, array( "\n" => " " ) );
     294        // Replace line breaks from all HTML elements with placeholders.
     295        $content = wp_replace_in_html_tags( $content, array( "\n" => '<!-- wp-line-break -->' ) );
    296296
    297297        // Find URLs that are on their own line.
    298         return preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array( $this, 'autoembed_callback' ), $content );
     298        $content = preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array( $this, 'autoembed_callback' ), $content );
     299
     300        // Put the line breaks back.
     301        return str_replace( '<!-- wp-line-break -->', "\n", $content );
    299302    }
    300303
  • branches/3.9/src/wp-includes/formatting.php

    r33386 r33523  
    292292    $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
    293293
    294     // Strip newlines from all elements.
    295     $pee = wp_replace_in_html_tags( $pee, array( "\n" => " " ) );
     294    // Find newlines in all elements and add placeholders.
     295    $pee = wp_replace_in_html_tags( $pee, array( "\n" => " <!-- wpnl --> " ) );
    296296
    297297    if ( strpos( $pee, '</object>' ) !== false ) {
     
    340340        $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee);
    341341
     342    // Restore newlines in all elements.
     343    $pee = str_replace( " <!-- wpnl --> ", "\n", $pee );
     344
    342345    return $pee;
     346}
     347
     348/**
     349 * Separate HTML elements and comments from the text.
     350 *
     351 * @since 4.2.4
     352 *
     353 * @param string $input The text which has to be formatted.
     354 * @return array The formatted text.
     355 */
     356function wp_html_split( $input ) {
     357    static $regex;
     358
     359    if ( ! isset( $regex ) ) {
     360        $comments =
     361              '!'           // Start of comment, after the <.
     362            . '(?:'         // Unroll the loop: Consume everything until --> is found.
     363            .     '-(?!->)' // Dash not followed by end of comment.
     364            .     '[^\-]*+' // Consume non-dashes.
     365            . ')*+'         // Loop possessively.
     366            . '(?:-->)?';   // End of comment. If not found, match all input.
     367
     368        $cdata =
     369              '!\[CDATA\['  // Start of comment, after the <.
     370            . '[^\]]*+'     // Consume non-].
     371            . '(?:'         // Unroll the loop: Consume everything until ]]> is found.
     372            .     '](?!]>)' // One ] not followed by end of comment.
     373            .     '[^\]]*+' // Consume non-].
     374            . ')*+'         // Loop possessively.
     375            . '(?:]]>)?';   // End of comment. If not found, match all input.
     376
     377        $regex =
     378              '/('              // Capture the entire match.
     379            .     '<'           // Find start of element.
     380            .     '(?(?=!--)'   // Is this a comment?
     381            .         $comments // Find end of comment.
     382            .     '|'
     383            .         '(?(?=!\[CDATA\[)' // Is this a comment?
     384            .             $cdata // Find end of comment.
     385            .         '|'
     386            .             '[^>]*>?' // Find end of element. If not found, match all input.
     387            .         ')'
     388            .     ')'
     389            . ')/s';
     390    }
     391
     392    return preg_split( $regex, $input, -1, PREG_SPLIT_DELIM_CAPTURE );
    343393}
    344394
     
    354404function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
    355405    // Find all elements.
    356     $comments =
    357           '!'           // Start of comment, after the <.
    358         . '(?:'         // Unroll the loop: Consume everything until --> is found.
    359         .     '-(?!->)' // Dash not followed by end of comment.
    360         .     '[^\-]*+' // Consume non-dashes.
    361         . ')*+'         // Loop possessively.
    362         . '(?:-->)?';   // End of comment. If not found, match all input.
    363 
    364     $regex =
    365           '/('              // Capture the entire match.
    366         .     '<'           // Find start of element.
    367         .     '(?(?=!--)'   // Is this a comment?
    368         .         $comments // Find end of comment.
    369         .     '|'
    370         .         '[^>]*>?' // Find end of element. If not found, match all input.
    371         .     ')'
    372         . ')/s';
    373 
    374     $textarr = preg_split( $regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE );
     406    $textarr = wp_html_split( $haystack );
    375407    $changed = false;
    376408
  • branches/3.9/src/wp-includes/shortcodes.php

    r33386 r33523  
    330330   
    331331    $pattern = get_shortcode_regex();
    332 
    333     $comment_regex =
    334           '!'           // Start of comment, after the <.
    335         . '(?:'         // Unroll the loop: Consume everything until --> is found.
    336         .     '-(?!->)' // Dash not followed by end of comment.
    337         .     '[^\-]*+' // Consume non-dashes.
    338         . ')*+'         // Loop possessively.
    339         . '(?:-->)?';   // End of comment. If not found, match all input.
    340 
    341     $regex =
    342           '/('                   // Capture the entire match.
    343         .     '<'                // Find start of element.
    344         .     '(?(?=!--)'        // Is this a comment?
    345         .         $comment_regex // Find end of comment.
    346         .     '|'
    347         .         '[^>]*>?'      // Find end of element. If not found, match all input.
    348         .     ')'
    349         . ')/s';
    350 
    351     $textarr = preg_split( $regex, $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
     332    $textarr = wp_html_split( $content );
    352333
    353334    foreach ( $textarr as &$element ) {
    354         if ( '<' !== $element[0] ) {
     335        if ( '' == $element || '<' !== $element[0] ) {
    355336            continue;
    356337        }
     
    367348        }
    368349
    369         if ( $ignore_html || '<!--' === substr( $element, 0, 4 ) ) {
     350        if ( $ignore_html || '<!--' === substr( $element, 0, 4 ) || '<![CDATA[' === substr( $element, 0, 9 ) ) {
    370351            // Encode all [ and ] chars.
    371352            $element = strtr( $element, $trans );
  • branches/3.9/tests/phpunit/tests/formatting/Autop.php

    r27761 r33523  
    274274        $this->assertEquals( "<p>$str</p>", trim( wpautop( $str ) ) );
    275275    }
     276
     277    /**
     278     * Do not allow newlines within HTML elements to become mangled.
     279     *
     280     * @ticket 33106
     281     * @dataProvider data_element_sanity
     282     */
     283    function test_element_sanity( $input, $output ) {
     284        return $this->assertEquals( $output, wpautop( $input ) );
     285    }
     286
     287    function data_element_sanity() {
     288        return array(
     289            array(
     290                "Hello <a\nhref='world'>",
     291                "<p>Hello <a\nhref='world'></p>\n",
     292            ),
     293            array(
     294                "Hello <!-- a\nhref='world' -->",
     295                "<p>Hello <!-- a\nhref='world' --></p>\n",
     296            ),
     297/* Block elements inside comments will fail this test in all versions, it's not a regression.
     298            array(
     299                "Hello <!-- <hr> a\nhref='world' -->",
     300                "<p>Hello <!-- <hr> a\nhref='world' --></p>\n",
     301            ),
     302            array(
     303                "Hello <![CDATA[ <hr> a\nhttps://youtu.be/jgz0uSaOZbE\n ]]>",
     304                "<p>Hello <![CDATA[ <hr> a\nhttps://youtu.be/jgz0uSaOZbE\n ]]></p>\n",
     305            ),
     306*/
     307            array(
     308                "Hello <![CDATA[ a\nhttps://youtu.be/jgz0uSaOZbE\n ]]>",
     309                "<p>Hello <![CDATA[ a\nhttps://youtu.be/jgz0uSaOZbE\n ]]></p>\n",
     310            ),
     311            array(
     312                "Hello <![CDATA[ <!-- a\nhttps://youtu.be/jgz0uSaOZbE\n a\n9 ]]> -->",
     313                "<p>Hello <![CDATA[ <!-- a\nhttps://youtu.be/jgz0uSaOZbE\n a\n9 ]]> --></p>\n",
     314            ),
     315            array(
     316                "Hello <![CDATA[ <!-- a\nhttps://youtu.be/jgz0uSaOZbE\n a\n9 --> a\n9 ]]>",
     317                "<p>Hello <![CDATA[ <!-- a\nhttps://youtu.be/jgz0uSaOZbE\n a\n9 --> a\n9 ]]></p>\n",
     318            ),
     319        );
     320    }
     321
    276322}
  • branches/3.9/tests/phpunit/tests/media.php

    r27726 r33523  
    438438    }
    439439
     440    /**
     441     * @ticket 33016
     442     */
     443    function test_multiline_cdata() {
     444        global $wp_embed;
     445
     446        $content = <<<EOF
     447<script>// <![CDATA[
     448_my_function('data');
     449// ]]>
     450</script>
     451EOF;
     452
     453        $result = $wp_embed->autoembed( $content );
     454        $this->assertEquals( $content, $result );
     455    }
     456
     457    /**
     458     * @ticket 33016
     459     */
     460    function test_multiline_comment() {
     461        global $wp_embed;
     462
     463        $content = <<<EOF
     464<script><!--
     465my_function();
     466// --> </script>
     467EOF;
     468
     469        $result = $wp_embed->autoembed( $content );
     470        $this->assertEquals( $content, $result );
     471    }
     472
     473
     474    /**
     475     * @ticket 33016
     476     */
     477    function test_multiline_comment_with_embeds() {
     478        $content = <<<EOF
     479Start.
     480[embed]http://www.youtube.com/embed/TEST01YRHA0[/embed]
     481<script><!--
     482my_function();
     483// --> </script>
     484http://www.youtube.com/embed/TEST02YRHA0
     485[embed]http://www.example.com/embed/TEST03YRHA0[/embed]
     486http://www.example.com/embed/TEST04YRHA0
     487Stop.
     488EOF;
     489
     490        $expected = <<<EOF
     491<p>Start.<br />
     492<a href="http://www.youtube.com/embed/TEST01YRHA0">http://www.youtube.com/embed/TEST01YRHA0</a><br />
     493<script><!--
     494my_function();
     495// --> </script></p>
     496<p>http://www.youtube.com/embed/TEST02YRHA0</p>
     497<p><a href="http://www.example.com/embed/TEST03YRHA0">http://www.example.com/embed/TEST03YRHA0</a></p>
     498<p>http://www.example.com/embed/TEST04YRHA0</p>
     499<p>Stop.</p>
     500
     501EOF;
     502
     503        $result = apply_filters( 'the_content', $content );
     504        $this->assertEquals( $expected, $result );
     505    }
     506
     507    /**
     508     * @ticket 33016
     509     */
     510    function filter_wp_embed_shortcode_custom( $content, $url ) {
     511        if ( 'https://www.example.com/?video=1' == $url ) {
     512            $content = '@embed URL was replaced@';
     513        }
     514        return $content;
     515    }
     516
     517    /**
     518     * @ticket 33016
     519     */
     520    function test_oembed_explicit_media_link() {
     521        global $wp_embed;
     522        add_filter( 'embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10, 2 );
     523
     524        $content = <<<EOF
     525https://www.example.com/?video=1
     526EOF;
     527
     528        $expected = <<<EOF
     529
     530@embed URL was replaced@
     531
     532EOF;
     533
     534        $result = $wp_embed->autoembed( $content );
     535        $this->assertEquals( $expected, $result );
     536
     537        $content = <<<EOF
     538<a href="https://www.example.com/?video=1">https://www.example.com/?video=1</a>
     539<script>// <![CDATA[
     540_my_function('data');
     541myvar = 'Hello world
     542https://www.example.com/?video=1
     543do not break this';
     544// ]]>
     545</script>
     546EOF;
     547
     548        $result = $wp_embed->autoembed( $content );
     549        $this->assertEquals( $content, $result );
     550
     551        remove_filter( 'embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10 );
     552    }
    440553}
Note: See TracChangeset for help on using the changeset viewer.