Preserve HTML entity-encoded whitespace in JSX text trimming#17798
Open
veeceey wants to merge 2 commits intobabel:mainfrom
Open
Preserve HTML entity-encoded whitespace in JSX text trimming#17798veeceey wants to merge 2 commits intobabel:mainfrom
veeceey wants to merge 2 commits intobabel:mainfrom
Conversation
cleanJSXElementLiteralChild was stripping whitespace characters that were intentionally encoded as HTML entities (e.g. &babel#32;, &babel#9;, &babel#10;). This happened because entities were decoded before the whitespace trimming algorithm ran, making entity-encoded spaces indistinguishable from literal whitespace. The fix uses the raw source text (child.extra.raw) for trimming, where entity syntax like &babel#32; is treated as non-whitespace content. After trimming, entities are decoded to produce the final string value. This matches the behavior of esbuild and tsc, which both preserve entity-encoded whitespace. Fixes babel#17683
Collaborator
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/60943 |
|
commit: |
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.
Problem
When JSX text contains HTML entity-encoded whitespace characters like
 (space),	(tab), or (newline), they get stripped during the JSX text whitespace trimming step. This happens becausecleanJSXElementLiteralChildoperates onchild.value, which has entities already decoded by the parser, making entity-encoded spaces indistinguishable from literal whitespace.For example:
Produces
children: ["foo", <hr />, "bar"]- the space is completely lost.Both esbuild and tsc correctly preserve these entity-encoded whitespace characters.
Fix
Modified
cleanJSXElementLiteralChildto usechild.extra.raw(the raw source text with entities still encoded) for whitespace trimming decisions. In the raw text, is treated as non-whitespace content (because it's the literal characters&,#,3,2,;). After trimming, the entities are decoded to produce the final string value.A
decodeHTMLEntitiesfunction handles all three entity forms: numeric decimal ( ), numeric hex ( ), and named (&), using the same XHTML entity table as the parser.Test plan
handling is unchanged (it was never affected since U+00A0 != U+0020)Fixes #17683