FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: lossless mode drops whitespace when a selector ends empty by theRizwan · Pull Request #337 · postcss/postcss-selector-parser · GitHub

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

Filter by extension

Filter by extension .js  (1) .mjs  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
17 changes: 17 additions & 0 deletions src/__tests__/pseudos.mjs
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
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,20 @@ test("nested pseudo classes", "section:not( :has(h1, h2 ) )", (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].nodes[1].nodes[0].type, "tag");
t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].nodes[1].nodes[0].value, "h2");
});

// Whitespace is stored on nodes, so a selector that ends before any node is
// created had nowhere to keep it and lost it. `test` asserts the round-trip.
test("trailing space after a comma inside a pseudo", ":not(a, )", (t, tree) => {
const args = tree.nodes[0].nodes[0].nodes;
t.deepEqual(args.length, 2);
t.deepEqual(args[1].toString(), " ");
});

test("trailing space in a nested pseudo", "div:has(a, )");
test("trailing space after several commas", ":not(a, b, )");
test("space before a trailing comma", ":not(a , )");
test("whitespace-only pseudo argument", ":not( )");
test("multiple spaces in a whitespace-only argument", ":not( )");
test("trailing space after a top-level comma", "a, ");
test("space around a comment in a pseudo argument", ":not( /*c*/ )");

32 changes: 32 additions & 0 deletions src/parser.js
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
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,37 @@ export default class Parser {
return this.newNode(node);
}

// Whitespace is only ever stored on a node, as `spaces.before` or
// `spaces.after`. `space()` parks leading whitespace on `this.spaces` for the
// next node to claim, so a selector that ends before any node is created --
// `:not(a, )`, `:not( )`, a trailing `a, ` -- drops it. Park it on an empty
// string node instead, which is what `parseWhitespaceEquivalentTokens`
// already does for the comment-adjacent case.
flushPendingSpaces() {
if (!this.spaces) {
return;
}
const last = this.current.last;
if (last) {
// `space()` routes trailing whitespace to `this.spaces` whenever the
// selector so far holds nothing but comments, so `:not( /*c*/ )` lands
// here rather than on the `spaces.after` path.
last.spaces.after += this.spaces;
this.spaces = "";
return;
}
const token = this.currToken || this.prevToken;
this.newNode(
new Str({
value: "",
source: token ? getTokenSource(token) : undefined,
sourceIndex: token ? token[TOKEN.START_POS] : 0,
}),
);
}

comma() {
this.flushPendingSpaces();
if (this.position === this.tokens.length - 1) {
this.root.trailingComma = true;
this.position++;
Expand Down Expand Up @@ -782,6 +812,7 @@ export default class Parser {
if (unbalanced) {
this.parse();
} else {
this.flushPendingSpaces();
this.current.source.end = tokenEnd(this.currToken);
this.current.parent.source.end = tokenEnd(this.currToken);
this.position++;
Expand Down Expand Up @@ -1005,6 +1036,7 @@ export default class Parser {
while (this.position < this.tokens.length) {
this.parse(true);
}
this.flushPendingSpaces();
this.current._inferEndPosition();
return this.root;
}
Expand Down
Loading

Back | FazBrowse Home | New Git URL