Changeset 33524
- Timestamp:
- 07/31/2015 01:45:01 AM (10 years ago)
- Location:
- branches/3.8
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.8/src/wp-includes/class-wp-embed.php
r33388 r33524 281 281 */ 282 282 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 -->' ) ); 285 285 286 286 // 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 ); 288 291 } 289 292 -
branches/3.8/src/wp-includes/formatting.php
r33388 r33524 249 249 $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines 250 250 251 // Strip newlines from all elements.252 $pee = wp_replace_in_html_tags( $pee, array( "\n" => " " ) );251 // Find newlines in all elements and add placeholders. 252 $pee = wp_replace_in_html_tags( $pee, array( "\n" => " <!-- wpnl --> " ) ); 253 253 254 254 if ( strpos($pee, '<object') !== false ) { … … 282 282 $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee); 283 283 284 // Restore newlines in all elements. 285 $pee = str_replace( " <!-- wpnl --> ", "\n", $pee ); 286 284 287 return $pee; 288 } 289 290 /** 291 * Separate HTML elements and comments from the text. 292 * 293 * @since 4.2.4 294 * 295 * @param string $input The text which has to be formatted. 296 * @return array The formatted text. 297 */ 298 function wp_html_split( $input ) { 299 static $regex; 300 301 if ( ! isset( $regex ) ) { 302 $comments = 303 '!' // Start of comment, after the <. 304 . '(?:' // Unroll the loop: Consume everything until --> is found. 305 . '-(?!->)' // Dash not followed by end of comment. 306 . '[^\-]*+' // Consume non-dashes. 307 . ')*+' // Loop possessively. 308 . '(?:-->)?'; // End of comment. If not found, match all input. 309 310 $cdata = 311 '!\[CDATA\[' // Start of comment, after the <. 312 . '[^\]]*+' // Consume non-]. 313 . '(?:' // Unroll the loop: Consume everything until ]]> is found. 314 . '](?!]>)' // One ] not followed by end of comment. 315 . '[^\]]*+' // Consume non-]. 316 . ')*+' // Loop possessively. 317 . '(?:]]>)?'; // End of comment. If not found, match all input. 318 319 $regex = 320 '/(' // Capture the entire match. 321 . '<' // Find start of element. 322 . '(?(?=!--)' // Is this a comment? 323 . $comments // Find end of comment. 324 . '|' 325 . '(?(?=!\[CDATA\[)' // Is this a comment? 326 . $cdata // Find end of comment. 327 . '|' 328 . '[^>]*>?' // Find end of element. If not found, match all input. 329 . ')' 330 . ')' 331 . ')/s'; 332 } 333 334 return preg_split( $regex, $input, -1, PREG_SPLIT_DELIM_CAPTURE ); 285 335 } 286 336 … … 296 346 function wp_replace_in_html_tags( $haystack, $replace_pairs ) { 297 347 // Find all elements. 298 $comments = 299 '!' // Start of comment, after the <. 300 . '(?:' // Unroll the loop: Consume everything until --> is found. 301 . '-(?!->)' // Dash not followed by end of comment. 302 . '[^\-]*+' // Consume non-dashes. 303 . ')*+' // Loop possessively. 304 . '(?:-->)?'; // End of comment. If not found, match all input. 305 306 $regex = 307 '/(' // Capture the entire match. 308 . '<' // Find start of element. 309 . '(?(?=!--)' // Is this a comment? 310 . $comments // Find end of comment. 311 . '|' 312 . '[^>]*>?' // Find end of element. If not found, match all input. 313 . ')' 314 . ')/s'; 315 316 $textarr = preg_split( $regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE ); 348 $textarr = wp_html_split( $haystack ); 317 349 $changed = false; 318 350 -
branches/3.8/src/wp-includes/shortcodes.php
r33388 r33524 320 320 321 321 $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 ); 342 323 343 324 foreach ( $textarr as &$element ) { 344 if ( ' <' !== $element[0] ) {325 if ( '' == $element || '<' !== $element[0] ) { 345 326 continue; 346 327 } … … 357 338 } 358 339 359 if ( $ignore_html || '<!--' === substr( $element, 0, 4 ) ) {340 if ( $ignore_html || '<!--' === substr( $element, 0, 4 ) || '<![CDATA[' === substr( $element, 0, 9 ) ) { 360 341 // Encode all [ and ] chars. 361 342 $element = strtr( $element, $trans ); -
branches/3.8/tests/phpunit/tests/formatting/Autop.php
r25002 r33524 99 99 $this->assertEquals( "<p>$str</p>", trim( wpautop( $str ) ) ); 100 100 } 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 101 147 } -
branches/3.8/tests/phpunit/tests/media.php
r25409 r33524 347 347 $this->assertEquals( $contents, $matches ); 348 348 } 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> 361 EOF; 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><!-- 375 my_function(); 376 // --> </script> 377 EOF; 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 389 Start. 390 [embed]http://www.youtube.com/embed/TEST01YRHA0[/embed] 391 <script><!-- 392 my_function(); 393 // --> </script> 394 http://www.youtube.com/embed/TEST02YRHA0 395 [embed]http://www.example.com/embed/TEST03YRHA0[/embed] 396 http://www.example.com/embed/TEST04YRHA0 397 Stop. 398 EOF; 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><!-- 404 my_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 411 EOF; 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 435 https://www.example.com/?video=1 436 EOF; 437 438 $expected = <<<EOF 439 440 @embed URL was replaced@ 441 442 EOF; 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'); 451 myvar = 'Hello world 452 https://www.example.com/?video=1 453 do not break this'; 454 // ]]> 455 </script> 456 EOF; 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 349 464 }
Note: See TracChangeset
for help on using the changeset viewer.