mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-23 11:29:54 +00:00
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
This commit is contained in:
@@ -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) => `<clipPath id="${id}"/><rect clip-path="url(#${id})"/>`).join("");
|
||||
const mounts = 1000;
|
||||
const { document, scopes } = scopedDocument([
|
||||
{ namespace: "host", html: `<svg>${defs}</svg>` },
|
||||
...Array.from({ length: mounts }, () => ({ namespace: "scene", html: `<svg>${defs}</svg>` })),
|
||||
]);
|
||||
const has = vi.spyOn(Set.prototype, "has");
|
||||
let idMaps: ReturnType<typeof namespaceCollidingSvgIds>;
|
||||
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;
|
||||
|
||||
@@ -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<string>;
|
||||
nextFreeSuffixByBase: Map<string, number>;
|
||||
}
|
||||
|
||||
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>): 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;
|
||||
|
||||
Reference in New Issue
Block a user