fix: [#2052] Correct caption element content model to allow flow content#2058
Merged
capricorn86 merged 1 commit intocapricorn86:masterfrom Feb 9, 2026
Merged
Conversation
22e636a to
614b105
Compare
…w flow content Fixes capricorn86#2052 ## Problem The caption element was incorrectly configured with contentModel: textOrComments, which only allowed text nodes and comments. Per the HTML spec, caption elements should contain flow content (inline and block elements), except table elements. This caused elements like <b>, <em>, <span>, etc. to be incorrectly moved outside the caption during parsing. ## Changes ### packages/happy-dom/src/config/HTMLElementConfig.ts Updated caption element configuration to match the HTML spec and follow the same pattern as td/th elements: - Changed contentModel from textOrComments to noForbiddenFirstLevelDescendants - Added forbiddenDescendants: table structure elements (table, tbody, thead, tfoot, tr, td, th, col, colgroup) - Added permittedParents: ['table'] to ensure caption only appears in tables ### packages/happy-dom/test/html-parser/HTMLParser.malformedHTML.test.ts Added comprehensive test coverage for caption element content model: - Inline elements preservation (b, strong, em, span, a) - Nested inline elements - Block-level elements (p, div) - Table element prohibition - Content serialization - permittedParents validation (wrong parent, standalone, correct parent) ## Implementation Details This fix follows the same pattern as PR capricorn86#2007 for paragraph elements, using the existing noForbiddenFirstLevelDescendants content model. While this doesn't recursively check deeply nested table elements, it matches the existing codebase patterns and handles the vast majority of real-world cases.
e9ef366 to
639c2d8
Compare
Contributor
Author
|
Was this done an LLM? Who on earth writes such pull request descriptions? |
Contributor
Author
|
I did use an LLM as a writing aid and reviewed the content myself. Please let me know if you have any feedback on the actual changes👋 |
capricorn86
approved these changes
Feb 9, 2026
Owner
capricorn86
left a comment
There was a problem hiding this comment.
Thank you for your contribution @atzzCokeK! :star
RAprogramm
pushed a commit
to RAprogramm/fork-happy-dom
that referenced
this pull request
Feb 20, 2026
…w flow content (capricorn86#2058) Fixes capricorn86#2052 ## Problem The caption element was incorrectly configured with contentModel: textOrComments, which only allowed text nodes and comments. Per the HTML spec, caption elements should contain flow content (inline and block elements), except table elements. This caused elements like <b>, <em>, <span>, etc. to be incorrectly moved outside the caption during parsing. ## Changes ### packages/happy-dom/src/config/HTMLElementConfig.ts Updated caption element configuration to match the HTML spec and follow the same pattern as td/th elements: - Changed contentModel from textOrComments to noForbiddenFirstLevelDescendants - Added forbiddenDescendants: table structure elements (table, tbody, thead, tfoot, tr, td, th, col, colgroup) - Added permittedParents: ['table'] to ensure caption only appears in tables ### packages/happy-dom/test/html-parser/HTMLParser.malformedHTML.test.ts Added comprehensive test coverage for caption element content model: - Inline elements preservation (b, strong, em, span, a) - Nested inline elements - Block-level elements (p, div) - Table element prohibition - Content serialization - permittedParents validation (wrong parent, standalone, correct parent) ## Implementation Details This fix follows the same pattern as PR capricorn86#2007 for paragraph elements, using the existing noForbiddenFirstLevelDescendants content model. While this doesn't recursively check deeply nested table elements, it matches the existing codebase patterns and handles the vast majority of real-world cases.
RAprogramm
pushed a commit
to RAprogramm/fork-happy-dom
that referenced
this pull request
Feb 20, 2026
…w flow content (capricorn86#2058) Fixes capricorn86#2052 ## Problem The caption element was incorrectly configured with contentModel: textOrComments, which only allowed text nodes and comments. Per the HTML spec, caption elements should contain flow content (inline and block elements), except table elements. This caused elements like <b>, <em>, <span>, etc. to be incorrectly moved outside the caption during parsing. ## Changes ### packages/happy-dom/src/config/HTMLElementConfig.ts Updated caption element configuration to match the HTML spec and follow the same pattern as td/th elements: - Changed contentModel from textOrComments to noForbiddenFirstLevelDescendants - Added forbiddenDescendants: table structure elements (table, tbody, thead, tfoot, tr, td, th, col, colgroup) - Added permittedParents: ['table'] to ensure caption only appears in tables ### packages/happy-dom/test/html-parser/HTMLParser.malformedHTML.test.ts Added comprehensive test coverage for caption element content model: - Inline elements preservation (b, strong, em, span, a) - Nested inline elements - Block-level elements (p, div) - Table element prohibition - Content serialization - permittedParents validation (wrong parent, standalone, correct parent) ## Implementation Details This fix follows the same pattern as PR capricorn86#2007 for paragraph elements, using the existing noForbiddenFirstLevelDescendants content model. While this doesn't recursively check deeply nested table elements, it matches the existing codebase patterns and handles the vast majority of real-world cases.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2052
Problem
The
<caption>element was incorrectly configured withcontentModel: textOrComments, which only allowed text nodes and comments as children. According to the HTML specification, caption elements should contain flow content (including inline and block elements), except table elements.This caused inline elements like
<b>,<em>,<span>, etc. to be incorrectly moved outside the caption during HTML parsing, resulting in broken DOM structures.Reproduction
Changes
packages/happy-dom/src/config/HTMLElementConfig.tsUpdated the
captionelement configuration to follow the HTML spec and match the pattern used by other table elements (td,th):Changes explained:
contentModel: Changed fromtextOrCommentstonoForbiddenFirstLevelDescendantsto allow flow contentforbiddenDescendants: Added table structure elements (per HTML spec: "Flow content, but with no descendant table elements")permittedParents: Added['table']to ensure caption only appears as a child of table elementspackages/happy-dom/test/html-parser/HTMLParser.malformedHTML.test.tsAdded comprehensive test coverage (9 new tests) for the caption element content model:
<b>,<strong>,<em>,<span>,<a>are correctly preserved inside caption<small><b>text</b></small>work correctly<p>and<div>are allowed (flow content)<table>elements inside caption are correctly moved outside<table>or when used standaloneImplementation Details
This implementation follows the same pattern as PR #2007 (paragraph elements), using the existing
noForbiddenFirstLevelDescendantscontent model. While this approach doesn't recursively check for deeply nested table elements (e.g.,<caption><div><table>...</table></div></caption>), it:forbiddenDescendantsandpermittedParentsTest Coverage
All tests pass:
Full test suite: