From ad791bcd415f51bcc884ff3f7c2ca573b06e5bda Mon Sep 17 00:00:00 2001 From: Boulea7 <145515129+Boulea7@users.noreply.github.com> Date: Wed, 2 Dec 2026 23:16:41 +0800 Subject: [PATCH] fix: preserve empty namespace boundaries in lossy selectors --- src/__tests__/lossy.mjs | 9 +++++++++ src/__tests__/property/known.mjs | 6 ------ src/parser.js | 11 ++++++++++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/__tests__/lossy.mjs b/src/__tests__/lossy.mjs index d64c209..0e6adff 100644 --- a/src/__tests__/lossy.mjs +++ b/src/__tests__/lossy.mjs @@ -46,6 +46,15 @@ ava("namespace - all elements without namespace, space before", testLossy, " | ava("namespace - all elements without namespace, space after", testLossy, "|* ", "|*"); ava("namespace - tag with no namespace, space before", testLossy, " |button", "|button"); ava("namespace - tag with no namespace, space after", testLossy, "|button ", "|button"); +ava("namespace - descendant tag with no namespace", testLossy, "section |input", "section |input"); +ava("namespace - descendant universal with no namespace", testLossy, "main\t|*", "main |*"); +ava("namespace - child tag with no namespace", testLossy, "section > |input", "section> |input"); +ava( + "namespace - comment in the descendant gap", + testLossy, + "nav /* note */ |a[href]", + "nav /* note */|a[href]", +); ava( "namespace - inside attribute, space before", testLossy, diff --git a/src/__tests__/property/known.mjs b/src/__tests__/property/known.mjs index d7b15b0..a01ce0d 100644 --- a/src/__tests__/property/known.mjs +++ b/src/__tests__/property/known.mjs @@ -11,12 +11,6 @@ export const KNOWN = [ note: "#189: a comment before a combinator is kept in the combinator's raws", match: (f) => f.type === "combinator" && f.text.includes("/*"), }, - { - property: "lossy output is stable", - tracking: "not filed yet", - note: "lossy output drops the whitespace before a `|`, so `a |b` becomes `a|b`", - match: (f) => /\s\|(?!=)|\*\/\|/.test(f.input), - }, { property: "values set through the API read back the same", tracking: `${ISSUE}357`, diff --git a/src/parser.js b/src/parser.js index f0909b5..611b74e 100644 --- a/src/parser.js +++ b/src/parser.js @@ -740,6 +740,13 @@ export default class Parser { let { space, rawSpace } = this.convertWhitespaceNodesToSpace( spaceOrDescendantSelectorNodes, ); + if (this.options.lossy && node.value === "|") { + // Keep the descendant space before an empty namespace prefix. + space = " "; + if (rawSpace) { + rawSpace = " " + rawSpace; + } + } node.spaces.before = space; node.rawSpaceBefore = rawSpace; } @@ -773,7 +780,9 @@ export default class Parser { } if (this.currToken && this.currToken[TOKEN.TYPE] === tokens.space) { - node.spaces.after = this.optionalSpace(this.content()); + // A bare pipe must stay separate from the preceding combinator. + const beforeEmptyNamespace = this.nextToken && this.content(this.nextToken) === "|"; + node.spaces.after = this.lossySpace(this.content(), beforeEmptyNamespace); this.position++; }