Skip to content

Commit 0ca18af

Browse files
authored
fix: escape values assigned to an id through the value setter (#326)
`ID` never overrode `value`, so assigning to `id.value` stored the string as-is and `toString()` emitted it unescaped: `#foo.bar` re-parses as an id plus a class. It now uses the same escaping setter `ClassName` has had since 2018. On an id parsed with an escape, reassigning `value` previously had no effect at all, because a stale `raws.value` survived the assignment and kept being emitted. Storing through `raws` fixes that too. Anyone assigning an already-escaped value now gets it escaped twice; `setPropertyWithoutEscape()` is the escape hatch.
1 parent b74fdd4 commit 0ca18af

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

‎API.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ Arguments:
6868

6969
* `props (object)`: The new node's properties.
7070

71+
Notes:
72+
* Assigning to `value` escapes characters that aren't valid in an identifier
73+
(`node.value = 'foo.bar'` stringifies as `.foo\.bar`). To assign a
74+
pre-escaped value, use `node.setPropertyWithoutEscape('value', v)` instead.
75+
7176
### `parser.combinator([props])`
7277

7378
Creates a new selector combinator.
@@ -123,6 +128,12 @@ Arguments:
123128

124129
* `props (object)`: The new node's properties.
125130

131+
Notes:
132+
* Assigning to `value` escapes characters that aren't valid in an identifier
133+
(`node.value = 'foo.bar'` stringifies as `#foo\.bar`), matching
134+
`parser.className`. To assign a pre-escaped value, use
135+
`node.setPropertyWithoutEscape('value', v)` instead.
136+
126137
### `parser.nesting([props])`
127138

128139
Creates a new nesting selector.

‎src/__tests__/id.mjs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ test("id selector", "#one", (t, tree) => {
55
t.deepEqual(tree.nodes[0].nodes[0].type, "id");
66
});
77

8+
test("ID#set value", "#fo\\o", (t, selectors) => {
9+
let id = selectors.first.first;
10+
t.deepEqual(id.raws, { value: "fo\\o" });
11+
id.value = "bar";
12+
t.deepEqual(id.raws, {});
13+
id.value = "foo.bar";
14+
t.deepEqual(id.raws, { value: "foo\\.bar" });
15+
});
16+
817
test("id selector with universal", "*#z98y ", (t, tree) => {
918
t.deepEqual(tree.nodes[0].nodes[0].value, "*");
1019
t.deepEqual(tree.nodes[0].nodes[0].type, "universal");

‎src/selectors/id.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1+
import cssesc from "../util/cssesc";
2+
import { ensureObject } from "../util";
13
import Node from "./node";
24
import { ID as IDType } from "./types";
35

46
export default class ID extends Node {
57
constructor(opts) {
68
super(opts);
79
this.type = IDType;
10+
this._constructed = true;
11+
}
12+
13+
set value(v) {
14+
if (this._constructed) {
15+
let escaped = cssesc(v, { isIdentifier: true });
16+
if (escaped !== v) {
17+
ensureObject(this, "raws");
18+
this.raws.value = escaped;
19+
} else if (this.raws) {
20+
delete this.raws.value;
21+
}
22+
}
23+
this._value = v;
24+
}
25+
26+
get value() {
27+
return this._value;
828
}
929

1030
valueToString() {

0 commit comments

Comments
 (0)