Skip to content

Commit 4d8437f

Browse files
authored
fix: preserve whitespace before a * namespace in attribute selectors (#325)
The `*` branch of the attribute parser stored the raw leading whitespace from `spaceBefore`, but that variable had just been cleared a few lines above, so `raws.spaces.attribute.before` became empty and `[ *|a ]` round-tripped to `[*|a ]`. The word-namespace branch already uses `commentBefore` here; do the same for `*`.
1 parent e2f9029 commit 4d8437f

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

src/__tests__/attributes.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ test("attribute selector spaces with namespace (both)", "[ foo|bar ]", (t, tr
2828
t.deepEqual(tree.nodes[0].nodes[0].type, "attribute");
2929
t.falsy(tree.nodes[0].nodes[0].quoted);
3030
});
31+
test("attribute selector spaces with universal namespace (both)", "[ *|bar ]", (t, tree) => {
32+
t.deepEqual(tree.nodes[0].nodes[0].namespace, "*");
33+
t.deepEqual(tree.nodes[0].nodes[0].attribute, "bar");
34+
t.deepEqual(tree.nodes[0].nodes[0].spaces.attribute.before, " ");
35+
t.deepEqual(tree.nodes[0].nodes[0].spaces.attribute.after, " ");
36+
t.deepEqual(tree.nodes[0].nodes[0].type, "attribute");
37+
});
3138
test("attribute selector spaces (both)", "[ href ]", (t, tree) => {
3239
t.deepEqual(tree.nodes[0].nodes[0].attribute, "href");
3340
t.deepEqual(tree.nodes[0].nodes[0].spaces.attribute.before, " ");

src/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export default class Parser {
228228
}
229229
if (commentBefore) {
230230
ensureObject(node, "raws", "spaces", "attribute");
231-
node.raws.spaces.attribute.before = spaceBefore;
231+
node.raws.spaces.attribute.before = commentBefore;
232232
commentBefore = "";
233233
}
234234
node.namespace = (node.namespace || "") + content;

0 commit comments

Comments
 (0)