From 1de742a71d78282dfb83382ac208657b31437113 Mon Sep 17 00:00:00 2001 From: greymoth-jp Date: Mon, 29 Jun 2026 19:00:28 +0900 Subject: [PATCH] fix(id): escape values assigned through the value setter ID had no value setter, so a programmatically assigned id value was emitted verbatim instead of escaped. ClassName has escaped assigned values since 2018; mirror the same setter on ID. --- src/__tests__/id.mjs | 9 +++++++++ src/selectors/id.js | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/__tests__/id.mjs b/src/__tests__/id.mjs index b7216d6..054f0a8 100644 --- a/src/__tests__/id.mjs +++ b/src/__tests__/id.mjs @@ -5,6 +5,15 @@ test("id selector", "#one", (t, tree) => { t.deepEqual(tree.nodes[0].nodes[0].type, "id"); }); +test("ID#set value", "#fo\\o", (t, selectors) => { + let id = selectors.first.first; + t.deepEqual(id.raws, { value: "fo\\o" }); + id.value = "bar"; + t.deepEqual(id.raws, {}); + id.value = "foo.bar"; + t.deepEqual(id.raws, { value: "foo\\.bar" }); +}); + test("id selector with universal", "*#z98y ", (t, tree) => { t.deepEqual(tree.nodes[0].nodes[0].value, "*"); t.deepEqual(tree.nodes[0].nodes[0].type, "universal"); diff --git a/src/selectors/id.js b/src/selectors/id.js index 9f11abb..8d69552 100644 --- a/src/selectors/id.js +++ b/src/selectors/id.js @@ -1,3 +1,5 @@ +import cssesc from "cssesc"; +import { ensureObject } from "../util"; import Node from "./node"; import { ID as IDType } from "./types"; @@ -5,6 +7,24 @@ export default class ID extends Node { constructor(opts) { super(opts); this.type = IDType; + this._constructed = true; + } + + set value(v) { + if (this._constructed) { + let escaped = cssesc(v, { isIdentifier: true }); + if (escaped !== v) { + ensureObject(this, "raws"); + this.raws.value = escaped; + } else if (this.raws) { + delete this.raws.value; + } + } + this._value = v; + } + + get value() { + return this._value; } valueToString() {