Opened 10 years ago
Last modified 5 weeks ago
#40676 new defect (bug)
wpautop adds opening & closing p tags around the opening a tag and around the closing a tag when the link contains certain flow content elements like div, h1, h2...
| Reported by: | diegocanal | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Formatting | Version: | 4.8 |
| Severity: | normal | Keywords: | 2nd-opinion has-patch has-unit-tests |
| Cc: | Focuses: |
Description (last modified by )
Hi,
Description
wpautop leaves <a href=""> (opening tag of the link) in between <p></p> tags and </a> (closing tag of the link) in between <p></p> tags when the link contains certain flow content elements like div, h1, h2...
Example 1
If I add this to the HTML editor:
<a href="https://example.com/"><div>DIV inside link</div></a>
The output source code is:
<p><a href="https://example.com/"></p> <div>DIV inside link</div> <p></a></p>
Example 2
If I add this to the HTML editor:
<a href="https://example.com/"><h1>H1 inside link</h1></a>
The output source code is:
<p><a href="https://example.com/"></p> <h1>H1 inside link</h1> <p></a></p>
Note 1
I would like to point out that html flow content elements such as <div> or headings (<h1>, <h2>, <h3> ,...) belong to the category of permitted content for the <a> element. References:
WHATWG HTML Living Standard |The definition of a
HTML5 | The definition of a
MDN | The definition of a.
Note 2
This issue might be related to ticket #34722
Attachments (1)
Change History (21)
This ticket was mentioned in Slack in #core by diegocanal. View the logs.
9 years ago
#4
follow-up:
↓ 6
@
9 years ago
As a general HTML rule (regardless of whether it's WordPress or not), block level elements should never be wrapped inside inline elements. So a div or h1 should never be placed inside an a tag. I imagine that is why wpautop handles things in this way, as you are going against the general HTML standards that are accepted all over the web.
I'm not on the core team and don't have the requisite knowledge for patching this, but I don't think it really warrants a patch as this is not a bug, but rather an HTML standard that is being adhered to.
#6
in reply to: ↑ 4
@
9 years ago
Replying to hlashbrooke:
As a general HTML rule (regardless of whether it's WordPress or not), block level elements should never be wrapped inside inline elements. So a
divorh1should never be placed inside anatag. I imagine that is whywpautophandles things in this way, as you are going against the general HTML standards that are accepted all over the web.
I'm not on the core team and don't have the requisite knowledge for patching this, but I don't think it really warrants a patch as this is not a bug, but rather an HTML standard that is being adhered to.
@hlashbrooke, have you fully read my initial comment (specially "Note 1")? After reading "Note 1", do you still need more arguments to make you realise that div and h1 can be perfectly placed inside an a element? I can give you more.
I provided arguments –referencing 3 authoritative sources– to support my statement, you haven't done the same to support yours. Could you please give us something more than "because I say so"?
#7
follow-up:
↓ 8
@
9 years ago
I didn't realise this was an argument :)
It seems you are probably correct, but parsing all of that content about HTML standards is pretty daunting to be honest, so I'm not going to go through the task of interpreting it all. I merely commented on here to weigh in on it a bit, but I'm not that invested in the outcome.
I was just pointing out that it isn't generally accepted in HTML practice, even if it is permissible. I'll leave this to more experienced minds to work out from here :)
#8
in reply to: ↑ 7
@
9 years ago
Replying to hlashbrooke:
I was just pointing out that it isn't generally accepted in HTML practice, even if it is permissible.
I couldn't disagree more with you. FYI, nesting block elements such as div and headings (h1, h2, etc) inside the a element (links) has been widely used since 2009. Here you have some examples I found in very well known sites:
#9
follow-up:
↓ 10
@
9 years ago
Well then - I'll let someone else with more authority weigh in here - I was just trying to be helpful :)
#10
in reply to: ↑ 9
@
9 years ago
Replying to hlashbrooke:
Well then - I'll let someone else with more authority weigh in here - I was just trying to be helpful :)
Thank you @hlashbrooke! :)
#11
@
9 years ago
I can confirm that is has been a recurring issue for me as well, and for quite some time. I have no knowledge of how to fix the parsing issue, but it is certainly there and would, in my opinion, warrant a patch.
This ticket was mentioned in Slack in #core by diegocanal. View the logs.
9 years ago
#15
follow-up:
↓ 16
@
9 years ago
- Keywords 2nd-opinion added
- Version 4.7.4
Worth noting
<a href="https://example.com/"><div>DIV inside link</div></a>
is valid in HTML5. However, WordPress shouldn't assume all the themes use HTML5 as this would be invalid in HTML 4.0.1.
#16
in reply to: ↑ 15
@
9 years ago
Replying to afercia:
Worth noting
<a href="https://example.com/"><div>DIV inside link</div></a>
is valid in HTML5. However, WordPress shouldn't assume all the themes use HTML5 as this would be invalid in HTML 4.0.1.
You are right pointing out that this kind of structures would not be valid in HTML 4.0.1 but...even if a developer made a mistake using invalid code (in HTML 4.0.1), how would it help having wpautop messing up the code? The way wpautop works now, it outputs invalid code for both standards, HTML5 and HTML 4.0.1. Sorry, I think I'm missing the point.
#17
@
9 years ago
@diegocanal bolding text is not necessary :) I'm not saying this shouldn't be addressed. Just that it's not so simple as it seems at first sight.
This ticket was mentioned in PR #14212 on WordPress/wordpress-develop by @irozum.
5 weeks ago
#19
- Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed
Summary
When a link wraps block-level elements (for example <a href="..."><div>...</div></a>), wpautop() leaves the opening <a> and the closing </a> each wrapped in their own <p>, producing invalid markup such as <p><a href="..."></p> and <p></a></p>.
This adds a late pass in wpautop(), after line-break handling, that unwraps a <p> containing only an opening <a> tag when a block element follows, and a <p> containing only </a> when a block closing tag precedes it. Links around inline content are unaffected.
Tests
Adds a data-provided test covering div, h1, and whitespace-separated cases. It fails on trunk and passes with the change, and the full Tests_Formatting group still passes.
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5.5
Used for: Implementation, tests, and PR description. Reviewed by Igor Rozum.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.
#20
@
5 weeks ago
Patch Testing Report
Patch Tested: https://github.com/WordPress/wordpress-develop/pull/14212 (head 6e2d1eb)
Environment
- WordPress: 7.2-alpha-63166-src
- PHP: 8.3.35
- Server: wordpress-develop local Docker environment, PHPUnit 9.6.38
- Database: MySQL 9.7.2
- Browser: N/A (unit tests and WP-CLI)
- OS: Windows 11
- Theme: N/A
- MU Plugins: None activated
- Plugins: None activated
Actual Results
- ✅ The examples from the ticket (
divandh1inside a link) are no longer wrapped in<p>tags. - ✅
--group 40676: 3 tests pass on single site and multisite; all 3 fail with trunk'sformatting.php. - ✅
--group formatting: 2136 tests, no failures.
Additional Notes
I ran some extra inputs through wpautop() on trunk and with the PR:
- Fixed: a link without attributes, two block links in a row,
<figure>and<p>inside a link, and a block link after a paragraph. - Unchanged, as expected: inline links (
Some <a href="/">inline</a> text.) and other inline elements such as<abbr><div>x</div></abbr>. - Only partly fixed: text between the link and the block element.
<a href="/">Intro\n<div>x</div></a>still gives<p><a href="/">Intro</p>; only the closing</a>is unwrapped.<a href="/"><div>x</div>\nmore</a>still gives<p>more</a></p>; only the opening tag is unwrapped. These cases are outside the ticket's examples; mentioning them in case a test or a note on scope is wanted.
- AI assistance: Claude Code (Claude Opus 5.5) was used to run the tests and the extra inputs; I reviewed the results.
Supplemental Artifacts
None
screenshot-wpautop-wp-bug