Skip to content

fix: escape id values supplied to the constructor - #360

Open
theRizwan wants to merge 1 commit into
postcss:mainfrom
theRizwan:fix/id-constructor-escape
Open

theRizwan wants to merge 1 commit into
postcss:mainfrom
theRizwan:fix/id-constructor-escape

Conversation

@theRizwan

Copy link
Copy Markdown
Contributor

#349 made parser.className({ value }) escape its value, which fixed #182. parser.id({ value }) has the same gap:

parser.id({ value: "hover:bg" }).toString();   // "#hover:bg"

const id = parser.id();
id.value = "hover:bg";
id.toString();                                  // "#hover\\:bg"

#hover:bg parses back as the id hover followed by a :bg pseudo-class, so a selector built through the API comes out with a different meaning.

The fix

The same change #349 made for ClassName. The ID constructor runs its value through the escaping setter unless raws.value was given. In splitWord(), the parser removes the raws the constructor adds when it had none, as it does for class names, so parsed ids keep their original spelling, including Less such as #foo@{bar}.

Verification

The tests mirror the ones #349 added for classes: five values that need escaping (a colon, a leading digit, a dot, a backslash, a non-ASCII character), explicit raws.value, a plain identifier getting no raws, and parsed ids keeping their raws state when cloned. The five escaping tests fail on main.

npm test is green: lint, type check, and coverage gates.

`parser.id({ value: "hover:bg" })` printed as `#hover:bg`, which parses back
as an id followed by a `:bg` pseudo. Assigning the same value after
construction escaped the colon. postcss#349 fixed this for class names; ids had
the same gap.

The constructor now goes through the escaping setter unless `raws.value`
is given, and the parser drops the raws it adds, as it does for classes,
so parsed ids keep their original spelling.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

className value not escaped when set using constructor

1 participant