From b30fcd75975d3951d0fba0517ae6e02516403616 Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Mon, 17 Aug 2015 17:35:58 +0000 Subject: [PATCH] Fix creating of extra `
` 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 --- src/wp-admin/js/editor.js | 8 ++++- src/wp-includes/formatting.php | 5 ++- tests/phpunit/tests/formatting/Autop.php | 44 +++++++++++++++++++++++- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/wp-admin/js/editor.js b/src/wp-admin/js/editor.js index 3d56e36bd4..d64d3e6045 100644 --- a/src/wp-admin/js/editor.js +++ b/src/wp-admin/js/editor.js @@ -272,7 +272,13 @@ text = text.replace( /<\/blockquote>\s*<\/p>/gi, '

'); text = text.replace( new RegExp( '

\\s*(]*)?>)', 'gi' ), '$1' ); text = text.replace( new RegExp( '(]*)?>)\\s*

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

|
)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|
)*/gi, '[caption$1[/caption]' ); diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 54d4355e86..70deb84d57 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -491,7 +491,7 @@ function wpautop( $pee, $br = true ) { $pee .= $last_pee; } // Change multiple
s into two line breaks, which will turn into paragraphs. - $pee = preg_replace('|
\s*
|', "\n\n", $pee); + $pee = preg_replace('|\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
+ $pee = str_replace( array( '
', '
' ), '
', $pee ); + // Replace any new line characters that aren't preceded by a
with a
. $pee = preg_replace('|(?)\s*\n|', "
\n", $pee); diff --git a/tests/phpunit/tests/formatting/Autop.php b/tests/phpunit/tests/formatting/Autop.php index 0612d3c996..e746321061 100644 --- a/tests/phpunit/tests/formatting/Autop.php +++ b/tests/phpunit/tests/formatting/Autop.php @@ -444,5 +444,47 @@ Paragraph two.'; ), ); } - + + /** + * wpautop() should not convert line breaks after
tags + * + * @ticket 33377 + */ + function test_that_wpautop_skips_line_breaks_after_br() { + $content = ' +line 1
+line 2
+line 3
+line 4 +line 5 +'; + + $expected = '

line 1
+line 2
+line 3
+line 4
+line 5

'; + + $this->assertEquals( $expected, trim( wpautop( $content ) ) ); + } + + /** + * wpautop() should convert multiple line breaks into a paragraph regarless of
format + * + * @ticket 33377 + */ + function test_that_wpautop_adds_a_paragraph_after_multiple_br() { + $content = ' +line 1
+
+line 2
+
+'; + + $expected = '

line 1

+

line 2

'; + + $this->assertEquals( $expected, trim( wpautop( $content ) ) ); + } + }