From 990aec78ba66a809d1c680d4f46f8a2b40b25446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sat, 21 Nov 2026 05:57:07 +0000 Subject: [PATCH] fix(core): remember each minted svg id's next free suffix (#8285) * test(core): pin svg id minting probes to the number of renames * fix(core): remember each minted svg id's next free suffix --- .../src/compiler/svgIdNamespacing.test.ts | 25 +++++++++++++++++++ .../core/src/compiler/svgIdNamespacing.ts | 17 +++++++------ 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/packages/core/src/compiler/svgIdNamespacing.test.ts b/packages/core/src/compiler/svgIdNamespacing.test.ts index 606e7d52c1..1dcd7d724c 100644 --- a/packages/core/src/compiler/svgIdNamespacing.test.ts +++ b/packages/core/src/compiler/svgIdNamespacing.test.ts @@ -803,6 +803,31 @@ it("splits a rule's selector list a fixed number of times, however many selector expect(splits).toBeLessThan(10); }); +// fails without the fix: each mount sharing a namespace probed past every name the earlier mounts minted. +it("mints names for many mounts sharing a namespace with probes linear in the renames", () => { + const ids = Array.from({ length: 20 }, (_, index) => `c${index}`); + const defs = ids.map((id) => ``).join(""); + const mounts = 1000; + const { document, scopes } = scopedDocument([ + { namespace: "host", html: `${defs}` }, + ...Array.from({ length: mounts }, () => ({ namespace: "scene", html: `${defs}` })), + ]); + const has = vi.spyOn(Set.prototype, "has"); + let idMaps: ReturnType; + let probes: number; + try { + idMaps = namespaceCollidingSvgIds(document, scopes); + probes = has.mock.calls.length; + } finally { + has.mockRestore(); + } + + const minted = idMaps.slice(1).flatMap((idMap) => ids.map((id) => idMap.get(id)!)); + expect(minted).toHaveLength(ids.length * mounts); + expect(new Set(minted).size).toBe(minted.length); + expect(probes).toBeLessThan(100 * minted.length); +}); + // fails without the fix: every ^=, $=, ~= and |= predicate compared its value against every alias. it("looks up prefix, suffix, token and dash predicates without reading every alias", () => { let reads = 0; diff --git a/packages/core/src/compiler/svgIdNamespacing.ts b/packages/core/src/compiler/svgIdNamespacing.ts index 10383fea4a..164005efb2 100644 --- a/packages/core/src/compiler/svgIdNamespacing.ts +++ b/packages/core/src/compiler/svgIdNamespacing.ts @@ -278,6 +278,7 @@ interface IdCensus { /** Every id in the document, extended with each minted id so no two * renames (or a rename and an authored id) can ever coincide. */ usedIds: Set; + nextFreeSuffixByBase: Map; } function currentAuthoredSvgId(el: Element): string | null { @@ -306,16 +307,18 @@ function buildIdCensus(document: SvgIdQueryable): IdCensus { else elementsById.set(authored, [el]); } } - return { elementsById, usedIds }; + return { elementsById, usedIds, nextFreeSuffixByBase: new Map() }; } /** A namespaced id that is not already taken anywhere in the document. */ -function mintNamespacedId(prefix: string, originalId: string, usedIds: Set): string { +function mintNamespacedId(prefix: string, originalId: string, census: IdCensus): string { const base = `${prefix}--${originalId}`; - let candidate = base; - for (let n = 2; usedIds.has(candidate); n += 1) candidate = `${base}-${n}`; - usedIds.add(candidate); - return candidate; + const name = (n: number) => (n === 1 ? base : `${base}-${n}`); + let n = census.nextFreeSuffixByBase.get(base) ?? 1; + while (census.usedIds.has(name(n))) n += 1; + census.nextFreeSuffixByBase.set(base, n + 1); + census.usedIds.add(name(n)); + return name(n); } function collisionScopes( @@ -346,7 +349,7 @@ function planRenames( if (elements.length < 2) continue; for (const index of collisionScopes(id, elements, scopeIndexByRenamable)) { prefixes[index] ??= sanitizeNamespaceSegment(scopes[index]!.namespace); - idMaps[index]!.set(id, mintNamespacedId(prefixes[index]!, id, census.usedIds)); + idMaps[index]!.set(id, mintNamespacedId(prefixes[index]!, id, census)); } } return idMaps;