WordPress.org

Make WordPress Core

Opened 6 years ago

Last modified 3 years ago

#20444 new defect (bug)

wpautop acts on a single line of text

Reported by: niallkennedy Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Formatting Keywords: wpautop has-patch needs-refresh
Focuses: Cc:

Description

HTML markup on a single line wrapped in a block element is interpreted and changed by wpautop. The wpautop function is described as replacing "double line-breaks with paragraph elements" which led me to expect the single line would not become many lines.

echo wpautop( '<div><p>Hello world</p><span>WordPress</span></div>' );
<div>
<p>Hello world</p>
<p><span>WordPress</span></div>

Extra <p> before the <span>.

I expected wpautop would find no newlines and return the original string.

The same string placed inside a HTML5 or XHTML 1 Strict document <body> is parsed as valid by W3C Validator.

Plugins acting on the_content with a priority less than 10 may have their single-line markup reinterpreted by wpautop.

Attachments (3)

test-wpautop.php (225 bytes) - added by niallkennedy 6 years ago.
wp-load and call wpautop with a single line div
Autop-20444.diff (652 bytes) - added by jond 4 years ago.
Autop.php unit test coverage for #20444
Autop-20444-fixed.diff (643 bytes) - added by jond 4 years ago.
Updated expected results

Download all attachments as: .zip

Change History (12)

@niallkennedy
6 years ago

wp-load and call wpautop with a single line div

#1 @MarcusPope
6 years ago

Another (w3c valid fragment) variation that will fail is:

asdfasdfasdf<br/>asdfasdfasdf<h1>Hello</h1>

which results in:

<p>asdfasdfasdf<br/>asdfasdfasdf<br />

<h1>Hello</h1>

It's missing the closing p tag after <br />. Changing the first preg_replace|$allblocks line in formatting.php's wpautop function to replace matches with "\n\n$1" (instead of "\n$1") will fix the above, but creates problems for the test case attached to test-wpautop.php.

<div>

<p>hello</p>

<p><span>Wordpress</span></div>

Though I'm guessing this ticket will be closed invalid as #7937 was. (sigh)

#3 @greenshady
6 years ago

  • Cc justin@… added

#4 @mdbitz
5 years ago

Hi, the p tag is considered a blocking element. In this case the output from wpautop should be:

<div>
<p>Hello world</p>
<p><span>WordPress</span></p>
</div>

I'm in the process of writing a comprehensive set of unit tests on the wpautop function to verify its current functionality and allow proper regression testing as new enhancements are requested. I've added the above as a test case to confirm that the second p tag is properly closed.

It might be another week till it gets put up for review as there are a good amount of tickets to go through.

#5 @nacin
4 years ago

  • Keywords wpautop added

#6 @nacin
4 years ago

  • Keywords needs-unit-tests added
  • Milestone changed from Awaiting Review to Future Release

mdbitz's ticket with tests ended up being #25856.

Fine with trying to establish a fix for this. Let's make sure we have strong test coverage though.

@jond
4 years ago

Autop.php unit test coverage for #20444

#7 @jond
4 years ago

Submitted unit test for unbroken lines based on the report by @niallkennedy

@jond
4 years ago

Updated expected results

#8 @jond
4 years ago

  • Keywords has-patch added; needs-unit-tests removed

This issue has unit tests and a patch is available as part of the comprehensive patch on related issue #27350.

#9 @chriscct7
3 years ago

  • Keywords needs-refresh added
Note: See TracTickets for help on using tickets.