Skip to content

fix(html/codegen): keep </p> for span-parent paragraphs#11756

Merged
kdy1 merged 2 commits intomainfrom
kdy1/fix-html-11748
Mar 31, 2026
Merged

fix(html/codegen): keep </p> for span-parent paragraphs#11756
kdy1 merged 2 commits intomainfrom
kdy1/fix-html-11748

Conversation

@kdy1
Copy link
Copy Markdown
Member

@kdy1 kdy1 commented Mar 31, 2026

Summary

  • avoid omitting </p> when the parent is <span> during HTML tag omission
  • add regression fixture for swc_html_codegen (issue-11748)
  • add recovery fixture for swc_html_minifier (issue-11748)

Verification

  • cargo test -p swc_html_codegen --test fixture -- --ignored issue_11748
  • cargo test -p swc_html_minifier --test fixture -- --ignored issue_11748
  • cargo test -p swc_html_codegen
  • cargo test -p swc_html_minifier
  • cargo fmt --all
  • cargo clippy --all --all-targets -- -D warnings

Closes #11748

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Mar 31, 2026

🦋 Changeset detected

Latest commit: e6fd8ab

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@kdy1 kdy1 marked this pull request as ready for review March 31, 2026 07:39
@kdy1 kdy1 requested review from a team as code owners March 31, 2026 07:39
Copilot AI review requested due to automatic review settings March 31, 2026 07:39
@kdy1 kdy1 changed the title fix(html): keep </p> for span-parent paragraphs fix(html/codegen): keep </p> for span-parent paragraphs Mar 31, 2026
@kdy1 kdy1 enabled auto-merge (squash) March 31, 2026 07:39
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Mar 31, 2026

Merging this PR will not alter performance

✅ 219 untouched benchmarks


Comparing kdy1/fix-html-11748 (e6fd8ab) with main (f21d336)

Open in CodSpeed

@claude
Copy link
Copy Markdown
Contributor

claude Bot commented Mar 31, 2026

PR Review: fix(html/codegen): keep

for span-parent paragraphs

Summary

This PR fixes #11748 by adding "span" to the exclusion list for </p> tag omission. When <span> is the parent of a <p>, the closing </p> tag is now preserved, preventing the minifier from changing how browsers parse the (invalid but common) markup <span><p>TEXT</p></span>.

Code Quality

The fix is clean, minimal, and placed correctly. The comment explaining why span is excluded (with issue reference) is helpful. The alphabetical ordering of the match arms is maintained. Good.

Potential Issue: Incomplete Coverage

The HTML spec only lists a, audio, del, ins, map, noscript, and video as exclusions. The codebase already extends this with other phrasing/inline elements (acronym, big, font, tt, strike, kbd, rbc) to handle invalid markup gracefully. Adding span follows this pattern, but many other phrasing content elements have the same problem:

  • abbr, b, bdi, bdo, cite, code, data, dfn, em, i, mark, q, s, samp, small, strong, sub, sup, time, u, var, label, button, output, meter, progress, ruby, slot

Any of these as a parent of <p> would trigger the same browser-reinterpretation issue. This fix addresses the most commonly reported case (<span>, especially via MDX), but a more robust approach might be to invert the logic: instead of maintaining a denylist of elements where </p> omission is unsafe, only allow omission for elements where nesting <p> is valid per the spec (i.e., flow content containers). That would be future-proof against similar reports for <em><p>...</p></em>, <strong><p>...</p></strong>, etc.

Not a blocker for this PR — it's a reasonable targeted fix — but worth considering as a follow-up.

Test Coverage

Good. Both swc_html_codegen and swc_html_minifier have fixture tests covering the exact reported case. The test inputs/outputs correctly verify round-trip preservation of </p> inside <span>.

Minor Nits

  • output.html and output.min.html are missing a trailing newline (\ No newline at end of file). This is cosmetic but inconsistent if other fixture files have trailing newlines.

Verdict

LGTM for merge as a targeted fix. Consider a follow-up to generalize the exclusion to all phrasing content elements.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes HTML tag-omission behavior so </p> is not omitted when a <p> is the last child of a <span>, preserving browser parsing stability for invalid-but-common markup patterns (e.g. MDX output), as reported in #11748.

Changes:

  • Update swc_html_codegen tag-omission rules to keep </p> when the parent element is span.
  • Add regression fixtures for swc_html_codegen and recovery fixtures for swc_html_minifier covering issue-11748.
  • Add a changeset entry for publishing/version bump.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

File Description
crates/swc_html_codegen/src/lib.rs Prevent optional omission of </p> when the parent is <span> to avoid changing how invalid HTML is parsed.
crates/swc_html_codegen/tests/fixture/issue-11748/* Regression fixture asserting minified output retains </p> under <span>.
crates/swc_html_minifier/tests/recovery/element/issue-11748/* Recovery-mode fixture ensuring minifier output keeps </p> for the invalid nesting case.
.changeset/neat-worms-vanish.md Declares release/version bump intent for this fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -0,0 +1,6 @@
---
swc_core: patch
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changeset includes swc_core: patch, but this PR doesn’t modify swc_core. This would trigger a swc_core version bump/release with no functional changes. Consider removing swc_core from the changeset (and listing only the affected crate(s), e.g. swc_html_codegen and any other crate you intend to publish for this fix).

Suggested change
swc_core: patch

Copilot uses AI. Check for mistakes.
@github-actions
Copy link
Copy Markdown
Contributor

Binary Sizes

File Size
swc.linux-x64-gnu.node 28M (29081800 bytes)

Commit: 3c5484a

@kdy1 kdy1 merged commit ede9950 into main Mar 31, 2026
57 checks passed
@kdy1 kdy1 deleted the kdy1/fix-html-11748 branch March 31, 2026 08:05
@github-actions github-actions Bot modified the milestones: Planned, 1.15.24 Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

@swc/html wrongly minifies invalid HTML markup

2 participants