fix(prettier-plugin-liquid): preserve U+00A0 and don't drop borrowed closing tags#1191
Open
BJvdA wants to merge 2 commits intoShopify:mainfrom
Open
fix(prettier-plugin-liquid): preserve U+00A0 and don't drop borrowed closing tags#1191BJvdA wants to merge 2 commits intoShopify:mainfrom
BJvdA wants to merge 2 commits intoShopify:mainfrom
Conversation
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.
What are you adding in this PR?
Fix a
@shopify/prettier-plugin-liquidbug where formatting an HTML text node containing only a non-breaking space (U+00A0) inside borrowed-closing-tag contexts produced broken output:Input:
Previous output (incorrect — both the NBSP and the
</em>are lost):Expected output (preserves the NBSP and the
</em>):Root cause
printTextNodeshort-circuited whitespace-only text withreturn ''. Two problems:\smatches U+00A0, but in HTML a non-breaking space is a meaningful character and must not be collapsed away.return ''skippedprintOpeningTagPrefix/printClosingTagSuffix, which is exactly where tag markers borrowed from the parent (seeneedsToBorrowParentClosingTagStartMarker) are emitted. When that runs on a whitespace-only text node, the borrowed</em>marker gets silently dropped.Fix (
packages/prettier-plugin-liquid/src/printer/printer-liquid-html.ts)[ \t\n\r\f]instead of\sfor the short-circuit, the trim, and the word split — so U+00A0 (and other non-collapsible Unicode "whitespace") flows through as a regular character.printOpeningTagPrefix/printClosingTagSuffixso any borrowed open/close markers survive.Adds a regression case to the
unit-paragraphsprinter fixture covering<p><strong><em>\u00A0</em></strong></p>.What's next? Any followup issues?
None.
What did you learn?
\sin JavaScript matches U+00A0 — which is fine for "is this byte whitespace-ish", but wrong any time you actually care about HTML semantics, where NBSP is non-collapsible content. Whenever a printer special-cases "whitespace-only" nodes, the early-return path also has to honor whatever work the surrounding tag-borrowing logic relies on.Tophatting
Repro / verification:
yarn workspace @shopify/prettier-plugin-liquid testThe new fixture under
unit-paragraphs(it should preserve non-breaking spaces (U+00A0) as text and not drop borrowed closing tags) covers the regression.Before you deploy
changeset(.changeset/preserve-nbsp-html-text.md)