Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/preserve-nbsp-html-text.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/prettier-plugin-liquid': patch
---

Preserve non-breaking spaces (U+00A0) in HTML text nodes and stop dropping borrowed closing tag markers when a text node is pure whitespace.
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,27 @@ function printTextNode(

if (isYamlFrontMatter(node)) return node.value;

if (node.value.match(/^\s*$/)) return '';
// Only ASCII/HTML whitespace is collapsible. Do not treat U+00A0 (NBSP) or
// other unicode "whitespace" as collapsible, since they are meaningful
// characters in the rendered output.
const HTML_WHITESPACE_RUN = /[ \t\n\r\f]+/g;
const HTML_WHITESPACE_TRIM = /^[ \t\n\r\f]+|[ \t\n\r\f]+$/g;

// If the text is purely collapsible whitespace we still need to emit the
// open/close prefixes so that any tag markers borrowed from the parent
// aren't lost (see needsToBorrowParentClosingTagStartMarker).
if (node.value.replace(HTML_WHITESPACE_RUN, '') === '') {
return [printOpeningTagPrefix(node, options), printClosingTagSuffix(node, options)];
}

const text = node.value;

const paragraphs = text
.split(/(\r?\n){2,}/)
.filter(Boolean) // removes empty paragraphs (trailingWhitespace)
.map((curr) => {
let doc = [];
const words = curr.trim().split(/\s+/g);
const words = curr.replace(HTML_WHITESPACE_TRIM, '').split(HTML_WHITESPACE_RUN);
let isFirst = true;
for (let j = 0; j < words.length; j++) {
const word = words[j];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,8 @@ printWidth: 20
>Lorem Ipsum</span
>
</p>

it should preserve non-breaking spaces (U+00A0) as text and not drop borrowed closing tags
<p>
<strong><em> </em></strong>
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ printWidth: 20
<p>
{{ drop }}<span>Lorem Ipsum</span>
</p>

it should preserve non-breaking spaces (U+00A0) as text and not drop borrowed closing tags
<p><strong><em> </em></strong></p>
Loading