WordPress.org

Make WordPress Core

Changeset 33518


Ignore:
Timestamp:
07/30/2015 07:40:33 PM (5 years ago)
Author:
azaozz
Message:

Backport r33469 and r33470 to 4.2.
See #33106.

Location:
branches/4.2
Files:
5 edited

Legend:

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

    r33360 r33518  
    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.2/src/wp-includes/formatting.php

    r33360 r33518  
    420420
    421421    // Standardize newline characters to "\n".
    422     $pee = str_replace(array("\r\n", "\r"), "\n", $pee); 
    423 
    424     // Strip newlines from all elements.
    425     $pee = wp_replace_in_html_tags( $pee, array( "\n" => " " ) );
     422    $pee = str_replace(array("\r\n", "\r"), "\n", $pee);
     423
     424    // Find newlines in all elements and add placeholders.
     425    $pee = wp_replace_in_html_tags( $pee, array( "\n" => " <!-- wpnl --> " ) );
    426426
    427427    // Collapse line breaks before and after <option> elements so they don't get autop'd.
     
    510510        $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee);
    511511
     512    // Restore newlines in all elements.
     513    $pee = str_replace( " <!-- wpnl --> ", "\n", $pee );
     514
    512515    return $pee;
     516}
     517
     518/**
     519 * Separate HTML elements and comments from the text.
     520 *
     521 * @since 4.2.4
     522 *
     523 * @param string $input The text which has to be formatted.
     524 * @return array The formatted text.
     525 */
     526function wp_html_split( $input ) {
     527    static $regex;
     528
     529    if ( ! isset( $regex ) ) {
     530        $comments =
     531              '!'           // Start of comment, after the <.
     532            . '(?:'         // Unroll the loop: Consume everything until --> is found.
     533            .     '-(?!->)' // Dash not followed by end of comment.
     534            .     '[^\-]*+' // Consume non-dashes.
     535            . ')*+'         // Loop possessively.
     536            . '(?:-->)?';   // End of comment. If not found, match all input.
     537
     538        $cdata =
     539              '!\[CDATA\['  // Start of comment, after the <.
     540            . '[^\]]*+'     // Consume non-].
     541            . '(?:'         // Unroll the loop: Consume everything until ]]> is found.
     542            .     '](?!]>)' // One ] not followed by end of comment.
     543            .     '[^\]]*+' // Consume non-].
     544            . ')*+'         // Loop possessively.
     545            . '(?:]]>)?';   // End of comment. If not found, match all input.
     546
     547        $regex =
     548              '/('              // Capture the entire match.
     549            .     '<'           // Find start of element.
     550            .     '(?(?=!--)'   // Is this a comment?
     551            .         $comments // Find end of comment.
     552            .     '|'
     553            .         '(?(?=!\[CDATA\[)' // Is this a comment?
     554            .             $cdata // Find end of comment.
     555            .         '|'
     556            .             '[^>]*>?' // Find end of element. If not found, match all input.
     557            .         ')'
     558            .     ')'
     559            . ')/s';
     560    }
     561
     562    return preg_split( $regex, $input, -1, PREG_SPLIT_DELIM_CAPTURE );
    513563}
    514564
     
    524574function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
    525575    // Find all elements.
    526     $comments =
    527           '!'           // Start of comment, after the <.
    528         . '(?:'         // Unroll the loop: Consume everything until --> is found.
    529         .     '-(?!->)' // Dash not followed by end of comment.
    530         .     '[^\-]*+' // Consume non-dashes.
    531         . ')*+'         // Loop possessively.
    532         . '(?:-->)?';   // End of comment. If not found, match all input.
    533 
    534     $regex =
    535           '/('              // Capture the entire match.
    536         .     '<'           // Find start of element.
    537         .     '(?(?=!--)'   // Is this a comment?
    538         .         $comments // Find end of comment.
    539         .     '|'
    540         .         '[^>]*>?' // Find end of element. If not found, match all input.
    541         .     ')'
    542         . ')/s';
    543 
    544     $textarr = preg_split( $regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE );
     576    $textarr = wp_html_split( $haystack );
    545577    $changed = false;
    546578
  • branches/4.2/src/wp-includes/shortcodes.php

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

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

    r32365 r33518  
    560560    }
    561561
     562    /**
     563     * @ticket 33016
     564     */
     565    function test_multiline_cdata() {
     566        global $wp_embed;
     567
     568        $content = <<<EOF
     569<script>// <![CDATA[
     570_my_function('data');
     571// ]]>
     572</script>
     573EOF;
     574
     575        $result = $wp_embed->autoembed( $content );
     576        $this->assertEquals( $content, $result );
     577    }
     578
     579    /**
     580     * @ticket 33016
     581     */
     582    function test_multiline_comment() {
     583        global $wp_embed;
     584
     585        $content = <<<EOF
     586<script><!--
     587my_function();
     588// --> </script>
     589EOF;
     590
     591        $result = $wp_embed->autoembed( $content );
     592        $this->assertEquals( $content, $result );
     593    }
     594
     595
     596    /**
     597     * @ticket 33016
     598     */
     599    function test_multiline_comment_with_embeds() {
     600        $content = <<<EOF
     601Start.
     602[embed]http://www.youtube.com/embed/TEST01YRHA0[/embed]
     603<script><!--
     604my_function();
     605// --> </script>
     606http://www.youtube.com/embed/TEST02YRHA0
     607[embed]http://www.example.com/embed/TEST03YRHA0[/embed]
     608http://www.example.com/embed/TEST04YRHA0
     609Stop.
     610EOF;
     611
     612        $expected = <<<EOF
     613<p>Start.<br />
     614https://youtube.com/watch?v=TEST01YRHA0<br />
     615<script><!--
     616my_function();
     617// --> </script><br />
     618https://youtube.com/watch?v=TEST02YRHA0<br />
     619<a href="http://www.example.com/embed/TEST03YRHA0">http://www.example.com/embed/TEST03YRHA0</a><br />
     620http://www.example.com/embed/TEST04YRHA0<br />
     621Stop.</p>
     622
     623EOF;
     624
     625        $result = apply_filters( 'the_content', $content );
     626        $this->assertEquals( $expected, $result );
     627    }
     628
     629    /**
     630     * @ticket 33016
     631     */
     632    function filter_wp_embed_shortcode_custom( $content, $url ) {
     633        if ( 'https://www.example.com/?video=1' == $url ) {
     634            $content = '@embed URL was replaced@';
     635        }
     636        return $content;
     637    }
     638
     639    /**
     640     * @ticket 33016
     641     */
     642    function test_oembed_explicit_media_link() {
     643        global $wp_embed;
     644        add_filter( 'embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10, 2 );
     645
     646        $content = <<<EOF
     647https://www.example.com/?video=1
     648EOF;
     649
     650        $expected = <<<EOF
     651@embed URL was replaced@
     652EOF;
     653
     654        $result = $wp_embed->autoembed( $content );
     655        $this->assertEquals( $expected, $result );
     656
     657        $content = <<<EOF
     658<a href="https://www.example.com/?video=1">https://www.example.com/?video=1</a>
     659<script>// <![CDATA[
     660_my_function('data');
     661myvar = 'Hello world
     662https://www.example.com/?video=1
     663do not break this';
     664// ]]>
     665</script>
     666EOF;
     667
     668        $result = $wp_embed->autoembed( $content );
     669        $this->assertEquals( $content, $result );
     670
     671        remove_filter( 'embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10 );
     672    }
    562673}
Note: See TracChangeset for help on using the changeset viewer.