Make WordPress Core

Changeset 33525


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

Backport r33469 and r33470 to 3.7.
See #33106.

Location:
branches/3.7
Files:
5 edited

Legend:

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

    r33389 r33525  
    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.7/src/wp-includes/formatting.php

    r33389 r33525  
    235235    $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
    236236
    237     // Strip newlines from all elements.
    238     $pee = wp_replace_in_html_tags( $pee, array( "\n" => " " ) );
     237    // Find newlines in all elements and add placeholders.
     238    $pee = wp_replace_in_html_tags( $pee, array( "\n" => " <!-- wpnl --> " ) );
    239239
    240240    if ( strpos($pee, '<object') !== false ) {
     
    268268        $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee);
    269269
     270    // Restore newlines in all elements.
     271    $pee = str_replace( " <!-- wpnl --> ", "\n", $pee );
     272
    270273    return $pee;
     274}
     275
     276/**
     277 * Separate HTML elements and comments from the text.
     278 *
     279 * @since 4.2.4
     280 *
     281 * @param string $input The text which has to be formatted.
     282 * @return array The formatted text.
     283 */
     284function wp_html_split( $input ) {
     285    static $regex;
     286
     287    if ( ! isset( $regex ) ) {
     288        $comments =
     289              '!'           // Start of comment, after the <.
     290            . '(?:'         // Unroll the loop: Consume everything until --> is found.
     291            .     '-(?!->)' // Dash not followed by end of comment.
     292            .     '[^\-]*+' // Consume non-dashes.
     293            . ')*+'         // Loop possessively.
     294            . '(?:-->)?';   // End of comment. If not found, match all input.
     295
     296        $cdata =
     297              '!\[CDATA\['  // Start of comment, after the <.
     298            . '[^\]]*+'     // Consume non-].
     299            . '(?:'         // Unroll the loop: Consume everything until ]]> is found.
     300            .     '](?!]>)' // One ] not followed by end of comment.
     301            .     '[^\]]*+' // Consume non-].
     302            . ')*+'         // Loop possessively.
     303            . '(?:]]>)?';   // End of comment. If not found, match all input.
     304
     305        $regex =
     306              '/('              // Capture the entire match.
     307            .     '<'           // Find start of element.
     308            .     '(?(?=!--)'   // Is this a comment?
     309            .         $comments // Find end of comment.
     310            .     '|'
     311            .         '(?(?=!\[CDATA\[)' // Is this a comment?
     312            .             $cdata // Find end of comment.
     313            .         '|'
     314            .             '[^>]*>?' // Find end of element. If not found, match all input.
     315            .         ')'
     316            .     ')'
     317            . ')/s';
     318    }
     319
     320    return preg_split( $regex, $input, -1, PREG_SPLIT_DELIM_CAPTURE );
    271321}
    272322
     
    282332function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
    283333    // Find all elements.
    284     $comments =
    285           '!'           // Start of comment, after the <.
    286         . '(?:'         // Unroll the loop: Consume everything until --> is found.
    287         .     '-(?!->)' // Dash not followed by end of comment.
    288         .     '[^\-]*+' // Consume non-dashes.
    289         . ')*+'         // Loop possessively.
    290         . '(?:-->)?';   // End of comment. If not found, match all input.
    291 
    292     $regex =
    293           '/('              // Capture the entire match.
    294         .     '<'           // Find start of element.
    295         .     '(?(?=!--)'   // Is this a comment?
    296         .         $comments // Find end of comment.
    297         .     '|'
    298         .         '[^>]*>?' // Find end of element. If not found, match all input.
    299         .     ')'
    300         . ')/s';
    301 
    302     $textarr = preg_split( $regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE );
     334    $textarr = wp_html_split( $haystack );
    303335    $changed = false;
    304336
  • branches/3.7/src/wp-includes/shortcodes.php

    r33389 r33525  
    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.7/tests/phpunit/tests/formatting/Autop.php

    r25002 r33525  
    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.7/tests/phpunit/tests/media.php

    r25409 r33525  
    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.