Fix creating of extra `<br />` tags in both PHP and JS variants of wpautop(). Add PHP tests to catch similar problems in the future.

Props valendesigns, azaozz. Fixes #33377.

git-svn-id: https://develop.svn.wordpress.org/trunk@33624 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Ozz 2015-08-17 17:35:58 +00:00
parent 3b1e1f0376
commit b30fcd7597
3 changed files with 54 additions and 3 deletions

View File

@ -272,7 +272,13 @@
text = text.replace( /<\/blockquote>\s*<\/p>/gi, '</p></blockquote>');
text = text.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' );
text = text.replace( new RegExp( '(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
text = text.replace( /\s*\n/gi, '<br />\n');
// Remove redundant spaces and line breaks after existing <br /> tags
text = text.replace( /(<br[^>]*>)\s*\n/gi, '$1' );
// Create <br /> from the remaining line breaks
text = text.replace( /\s*\n/g, '<br />\n');
text = text.replace( new RegExp( '(</?(?:' + blocklist + ')[^>]*>)\\s*<br />', 'gi' ), '$1' );
text = text.replace( /<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1' );
text = text.replace( /(?:<p>|<br ?\/?>)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|<br ?\/?>)*/gi, '[caption$1[/caption]' );

View File

@ -491,7 +491,7 @@ function wpautop( $pee, $br = true ) {
$pee .= $last_pee;
}
// Change multiple <br>s into two line breaks, which will turn into paragraphs.
$pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
$pee = preg_replace('|<br\s*/?>\s*<br\s*/?>|', "\n\n", $pee);
$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)';
@ -574,6 +574,9 @@ function wpautop( $pee, $br = true ) {
// Replace newlines that shouldn't be touched with a placeholder.
$pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', '_autop_newline_preservation_helper', $pee);
// Normalize <br>
$pee = str_replace( array( '<br>', '<br/>' ), '<br />', $pee );
// Replace any new line characters that aren't preceded by a <br /> with a <br />.
$pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee);

View File

@ -444,5 +444,47 @@ Paragraph two.';
),
);
}
/**
* wpautop() should not convert line breaks after <br /> tags
*
* @ticket 33377
*/
function test_that_wpautop_skips_line_breaks_after_br() {
$content = '
line 1<br>
line 2<br/>
line 3<br />
line 4
line 5
';
$expected = '<p>line 1<br />
line 2<br />
line 3<br />
line 4<br />
line 5</p>';
$this->assertEquals( $expected, trim( wpautop( $content ) ) );
}
/**
* wpautop() should convert multiple line breaks into a paragraph regarless of <br /> format
*
* @ticket 33377
*/
function test_that_wpautop_adds_a_paragraph_after_multiple_br() {
$content = '
line 1<br>
<br/>
line 2<br/>
<br />
';
$expected = '<p>line 1</p>
<p>line 2</p>';
$this->assertEquals( $expected, trim( wpautop( $content ) ) );
}
}