Make WordPress Core

Ticket #33106: 33106.5.patch

File 33106.5.patch, 6.0 KB (added by miqrogroove, 10 years ago)
  • src/wp-includes/class-wp-embed.php

     
    318318         * @return string Potentially modified $content.
    319319         */
    320320        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 ) {
    323324
    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 );
    326335        }
    327336
    328337        /**
  • src/wp-includes/formatting.php

     
    504504        // Standardize newline characters to "\n".
    505505        $pee = str_replace(array("\r\n", "\r"), "\n", $pee);
    506506
    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 --> " ) );
    509509
    510510        // Collapse line breaks before and after <option> elements so they don't get autop'd.
    511511        if ( strpos( $pee, '<option' ) !== false ) {
     
    592592        if ( !empty($pre_tags) )
    593593                $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee);
    594594
     595        // Restore newlines in all elements.
     596        $pee = str_replace( " <!-- wpnl --> ", "\n", $pee );
     597
    595598        return $pee;
    596599}
    597600
    598601/**
    599  * Replace characters or phrases within HTML elements only.
     602 * Separate HTML elements and comments from the text.
    600603 *
    601  * @since 4.2.3
     604 * @since 4.2.4
    602605 *
    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.
    606608 */
    607 function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
     609function wp_html_split( $input ) {
    608610        // Find all elements.
    609611        $comments =
    610612                  '!'           // Start of comment, after the <.
     
    614616                . ')*+'         // Loop possessively.
    615617                . '(?:-->)?';   // End of comment. If not found, match all input.
    616618
     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
    617628        $regex =
    618629                  '/('              // Capture the entire match.
    619630                .     '<'           // Find start of element.
     
    620631                .     '(?(?=!--)'   // Is this a comment?
    621632                .         $comments // Find end of comment.
    622633                .     '|'
    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                .         ')'
    624639                .     ')'
    625640                . ')/s';
    626641
    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 */
     654function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
     655        // Find all elements.
     656        $textarr = wp_html_split( $haystack );
    628657        $changed = false;
    629658
    630659        // Optimize when searching for one item.
  • tests/phpunit/tests/formatting/Autop.php

     
    399399
    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}