mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-23 11:29:54 +00:00
fix(core): check svg id scopes against excluded roots in one ancestor walk (#8273)
* test(core): a scope with thousands of excluded clips must check each element once * test(core): a page of many svg clips and mounts must scope without a pairwise scan * fix(core): check svg id scopes against excluded roots in one ancestor walk
This commit is contained in:
@@ -1421,3 +1421,36 @@ describe("dropAuthoredSvgReferenceAliases", () => {
|
||||
expect(serialize()).not.toContain("data-hf-svg-reference-aliases");
|
||||
});
|
||||
});
|
||||
|
||||
// fails without the fix: each clip's scope tested every mount, and the page's scope every element, by contains().
|
||||
it("scopes a page of 2000 svg clips and 200 mounts without testing each against every other", () => {
|
||||
const clips = Array.from(
|
||||
{ length: 2000 },
|
||||
(_, i) => `<div data-start="${i}" data-duration="1"><svg><rect/></svg></div>`,
|
||||
).join("");
|
||||
const mounts = Array.from(
|
||||
{ length: 200 },
|
||||
(_, i) => `<div data-composition-id="m${i}" data-composition-src="s.html"></div>`,
|
||||
).join("");
|
||||
const { document } = parseHTML(
|
||||
`<!DOCTYPE html><html><body><div data-composition-id="main">${mounts}${clips}</div></body></html>`,
|
||||
);
|
||||
const hosts = [...document.querySelectorAll("[data-composition-src]")];
|
||||
const elements = document.querySelectorAll("*").length;
|
||||
let owner = Object.getPrototypeOf(document.body);
|
||||
while (!Object.hasOwn(owner, "contains")) owner = Object.getPrototypeOf(owner);
|
||||
const contains = vi.spyOn(owner, "contains");
|
||||
let checks: number;
|
||||
try {
|
||||
inlineSubCompositions(document, hosts, {
|
||||
resolveHtml: () =>
|
||||
`<div data-composition-id="s"><svg><clipPath id="c"/><rect clip-path="url(#c)"/></svg></div>`,
|
||||
parseHtml: (html) => parseHTML(html).document,
|
||||
});
|
||||
checks = contains.mock.calls.length;
|
||||
} finally {
|
||||
contains.mockRestore();
|
||||
}
|
||||
|
||||
expect(checks).toBeLessThan(elements);
|
||||
});
|
||||
|
||||
@@ -825,6 +825,14 @@ export function inlineSubCompositions(
|
||||
dropAuthoredSvgReferenceAliases(document);
|
||||
const hostRoots = svgIdScopes.map(({ root }) => root);
|
||||
const clips = sceneClipRoots(document, hostRoots);
|
||||
// Clips are outermost, so each host sits in at most one: its nearest clip ancestor.
|
||||
const clipIndex = new Map(clips.map((clip, index) => [clip, index]));
|
||||
const hostsInClip = clips.map((): Element[] => []);
|
||||
for (const host of hostRoots) {
|
||||
let at = host.parentElement;
|
||||
while (at && !clipIndex.has(at)) at = at.parentElement;
|
||||
if (at) hostsInClip[clipIndex.get(at)!]!.push(host);
|
||||
}
|
||||
const svgIdMaps = namespaceCollidingSvgIds(
|
||||
document,
|
||||
[
|
||||
@@ -844,7 +852,7 @@ export function inlineSubCompositions(
|
||||
...clips.map((root, index) => ({
|
||||
root,
|
||||
namespace: `clip-${index}`,
|
||||
exclude: hostRoots.filter((host) => root.contains(host)),
|
||||
exclude: hostsInClip[index],
|
||||
})),
|
||||
],
|
||||
undefined,
|
||||
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
SVG_AUTHORED_ID_ATTR,
|
||||
namespaceCollidingSvgIds,
|
||||
rewriteSvgIdReferencesInCss,
|
||||
sceneClipRoots,
|
||||
type SvgIdScope,
|
||||
} from "./svgIdNamespacing";
|
||||
|
||||
@@ -750,3 +751,36 @@ it("rewriteSvgIdReferencesInCss scans a selector of 300000 unclosed brackets in
|
||||
rewriteSvgIdReferencesInCss(`:is(${run}) { fill: red; }`, new Map(), references),
|
||||
).toContain(run);
|
||||
}, 10_000);
|
||||
|
||||
// fails without the fix: every element of the root scope was checked against every excluded clip, one contains() each.
|
||||
it("checks a scope's elements against thousands of excluded clips in one walk each, not once per clip", () => {
|
||||
const clips = Array.from(
|
||||
{ length: 2000 },
|
||||
(_, i) => `<div data-start="${i}" data-duration="1"><svg><rect/></svg></div>`,
|
||||
).join("");
|
||||
const { document } = parseHTML(
|
||||
`<!DOCTYPE html><html><body><div data-composition-id="main"><svg><clipPath id="c"/><rect clip-path="url(#c)"/></svg><div data-composition-id="sub" data-composition-src="s.html"></div>${clips}</div></body></html>`,
|
||||
);
|
||||
const queryable = document as unknown as Parameters<typeof namespaceCollidingSvgIds>[0];
|
||||
const root = document.querySelector('[data-composition-id="main"]')!;
|
||||
const host = document.querySelector("[data-composition-src]")!;
|
||||
const elements = root.querySelectorAll("*").length;
|
||||
let owner = Object.getPrototypeOf(root);
|
||||
while (!Object.hasOwn(owner, "contains")) owner = Object.getPrototypeOf(owner);
|
||||
const contains = vi.spyOn(owner, "contains");
|
||||
let checks: number;
|
||||
let found: Element[];
|
||||
try {
|
||||
found = sceneClipRoots(queryable, [host]);
|
||||
namespaceCollidingSvgIds(queryable, [
|
||||
{ root, namespace: "main", exclude: [host, ...found] },
|
||||
...found.map((clip, i) => ({ root: clip, namespace: `clip${i}` })),
|
||||
]);
|
||||
checks = contains.mock.calls.length;
|
||||
} finally {
|
||||
contains.mockRestore();
|
||||
}
|
||||
|
||||
expect(found).toHaveLength(2000);
|
||||
expect(checks).toBeLessThan(elements);
|
||||
});
|
||||
|
||||
@@ -223,15 +223,16 @@ function collectNativelyReferencedIds(
|
||||
return referenced;
|
||||
}
|
||||
|
||||
function isExcluded(el: Element, exclude: readonly Element[]): boolean {
|
||||
return exclude.some((excluded) => excluded === el || excluded.contains(el));
|
||||
function isExcluded(el: Element, exclude: ReadonlySet<Element>): boolean {
|
||||
for (let at: Element | null = el; at; at = at.parentElement) if (exclude.has(at)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/** `root` plus every descendant that is not inside an excluded subtree. */
|
||||
function collectScopeElements(scope: SvgIdScope): Element[] {
|
||||
const exclude = scope.exclude ?? [];
|
||||
const exclude = new Set(scope.exclude);
|
||||
const descendants = [...scope.root.querySelectorAll("*")];
|
||||
const own = exclude.length ? descendants.filter((el) => !isExcluded(el, exclude)) : descendants;
|
||||
const own = exclude.size ? descendants.filter((el) => !isExcluded(el, exclude)) : descendants;
|
||||
return [scope.root, ...own];
|
||||
}
|
||||
|
||||
@@ -244,9 +245,9 @@ interface ResolvedScope {
|
||||
|
||||
/** Every `<svg>`-subtree element in the scope that carries an id. */
|
||||
function collectSvgIdElements(scope: SvgIdScope): Element[] {
|
||||
const exclude = scope.exclude ?? [];
|
||||
const exclude = new Set(scope.exclude);
|
||||
const matches = [...scope.root.querySelectorAll("svg [id], svg[id]")];
|
||||
return exclude.length ? matches.filter((el) => !isExcluded(el, exclude)) : matches;
|
||||
return exclude.size ? matches.filter((el) => !isExcluded(el, exclude)) : matches;
|
||||
}
|
||||
|
||||
function resolveScope(scope: SvgIdScope): ResolvedScope {
|
||||
@@ -451,11 +452,12 @@ const CLIP_SELECTOR = "[data-start]:not([data-composition-id]):not([data-composi
|
||||
/** The outermost timed clips holding an `<svg>` outside every composition host. Each is its own id scope, as an
|
||||
* inlined composition is, so two scenes drawn with the same ids each resolve their own `url(#id)` and `href`. */
|
||||
export function sceneClipRoots(document: SvgIdQueryable, hosts: readonly Element[]): Element[] {
|
||||
const hostSet = new Set(hosts);
|
||||
return [...document.querySelectorAll(CLIP_SELECTOR)].filter(
|
||||
(clip) =>
|
||||
!clip.parentElement?.closest(CLIP_SELECTOR) &&
|
||||
!!clip.parentElement?.closest("[data-composition-id]") &&
|
||||
!isExcluded(clip, hosts) &&
|
||||
!isExcluded(clip, hostSet) &&
|
||||
!!clip.querySelector("svg"),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user