diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index be8ea6c994..98202f547b 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -496,7 +496,7 @@ function wpautop( $pee, $br = true ) { $allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)'; // Add a single line break above block-level opening tags. - $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee); + $pee = preg_replace('!(<' . $allblocks . '[\s/>])!', "\n$1", $pee); // Add a double line break below block-level closing tags. $pee = preg_replace('!()!', "$1\n\n", $pee); @@ -596,7 +596,9 @@ function wpautop( $pee, $br = true ) { $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee); // Restore newlines in all elements. - $pee = str_replace( " ", "\n", $pee ); + if ( false !== strpos( $pee, '' ) ) { + $pee = str_replace( array( ' ', '' ), "\n", $pee ); + } return $pee; } diff --git a/tests/phpunit/tests/formatting/Autop.php b/tests/phpunit/tests/formatting/Autop.php index e746321061..ccc0d3ac48 100644 --- a/tests/phpunit/tests/formatting/Autop.php +++ b/tests/phpunit/tests/formatting/Autop.php @@ -328,6 +328,7 @@ Paragraph two.'; 'summary', ); + // Check whitespace normalization. $content = array(); foreach ( $blocks as $block ) { @@ -335,9 +336,37 @@ Paragraph two.'; } $expected = join( "\n", $content ); - $content = join( "\n\n", $content ); // WS difference + $input = join( "\n\n", $content ); // WS difference - $this->assertEquals( $expected, trim( wpautop( $content ) ) ); + $this->assertEquals( $expected, trim( wpautop( $input ) ) ); + + $input = join( "", $content ); // WS difference + + $this->assertEquals( $expected, trim( wpautop( $input ) ) ); + + // Check whitespace addition. + $content = array(); + + foreach ( $blocks as $block ) { + $content[] = "<$block/>"; + } + + $expected = join( "\n", $content ); + $input = join( "", $content ); + + $this->assertEquals( $expected, trim( wpautop( $input ) ) ); + + // Check whitespace addition with attributes. + $content = array(); + + foreach ( $blocks as $block ) { + $content[] = "<$block attr='value'>foo"; + } + + $expected = join( "\n", $content ); + $input = join( "", $content ); + + $this->assertEquals( $expected, trim( wpautop( $input ) ) ); } /** @@ -420,6 +449,14 @@ Paragraph two.'; "Hello ", "

Hello

\n", ), + array( + "Hello ", + "

Hello

\n", + ), + array( + "Hello ", + "

Hello

\n", + ), /* Block elements inside comments will fail this test in all versions, it's not a regression. array( "Hello ",