Opened 20 months ago
Last modified 4 weeks ago
#63148 new defect (bug)
wpautop() formatting function generate invalid HTML if the text end with an empty <p> tag
| Reported by: | gillesdoge | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Formatting | Version: | |
| Severity: | normal | Keywords: | has-test-info has-patch has-unit-tests changes-requested |
| Cc: | Focuses: |
Description
It's probably my first ticket (hi there 👋 !).
When preparing a migration of an old WordPress, I was facing the following issue:
<?php $text = "<p>test</p> <p></p>"; var_dump(wpautop($text); // Output: string(16) "<p>test</p></p> // "
You may ask, but why do you need to apply wpautop() on an already formatted text?
Because some of the content needed it (paragraphs only separated by newlines) and some was already formatted with HTML and Classic Editor.
My current countermeasure is to test if the content already have <p> and don't apply wpautop().
Is this a know issue (I found nothing when searching) ?
Attachments (1)
Change History (13)
#1
@
20 months ago
- Keywords needs-patch has-testing-info added
This ticket was mentioned in PR #8569 on WordPress/wordpress-develop by @sukhendu2002.
20 months ago
#2
- Keywords has-patch added; needs-patch removed
Trac ticket: https://core.trac.wordpress.org/ticket/63148
#4
@
12 months ago
- Keywords needs-unit-tests added
- Version trunk
I'm reviewing this ticket in preparation for a 6.9 dry run.
I'm removing the version field, since at least this issue isn't an issue that occurred in an unreleased development branch.
Additionally, unit tests are needed to move this ticket forward.
#7
@
5 weeks ago
The nine added lines in PR #8569 resolve the reported trailing empty paragraph, but leave a paragraph unclosed when the final paragraph contains only a br tag.
Input to wpautop(): "<p>a</p>\n<p><br></p>"
Trunk output: "<p>a</p>\n<p></p>\n"
With the PR change: "<p>a</p>\n<p>\n"
Here \n represents a newline.
The attached test-only patch adds five cases covering the reported input, a whitespace-only final paragraph, a br-only paragraph, nonempty paragraphs, and inline markup.
The comparison uses trunk df8a00e448825d0ba80374e75df83ae894403264, first with the tests alone, then with the nine-line addition from PR #8569 at 9bedef3956b4a21ab3454ff2aff68af0da258070. The relevant source and test files are unchanged on trunk 10d39401bf9e382a8e438f38c672fe34c458bdaa, where the test patch also applies cleanly.
The full wpAutop test class was run on PHP 8.3.35 with PHPUnit 9.6.38. Composer dependencies were installed and known-bug tests were forced on in wp-tests-config.php. The same command was run from the repository root in both cases:
php ./vendor/phpunit/phpunit/phpunit -c phpunit.xml.dist tests/phpunit/tests/formatting/wpAutop.php --no-coverage
Both runs executed 30 tests and 39 assertions. Trunk has two failures, for the reported input and whitespace-only variant. With the PR addition, those pass and the br-only case fails. The nonempty-paragraph and inline-markup controls pass in both runs.
Could we include these cases when revising the fix?
This ticket was mentioned in PR #14464 on WordPress/wordpress-develop by @faisalahammad.
4 weeks ago
#8
- Keywords has-unit-tests added; needs-unit-tests removed
This PR adds unit tests for the needs-unit-tests keyword on the ticket. It is built on top of #8569 by @Sukhendu2002, so the first two commits are from that PR and the last commit adds the tests. Props to @Sukhendu2002 for the fix.
The new test test_wpautop_removes_empty_paragraphs() in tests/phpunit/tests/formatting/wpAutop.php uses a data provider with 9 cases: empty paragraphs at the end, in the middle and on their own, whitespace-only and newline-only paragraphs, plus two cases that must stay unchanged ( and <pre>).
With the patch from #8569, 7 of 9 cases pass. Two still fail when the empty paragraph is the only content:
| Input | Output with #8569 | Expected |
|---|---|---|
<p></p> | "</p>\n" | ''
|
"<p>\n</p>" | "</p>\n" | ''
|
Without any patch, 7 of 9 cases fail.
The input <p></p> becomes its own block and gets wrapped as <p><p></p></p>. The existing empty P cleanup removes only the inner pair, then the "strip P before a block tag" rule turns what is left into a stray </p>. A smaller alternative is one line right after the wrap loop, before the existing empty P cleanup:
$text = preg_replace( '|<p>\s*<p>\s*</p>\s*</p>\n|', '', $text );
With that line in place of the three new regexes, all 9 cases and the rest of the wpautop() tests pass. Details are in the Trac ticket.
Testing: vendor/bin/phpunit --filter Tests_Formatting_wpAutop. PHPCS is clean on the test file.
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Drafting the test cases and tracing the cause of the two failing cases. I reviewed the tests, ran them against both patches and take responsibility for them.
#9
@
4 weeks ago
- Keywords changes-requested added
I've added unit tests for PR 8569 as a data provider in tests/phpunit/tests/formatting/wpAutop.php (9 cases): https://github.com/WordPress/wordpress-develop/pull/14464
7 of 9 pass with the patch. Two still fail when the empty paragraph is the only content:
wpautop( '<p></p>' )returns"</p>\n", expected''wpautop( "<p>\n</p>" )returns"</p>\n", expected''
The cause: the input <p></p> becomes its own block and gets wrapped as <p><p></p></p>. The empty paragraph cleanup removes only the inner pair. Then the "strip P before a block tag" rule turns what is left into a stray </p>.
A smaller alternative is to remove the double wrapped empty paragraph right after the wrap loop, before the existing empty P cleanup:
$text = preg_replace( '|<p>\s*<p>\s*</p>\s*</p>\n|', '', $text );
With that one line in place of the three new regexes, all 9 cases pass, along with the rest of the wpautop tests. It also avoids adding global cleanup passes that run on all content.
#10
@
4 weeks ago
Update: I changed PR 14464 to use the one line fix in place of the three regexes from PR 8569: https://github.com/WordPress/wordpress-develop/pull/14464
The PR now has the fix right after the wrap loop in wpautop(), plus the 9 test cases:
$text = preg_replace( '|<p>\s*<p>\s*</p>\s*</p>\n|', '', $text );
All 9 new cases pass now, including the two that failed before (<p></p> and "<p>\n</p>" on their own). The rest of the wpautop() tests also pass.
Props to @sukhendu2002 for the first patch.
@rejaulalomkhan commented on PR #14464:
4 weeks ago
#11
Thanks for working on this fix and tests, @faisalahammad! Tested this locally against trunk and verified that the double-wrapped empty paragraph cleanup resolves the stray </p> tag cleanly.
Patch Testing Report
Patch tested: https://github.com/WordPress/wordpress-develop/pull/14464.diff
Environment
- WordPress: 7.2-alpha-63166-src
- PHP: 8.2.29
- Server: PHP CLI
- Database: MySQL 8.0.30
- Browser: N/A (unit tests)
- OS: Windows 10
Steps taken
- Switched to
upstream/trunkand ranvendor/bin/phpunit -c phpunit.xml.dist --filter test_wpautop_removes_empty_paragraphs. 7 of 9 dataset test cases failed due to stray</p>tags (e.g.<p>test</p>\n<p></p>returned<p>test</p></p>\n,<p></p>returned</p>\n). - Applied the patch from PR #14464 and re-ran
vendor/bin/phpunit -c phpunit.xml.dist --filter test_wpautop_removes_empty_paragraphs. - Verified all 9 dataset test cases pass cleanly (single site and multisite).
- Ran the entire
Tests_Formatting_wpAutoptest suite (34 tests, 43 assertions) in normal and reverse order (--order-by=reverse). All passed. - Ran PHPCS on
src/wp-includes/formatting.phpandtests/phpunit/tests/formatting/wpAutop.php: 0 errors, 0 warnings. - ✅ Patch is solving the problem: removes stray closing
</p>tags when content contains empty paragraph tags, while properly preserving legitimate content ( ,<pre><p></p></pre>).
Expected result
- Passing content with empty paragraph tags like
<p></p>,<p>\n</p>, or<p>test</p>\n<p></p>towpautop()should cleanly remove the empty paragraph without leaving orphaned</p>closing tags.
Additional Notes
- Tested both single-site and multisite suites.
- AI assistance: Antigravity with Gemini 2.5 Pro was used to run the test suite and verify failures; results reviewed and verified by me.
@wildworks commented on PR #8569:
4 weeks ago
#12
Closing this in favor of #14464
Reproduction Report
Description
This report validates that the issue can be reproduced.
Environment
Actual Results
Supplemental Artifacts