Skip to content

Commit 8d6abf1

Browse files
fix(css): read no custom selector name a quoted string spells
A list holding ":--bar" in an attribute value no longer reads as a cycle through --bar, which dropped a selector that resolves.
1 parent 33cbea1 commit 8d6abf1

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

‎lib/css/syntax-parser.js‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15753,6 +15753,34 @@ const _orderCustomAtRules = (rules) => {
1575315753
// A `@custom-selector` name as a stored list writes it: after one `:`, not two.
1575415754
const _NESTED_CUSTOM_SELECTOR_RE = /(?:^|[^:\\]):(--[^\s(),:.#[>+~*]+)/g;
1575515755

15756+
/**
15757+
* A printed prelude with each quoted string's contents taken out, so a name a
15758+
* string spells is no name read.
15759+
* @param {string} text the printed text
15760+
* @returns {string} it without what its strings hold
15761+
*/
15762+
const _withoutStrings = (text) => {
15763+
let out = "";
15764+
for (let i = 0; i < text.length; i++) {
15765+
const code = text.charCodeAt(i);
15766+
if (code === CC_REVERSE_SOLIDUS) {
15767+
out += text.slice(i, i + 2);
15768+
i++;
15769+
} else if (code === CC_QUOTATION_MARK || code === CC_APOSTROPHE) {
15770+
let end = i + 1;
15771+
while (end < text.length && text.charCodeAt(end) !== code) {
15772+
if (text.charCodeAt(end) === CC_REVERSE_SOLIDUS) end++;
15773+
end++;
15774+
}
15775+
out += `${text[i]}${text[i]}`;
15776+
i = end;
15777+
} else {
15778+
out += text[i];
15779+
}
15780+
}
15781+
return out;
15782+
};
15783+
1575615784
/**
1575715785
* Drop each name still read through another name in `names`, which is on a
1575815786
* cycle and so stands for nothing.
@@ -15763,7 +15791,8 @@ const _NESTED_CUSTOM_SELECTOR_RE = /(?:^|[^:\\]):(--[^\s(),:.#[>+~*]+)/g;
1576315791
const _dropCyclicNames = (names, reference) => {
1576415792
/** @type {string[]} */
1576515793
const cyclic = [];
15766-
for (const [name, written] of names) {
15794+
for (const [name, stated] of names) {
15795+
const written = _withoutStrings(stated);
1576715796
reference.lastIndex = 0;
1576815797
for (
1576915798
let found = reference.exec(written);

‎test/unitCases/CssSyntax.unittest.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12465,6 +12465,20 @@ describe("CssSyntax minify — `@custom-selector`", () => {
1246512465
);
1246612466
});
1246712467

12468+
it("reads no name a string spells", () => {
12469+
expect(
12470+
resolve(
12471+
'@custom-selector :--bar h1;@custom-selector :--foo [data-x=":--bar x"],[data-y=\'a\\\' :--bar\'];:--foo{color:red}'
12472+
)
12473+
).toBe(':is([data-x=":--bar x"],[data-y="a\' :--bar"]){color:red}');
12474+
// Nor one after an escape, or in a string holding both quotes.
12475+
expect(
12476+
resolve(
12477+
"@custom-selector :--bar h1;@custom-selector :--foo .a\\:--bar,[data-z=\"a\\\"b' :--bar\"];:--foo{color:red}"
12478+
)
12479+
).toBe(":is(.a\\:--bar,[data-z=\"a\\\"b' :--bar\"]){color:red}");
12480+
});
12481+
1246812482
it("keeps a selector name apart from a `@custom-media` one", () => {
1246912483
expect(
1247012484
resolve(

0 commit comments

Comments
 (0)