Skip to content

Commit 8f10b8d

Browse files
authored
fix: lossless mode drops whitespace when a selector ends empty (#337)
`space()` parks leading whitespace on `this.spaces` for the next node to claim, and `newNode` is its only consumer. A selector that ends before any node is created therefore loses it: :not(a, ) -> :not(a,) :not( ) -> :not() a, -> a, Two conditions route whitespace there: the previous token is a comma or an opening parenthesis, or the selector so far holds nothing but comments. The second is why `:not( /*c*/ )` also loses its trailing space. Pending whitespace is now flushed wherever a selector can close: at a comma, at a pseudo's closing parenthesis, and at end of input. It attaches to the last node's `spaces.after` when there is one, otherwise to an empty string node, matching what `parseWhitespaceEquivalentTokens` already does. Lossy output is unchanged. Across 480 generated selectors: 240 newly round-trip, no regressions, and `lossless: false` is byte-identical to before. Fixes #298
1 parent 15c2b45 commit 8f10b8d

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

‎src/__tests__/pseudos.mjs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,20 @@ test("nested pseudo classes", "section:not( :has(h1, h2 ) )", (t, tree) => {
184184
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].nodes[1].nodes[0].type, "tag");
185185
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].nodes[1].nodes[0].value, "h2");
186186
});
187+
188+
// Whitespace is stored on nodes, so a selector that ends before any node is
189+
// created had nowhere to keep it and lost it. `test` asserts the round-trip.
190+
test("trailing space after a comma inside a pseudo", ":not(a, )", (t, tree) => {
191+
const args = tree.nodes[0].nodes[0].nodes;
192+
t.deepEqual(args.length, 2);
193+
t.deepEqual(args[1].toString(), " ");
194+
});
195+
196+
test("trailing space in a nested pseudo", "div:has(a, )");
197+
test("trailing space after several commas", ":not(a, b, )");
198+
test("space before a trailing comma", ":not(a , )");
199+
test("whitespace-only pseudo argument", ":not( )");
200+
test("multiple spaces in a whitespace-only argument", ":not( )");
201+
test("trailing space after a top-level comma", "a, ");
202+
test("space around a comment in a pseudo argument", ":not( /*c*/ )");
203+

‎src/parser.js‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,37 @@ export default class Parser {
641641
return this.newNode(node);
642642
}
643643

644+
// Whitespace is only ever stored on a node, as `spaces.before` or
645+
// `spaces.after`. `space()` parks leading whitespace on `this.spaces` for the
646+
// next node to claim, so a selector that ends before any node is created --
647+
// `:not(a, )`, `:not( )`, a trailing `a, ` -- drops it. Park it on an empty
648+
// string node instead, which is what `parseWhitespaceEquivalentTokens`
649+
// already does for the comment-adjacent case.
650+
flushPendingSpaces() {
651+
if (!this.spaces) {
652+
return;
653+
}
654+
const last = this.current.last;
655+
if (last) {
656+
// `space()` routes trailing whitespace to `this.spaces` whenever the
657+
// selector so far holds nothing but comments, so `:not( /*c*/ )` lands
658+
// here rather than on the `spaces.after` path.
659+
last.spaces.after += this.spaces;
660+
this.spaces = "";
661+
return;
662+
}
663+
const token = this.currToken || this.prevToken;
664+
this.newNode(
665+
new Str({
666+
value: "",
667+
source: token ? getTokenSource(token) : undefined,
668+
sourceIndex: token ? token[TOKEN.START_POS] : 0,
669+
}),
670+
);
671+
}
672+
644673
comma() {
674+
this.flushPendingSpaces();
645675
if (this.position === this.tokens.length - 1) {
646676
this.root.trailingComma = true;
647677
this.position++;
@@ -782,6 +812,7 @@ export default class Parser {
782812
if (unbalanced) {
783813
this.parse();
784814
} else {
815+
this.flushPendingSpaces();
785816
this.current.source.end = tokenEnd(this.currToken);
786817
this.current.parent.source.end = tokenEnd(this.currToken);
787818
this.position++;
@@ -1011,6 +1042,7 @@ export default class Parser {
10111042
while (this.position < this.tokens.length) {
10121043
this.parse(true);
10131044
}
1045+
this.flushPendingSpaces();
10141046
this.current._inferEndPosition();
10151047
return this.root;
10161048
}

0 commit comments

Comments
 (0)