Skip to content

fix(html/formatter): preserve closing > before self-closing elements#9333

Open
terror wants to merge 1 commit intobiomejs:mainfrom
terror:formatter-mangles-close-bracket
Open

fix(html/formatter): preserve closing > before self-closing elements#9333
terror wants to merge 1 commit intobiomejs:mainfrom
terror:formatter-mangles-close-bracket

Conversation

@terror
Copy link

@terror terror commented Mar 4, 2026

Summary

Fixes #9310.

When an inline element like <code> is immediately followed by a self-closing element like <br>, the
formatter's token borrowing pattern would steal the closing > from </code> to pass to the next
sibling. But self-closing elements (<br>, <img>, etc.) can't accept borrowed tokens in format_element_with_borrowing, so the > was silently dropped:

- <code>cubing.js</code<br>
+ <code>cubing.js</code><br>

The fix excludes self-closing elements from the borrowing pattern by only setting
next_is_adjacent_inline when the next element is an HtmlElement (which has opening/closing tags).

Test Plan

  • Added inline-before-self-closing.html snapshot test covering <code>, <span>, <b>, and <a>
    followed by <br> and <img>.
  • All existing HTML formatter tests pass (125 spec + 143 Prettier).

Docs

No docs changes needed.

@changeset-bot
Copy link

changeset-bot bot commented Mar 4, 2026

🦋 Changeset detected

Latest commit: ab2948e

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

This PR includes changesets to release 13 packages
Name Type
@biomejs/biome Patch
@biomejs/cli-win32-x64 Patch
@biomejs/cli-win32-arm64 Patch
@biomejs/cli-darwin-x64 Patch
@biomejs/cli-darwin-arm64 Patch
@biomejs/cli-linux-x64 Patch
@biomejs/cli-linux-arm64 Patch
@biomejs/cli-linux-x64-musl Patch
@biomejs/cli-linux-arm64-musl Patch
@biomejs/wasm-web Patch
@biomejs/wasm-bundler Patch
@biomejs/wasm-nodejs Patch
@biomejs/backend-jsonrpc Patch

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

@github-actions github-actions bot added A-Formatter Area: formatter L-HTML Language: HTML and super languages labels Mar 4, 2026
Exclude self-closing elements from the token borrowing pattern by only
setting next_is_adjacent_inline when the next element is an HtmlElement.
@terror terror force-pushed the formatter-mangles-close-bracket branch from eb58e58 to ab2948e Compare March 4, 2026 23:10
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 4, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: de638b4e-4c8d-46f5-84ea-7a9a6a04e1c4

📥 Commits

Reviewing files that changed from the base of the PR and between eb58e58 and ab2948e.

⛔ Files ignored due to path filters (1)
  • crates/biome_html_formatter/tests/specs/html/elements/inline/inline-before-self-closing.html.snap is excluded by !**/*.snap and included by **
📒 Files selected for processing (3)
  • .changeset/sour-ducks-flash.md
  • crates/biome_html_formatter/src/html/lists/element_list.rs
  • crates/biome_html_formatter/tests/specs/html/elements/inline/inline-before-self-closing.html
🚧 Files skipped from review as they are similar to previous changes (2)
  • crates/biome_html_formatter/src/html/lists/element_list.rs
  • crates/biome_html_formatter/tests/specs/html/elements/inline/inline-before-self-closing.html

Walkthrough

Introduces a stricter borrowing check next_can_borrow for inline HTML elements: the previous next_is_adjacent_inline is replaced and borrowing of the prior element’s closing angle is allowed only when the next sibling is a non-text HtmlElement and both elements are externally whitespace-sensitive. Adjusts when borrowed_sibling_r_angle is stored and passes next_can_borrow into the element formatter to prevent mangling cases like <code>cubing.js</code><br>.

Possibly related PRs

Suggested reviewers

  • dyc3
  • ematipico
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main fix: preserving the closing > token when inline elements precede self-closing elements in HTML formatting.
Description check ✅ Passed The description thoroughly explains the issue, root cause, and solution, directly addressing the formatting bug and including test coverage details.
Linked Issues check ✅ Passed The pull request successfully addresses issue #9310 by preventing token borrowing from closing tags when the next element is self-closing, validated by the added snapshot test.
Out of Scope Changes check ✅ Passed All changes are tightly scoped to fixing the HTML formatter's token-borrowing logic for inline-to-self-closing element transitions, with no unrelated modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Formatter Area: formatter L-HTML Language: HTML and super languages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 HTML formatter mangles closing bracket for <code> followed by <br>

1 participant