Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/stringify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ describe("Stringify CSS spec compliance", () => {
["a\u{7F}b", String.raw`a\7f b`, "DEL character"],
["a\u{0}b", String.raw`a\fffd b`, "null → FFFD"],
["a'b", String.raw`a\'b`, "single quote"],
["a=b", String.raw`a\=b`, "equals sign"],
["a&b", String.raw`a\&b`, "ampersand"],
["a{b}c", String.raw`a\{b\}c`, "braces"],
["a?b", String.raw`a\?b`, "question mark"],
["a@b", String.raw`a\@b`, "at sign"],
Comment thread
coderabbitai[bot] marked this conversation as resolved.
["a`b", String.raw`a\`b`, "backtick"],
];

it.each(escapeCases)("%s → %s (%s)", (name, expected) => {
Expand All @@ -44,3 +50,18 @@ describe("Stringify CSS spec compliance", () => {
expect(stringify(ast)).toBe(name);
});
});

describe("Stringify round-trips special characters in identifiers", () => {
const selectors = [
String.raw`.x\&y`,
String.raw`[data-foo\=bar]`,
String.raw`.a\{b\}c`,
String.raw`.w-1\/2`,
String.raw`#a\?b`,
];
it.each(selectors)("%s", (selector) => {
const original = parse(selector);
const roundTripped = parse(stringify(original));
expect(roundTripped).toStrictEqual(original);
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
7 changes: 7 additions & 0 deletions src/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ const charsToEscapeInName = new Set(
"/",
",",
";",
"=",
"&",
"?",
"@",
"`",
"{",
"}",
].map((c) => c.charCodeAt(0)),
);

Expand Down