Changeset 33521
- Timestamp:
- 07/31/2015 01:42:39 AM (10 years ago)
- Location:
- branches/4.1
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/4.1/src/wp-includes/class-wp-embed.php
r33380 r33521 313 313 */ 314 314 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 -->' ) ); 317 317 318 318 // 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 ); 320 323 } 321 324 -
branches/4.1/src/wp-includes/formatting.php
r33380 r33521 411 411 $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines 412 412 413 // Strip newlines from all elements.414 $pee = wp_replace_in_html_tags( $pee, array( "\n" => " " ) );413 // Find newlines in all elements and add placeholders. 414 $pee = wp_replace_in_html_tags( $pee, array( "\n" => " <!-- wpnl --> " ) ); 415 415 416 416 if ( strpos( $pee, '<option' ) !== false ) { … … 465 465 $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee); 466 466 467 // Restore newlines in all elements. 468 $pee = str_replace( " <!-- wpnl --> ", "\n", $pee ); 469 467 470 return $pee; 471 } 472 473 /** 474 * Separate HTML elements and comments from the text. 475 * 476 * @since 4.2.4 477 * 478 * @param string $input The text which has to be formatted. 479 * @return array The formatted text. 480 */ 481 function wp_html_split( $input ) { 482 static $regex; 483 484 if ( ! isset( $regex ) ) { 485 $comments = 486 '!' // Start of comment, after the <. 487 . '(?:' // Unroll the loop: Consume everything until --> is found. 488 . '-(?!->)' // Dash not followed by end of comment. 489 . '[^\-]*+' // Consume non-dashes. 490 . ')*+' // Loop possessively. 491 . '(?:-->)?'; // End of comment. If not found, match all input. 492 493 $cdata = 494 '!\[CDATA\[' // Start of comment, after the <. 495 . '[^\]]*+' // Consume non-]. 496 . '(?:' // Unroll the loop: Consume everything until ]]> is found. 497 . '](?!]>)' // One ] not followed by end of comment. 498 . '[^\]]*+' // Consume non-]. 499 . ')*+' // Loop possessively. 500 . '(?:]]>)?'; // End of comment. If not found, match all input. 501 502 $regex = 503 '/(' // Capture the entire match. 504 . '<' // Find start of element. 505 . '(?(?=!--)' // Is this a comment? 506 . $comments // Find end of comment. 507 . '|' 508 . '(?(?=!\[CDATA\[)' // Is this a comment? 509 . $cdata // Find end of comment. 510 . '|' 511 . '[^>]*>?' // Find end of element. If not found, match all input. 512 . ')' 513 . ')' 514 . ')/s'; 515 } 516 517 return preg_split( $regex, $input, -1, PREG_SPLIT_DELIM_CAPTURE ); 468 518 } 469 519 … … 479 529 function wp_replace_in_html_tags( $haystack, $replace_pairs ) { 480 530 // Find all elements. 481 $comments = 482 '!' // Start of comment, after the <. 483 . '(?:' // Unroll the loop: Consume everything until --> is found. 484 . '-(?!->)' // Dash not followed by end of comment. 485 . '[^\-]*+' // Consume non-dashes. 486 . ')*+' // Loop possessively. 487 . '(?:-->)?'; // End of comment. If not found, match all input. 488 489 $regex = 490 '/(' // Capture the entire match. 491 . '<' // Find start of element. 492 . '(?(?=!--)' // Is this a comment? 493 . $comments // Find end of comment. 494 . '|' 495 . '[^>]*>?' // Find end of element. If not found, match all input. 496 . ')' 497 . ')/s'; 498 499 $textarr = preg_split( $regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE ); 531 $textarr = wp_html_split( $haystack ); 500 532 $changed = false; 501 533 -
branches/4.1/src/wp-includes/shortcodes.php
r33380 r33521 326 326 327 327 $pattern = get_shortcode_regex(); 328 329 $comment_regex = 330 '!' // Start of comment, after the <. 331 . '(?:' // Unroll the loop: Consume everything until --> is found. 332 . '-(?!->)' // Dash not followed by end of comment. 333 . '[^\-]*+' // Consume non-dashes. 334 . ')*+' // Loop possessively. 335 . '(?:-->)?'; // End of comment. If not found, match all input. 336 337 $regex = 338 '/(' // Capture the entire match. 339 . '<' // Find start of element. 340 . '(?(?=!--)' // Is this a comment? 341 . $comment_regex // Find end of comment. 342 . '|' 343 . '[^>]*>?' // Find end of element. If not found, match all input. 344 . ')' 345 . ')/s'; 346 347 $textarr = preg_split( $regex, $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ); 328 $textarr = wp_html_split( $content ); 348 329 349 330 foreach ( $textarr as &$element ) { 350 if ( ' <' !== $element[0] ) {331 if ( '' == $element || '<' !== $element[0] ) { 351 332 continue; 352 333 } … … 363 344 } 364 345 365 if ( $ignore_html || '<!--' === substr( $element, 0, 4 ) ) {346 if ( $ignore_html || '<!--' === substr( $element, 0, 4 ) || '<![CDATA[' === substr( $element, 0, 9 ) ) { 366 347 // Encode all [ and ] chars. 367 348 $element = strtr( $element, $trans ); -
branches/4.1/tests/phpunit/tests/formatting/Autop.php
r29788 r33521 401 401 $this->assertEquals( $expected, trim( wpautop( $content ) ) ); 402 402 } 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 403 449 } -
branches/4.1/tests/phpunit/tests/media.php
r33374 r33521 468 468 } 469 469 470 /** 471 * @ticket 33016 472 */ 473 function test_multiline_cdata() { 474 global $wp_embed; 475 476 $content = <<<EOF 477 <script>// <![CDATA[ 478 _my_function('data'); 479 // ]]> 480 </script> 481 EOF; 482 483 $result = $wp_embed->autoembed( $content ); 484 $this->assertEquals( $content, $result ); 485 } 486 487 /** 488 * @ticket 33016 489 */ 490 function test_multiline_comment() { 491 global $wp_embed; 492 493 $content = <<<EOF 494 <script><!-- 495 my_function(); 496 // --> </script> 497 EOF; 498 499 $result = $wp_embed->autoembed( $content ); 500 $this->assertEquals( $content, $result ); 501 } 502 503 504 /** 505 * @ticket 33016 506 */ 507 function test_multiline_comment_with_embeds() { 508 $content = <<<EOF 509 Start. 510 [embed]http://www.youtube.com/embed/TEST01YRHA0[/embed] 511 <script><!-- 512 my_function(); 513 // --> </script> 514 http://www.youtube.com/embed/TEST02YRHA0 515 [embed]http://www.example.com/embed/TEST03YRHA0[/embed] 516 http://www.example.com/embed/TEST04YRHA0 517 Stop. 518 EOF; 519 520 $expected = <<<EOF 521 <p>Start.</p> 522 <p>https://youtube.com/watch?v=TEST01YRHA0</p> 523 <p><script><!-- 524 my_function(); 525 // --> </script></p> 526 <p>https://youtube.com/watch?v=TEST02YRHA0</p> 527 <p><a href="http://www.example.com/embed/TEST03YRHA0">http://www.example.com/embed/TEST03YRHA0</a></p> 528 <p>http://www.example.com/embed/TEST04YRHA0</p> 529 <p>Stop.</p> 530 531 EOF; 532 533 $result = apply_filters( 'the_content', $content ); 534 $this->assertEquals( $expected, $result ); 535 } 536 537 /** 538 * @ticket 33016 539 */ 540 function filter_wp_embed_shortcode_custom( $content, $url ) { 541 if ( 'https://www.example.com/?video=1' == $url ) { 542 $content = '@embed URL was replaced@'; 543 } 544 return $content; 545 } 546 547 /** 548 * @ticket 33016 549 */ 550 function test_oembed_explicit_media_link() { 551 global $wp_embed; 552 add_filter( 'embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10, 2 ); 553 554 $content = <<<EOF 555 https://www.example.com/?video=1 556 EOF; 557 558 $expected = <<<EOF 559 560 @embed URL was replaced@ 561 562 EOF; 563 564 $result = $wp_embed->autoembed( $content ); 565 $this->assertEquals( $expected, $result ); 566 567 $content = <<<EOF 568 <a href="https://www.example.com/?video=1">https://www.example.com/?video=1</a> 569 <script>// <![CDATA[ 570 _my_function('data'); 571 myvar = 'Hello world 572 https://www.example.com/?video=1 573 do not break this'; 574 // ]]> 575 </script> 576 EOF; 577 578 $result = $wp_embed->autoembed( $content ); 579 $this->assertEquals( $content, $result ); 580 581 remove_filter( 'embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10 ); 582 } 470 583 }
Note: See TracChangeset
for help on using the changeset viewer.