Make WordPress Core

Changeset 33522


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

Backport r33469 and r33470 to 4.0.
See #33106.

Location:
branches/4.0
Files:
5 edited

Legend:

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

    r33381 r33522  
    327327     */
    328328    public function autoembed( $content ) {
    329         // Strip newlines from all elements.
    330         $content = wp_replace_in_html_tags( $content, array( "\n" => " " ) );
     329        // Replace line breaks from all HTML elements with placeholders.
     330        $content = wp_replace_in_html_tags( $content, array( "\n" => '<!-- wp-line-break -->' ) );
    331331
    332332        // Find URLs that are on their own line.
    333         return preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array( $this, 'autoembed_callback' ), $content );
     333        $content = preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array( $this, 'autoembed_callback' ), $content );
     334
     335        // Put the line breaks back.
     336        return str_replace( '<!-- wp-line-break -->', "\n", $content );
    334337    }
    335338
  • branches/4.0/src/wp-includes/formatting.php

    r33381 r33522  
    406406    $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
    407407
    408     // Strip newlines from all elements.
    409     $pee = wp_replace_in_html_tags( $pee, array( "\n" => " " ) );
     408    // Find newlines in all elements and add placeholders.
     409    $pee = wp_replace_in_html_tags( $pee, array( "\n" => " <!-- wpnl --> " ) );
    410410
    411411    if ( strpos( $pee, '<option' ) !== false ) {
     
    460460        $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee);
    461461
     462    // Restore newlines in all elements.
     463    $pee = str_replace( " <!-- wpnl --> ", "\n", $pee );
     464
    462465    return $pee;
     466}
     467
     468/**
     469 * Separate HTML elements and comments from the text.
     470 *
     471 * @since 4.2.4
     472 *
     473 * @param string $input The text which has to be formatted.
     474 * @return array The formatted text.
     475 */
     476function wp_html_split( $input ) {
     477    static $regex;
     478
     479    if ( ! isset( $regex ) ) {
     480        $comments =
     481              '!'           // Start of comment, after the <.
     482            . '(?:'         // Unroll the loop: Consume everything until --> is found.
     483            .     '-(?!->)' // Dash not followed by end of comment.
     484            .     '[^\-]*+' // Consume non-dashes.
     485            . ')*+'         // Loop possessively.
     486            . '(?:-->)?';   // End of comment. If not found, match all input.
     487
     488        $cdata =
     489              '!\[CDATA\['  // Start of comment, after the <.
     490            . '[^\]]*+'     // Consume non-].
     491            . '(?:'         // Unroll the loop: Consume everything until ]]> is found.
     492            .     '](?!]>)' // One ] not followed by end of comment.
     493            .     '[^\]]*+' // Consume non-].
     494            . ')*+'         // Loop possessively.
     495            . '(?:]]>)?';   // End of comment. If not found, match all input.
     496
     497        $regex =
     498              '/('              // Capture the entire match.
     499            .     '<'           // Find start of element.
     500            .     '(?(?=!--)'   // Is this a comment?
     501            .         $comments // Find end of comment.
     502            .     '|'
     503            .         '(?(?=!\[CDATA\[)' // Is this a comment?
     504            .             $cdata // Find end of comment.
     505            .         '|'
     506            .             '[^>]*>?' // Find end of element. If not found, match all input.
     507            .         ')'
     508            .     ')'
     509            . ')/s';
     510    }
     511
     512    return preg_split( $regex, $input, -1, PREG_SPLIT_DELIM_CAPTURE );
    463513}
    464514
     
    474524function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
    475525    // Find all elements.
    476     $comments =
    477           '!'           // Start of comment, after the <.
    478         . '(?:'         // Unroll the loop: Consume everything until --> is found.
    479         .     '-(?!->)' // Dash not followed by end of comment.
    480         .     '[^\-]*+' // Consume non-dashes.
    481         . ')*+'         // Loop possessively.
    482         . '(?:-->)?';   // End of comment. If not found, match all input.
    483 
    484     $regex =
    485           '/('              // Capture the entire match.
    486         .     '<'           // Find start of element.
    487         .     '(?(?=!--)'   // Is this a comment?
    488         .         $comments // Find end of comment.
    489         .     '|'
    490         .         '[^>]*>?' // Find end of element. If not found, match all input.
    491         .     ')'
    492         . ')/s';
    493 
    494     $textarr = preg_split( $regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE );
     526    $textarr = wp_html_split( $haystack );
    495527    $changed = false;
    496528
  • branches/4.0/src/wp-includes/shortcodes.php

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

    r29328 r33522  
    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.0/tests/phpunit/tests/media.php

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