Ticket #33106: 33106.5.patch
File 33106.5.patch, 6.0 KB (added by , 9 years ago) |
---|
-
src/wp-includes/class-wp-embed.php
318 318 * @return string Potentially modified $content. 319 319 */ 320 320 public function autoembed( $content ) { 321 // Strip newlines from all elements. 322 $content = wp_replace_in_html_tags( $content, array( "\n" => " " ) ); 321 // Avoid multiline elements and comments. 322 $content = wp_html_split( $content ); 323 for ( $i = 0, $c = count( $content ); $i < $c; $i += 2 ) { 323 324 324 // Find URLs that are on their own line. 325 return preg_replace_callback( '|^(\s*)(https?://[^\s"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content ); 325 if ( '' == $content[$i] || false === strpos( $content[$i], 'http' ) ) { 326 continue; 327 } 328 329 // Find URLs that are on their own line. 330 $content[$i] = preg_replace_callback( '|^(\s*)(https?://[^\s"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content[$i] ); 331 332 } 333 334 return implode( $content ); 326 335 } 327 336 328 337 /** -
src/wp-includes/formatting.php
504 504 // Standardize newline characters to "\n". 505 505 $pee = str_replace(array("\r\n", "\r"), "\n", $pee); 506 506 507 // Strip newlines from all elements.508 $pee = wp_replace_in_html_tags( $pee, array( "\n" => " " ) );507 // Find newlines in all elements and add placeholders. 508 $pee = wp_replace_in_html_tags( $pee, array( "\n" => " <!-- wpnl --> " ) ); 509 509 510 510 // Collapse line breaks before and after <option> elements so they don't get autop'd. 511 511 if ( strpos( $pee, '<option' ) !== false ) { … … 592 592 if ( !empty($pre_tags) ) 593 593 $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee); 594 594 595 // Restore newlines in all elements. 596 $pee = str_replace( " <!-- wpnl --> ", "\n", $pee ); 597 595 598 return $pee; 596 599 } 597 600 598 601 /** 599 * Replace characters or phrases within HTML elements only.602 * Separate HTML elements and comments from the text. 600 603 * 601 * @since 4.2. 3604 * @since 4.2.4 602 605 * 603 * @param string $haystack The text which has to be formatted. 604 * @param array $replace_pairs In the form array('from' => 'to', ...). 605 * @return string The formatted text. 606 * @param string $input The text which has to be formatted. 607 * @return array The formatted text. 606 608 */ 607 function wp_ replace_in_html_tags( $haystack, $replace_pairs) {609 function wp_html_split( $input ) { 608 610 // Find all elements. 609 611 $comments = 610 612 '!' // Start of comment, after the <. … … 614 616 . ')*+' // Loop possessively. 615 617 . '(?:-->)?'; // End of comment. If not found, match all input. 616 618 619 $cdata = 620 '!\[CDATA\[' // Start of comment, after the <. 621 . '[^\]]*+' // Consume non-]. 622 . '(?:' // Unroll the loop: Consume everything until ]]> is found. 623 . '](?!]>)' // One ] not followed by end of comment. 624 . '[^\]]*+' // Consume non-]. 625 . ')*+' // Loop possessively. 626 . '(?:]]>)?'; // End of comment. If not found, match all input. 627 617 628 $regex = 618 629 '/(' // Capture the entire match. 619 630 . '<' // Find start of element. … … 620 631 . '(?(?=!--)' // Is this a comment? 621 632 . $comments // Find end of comment. 622 633 . '|' 623 . '[^>]*>?' // Find end of element. If not found, match all input. 634 . '(?(?=!\[CDATA\[)' // Is this a comment? 635 . $cdata // Find end of comment. 636 . '|' 637 . '[^>]*>?' // Find end of element. If not found, match all input. 638 . ')' 624 639 . ')' 625 640 . ')/s'; 626 641 627 $textarr = preg_split( $regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE ); 642 return preg_split( $regex, $input, -1, PREG_SPLIT_DELIM_CAPTURE ); 643 } 644 645 /** 646 * Replace characters or phrases within HTML elements only. 647 * 648 * @since 4.2.3 649 * 650 * @param string $haystack The text which has to be formatted. 651 * @param array $replace_pairs In the form array('from' => 'to', ...). 652 * @return string The formatted text. 653 */ 654 function wp_replace_in_html_tags( $haystack, $replace_pairs ) { 655 // Find all elements. 656 $textarr = wp_html_split( $haystack ); 628 657 $changed = false; 629 658 630 659 // Optimize when searching for one item. -
tests/phpunit/tests/formatting/Autop.php
399 399 400 400 $this->assertEquals( $expected, trim( wpautop( $content ) ) ); 401 401 } 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 402 448 }