fix(core): split a rule's selector list once when rewriting svg id references (#8275)

* test(core): a rule of thousands of selectors must split its list a fixed number of times

* fix(core): split a rule's selector list once when rewriting svg id references
This commit is contained in:
Miguel Ángel
2026-11-20 07:03:21 +00:00
committed by GitHub
parent 07928702f8
commit fbbbfe0587
2 changed files with 21 additions and 2 deletions
@@ -1,6 +1,7 @@
import { describe, expect, it, vi } from "vitest";
import { JSDOM } from "jsdom";
import { parseHTML } from "linkedom";
import postcss from "postcss";
const hashCalls = vi.hoisted(() => [] as string[]);
vi.mock("../canary", async (importOriginal) => {
@@ -784,3 +785,20 @@ it("checks a scope's elements against thousands of excluded clips in one walk ea
expect(found).toHaveLength(2000);
expect(checks).toBeLessThan(elements);
});
// fails without the fix: each selector's comparison re-read rule.selectors, which splits the whole list again.
it("splits a rule's selector list a fixed number of times, however many selectors it holds", () => {
const css = `${Array.from({ length: 2000 }, (_, i) => `#x${i}`).join(",")} { color: red; }`;
const alias = { name: "href", localName: "href", namespaceURI: null, before: "#a", after: "#a" };
const comma = vi.spyOn(postcss.list, "comma");
let splits: number;
try {
rewriteSvgIdReferencesInCss(css, new Map(), [alias]);
splits = comma.mock.calls.length;
} finally {
comma.mockRestore();
}
expect(splits).toBeGreaterThan(0);
expect(splits).toBeLessThan(10);
});
@@ -568,10 +568,11 @@ export function rewriteSvgIdReferencesInCss(
];
});
root.walkRules((rule) => {
const rewritten = rule.selectors.map((selector) =>
const selectors = rule.selectors;
const rewritten = selectors.map((selector) =>
rewriteSvgSelectors(selector, idsNamedBy(selector), references, undefined, chargeSelector),
);
if (rewritten.some((selector, index) => selector !== rule.selectors[index])) {
if (rewritten.some((selector, index) => selector !== selectors[index])) {
rule.selectors = rewritten;
mutated = true;
}