Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions packages/core/src/compiler/compositionScoping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,56 @@ window.__timelines.scene = tl;
expect(gsapTargets).toEqual([["Scene"], ["Scene"]]);
});

it("scopes getElementById when duplicate IDs exist across composition roots", () => {
const { document } = parseHTML(`
<div data-composition-id="scene-a"><canvas id="gl-canvas"></canvas></div>
<div data-composition-id="scene-b"><canvas id="gl-canvas"></canvas></div>
`);
const fakeWindow = {
document,
__selectedComp: "",
__timelines: {},
};
const wrapped = wrapScopedCompositionScript(
`
window.__selectedComp =
document.getElementById("gl-canvas")
?.closest("[data-composition-id]")
?.getAttribute("data-composition-id") || "null";
`,
"scene-b",
);

new Function("window", wrapped)(fakeWindow);

expect(fakeWindow.__selectedComp).toBe("scene-b");
});

it("scopes getElementById for IDs that need CSS selector escaping", () => {
const { document } = parseHTML(`
<div data-composition-id="scene-a"><div id="clip:1"></div></div>
<div data-composition-id="scene-b"><div id="clip:1"></div></div>
`);
const fakeWindow = {
document,
__selectedComp: "",
__timelines: {},
};
const wrapped = wrapScopedCompositionScript(
`
window.__selectedComp =
document.getElementById("clip:1")
?.closest("[data-composition-id]")
?.getAttribute("data-composition-id") || "null";
`,
"scene-b",
);

new Function("window", wrapped)(fakeWindow);

expect(fakeWindow.__selectedComp).toBe("scene-b");
});

it("reads scoped proxy accessors with the original target receiver", () => {
const root = {
contains(node: unknown) {
Expand Down
25 changes: 19 additions & 6 deletions packages/core/src/compiler/compositionScoping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,30 @@ export function wrapScopedCompositionScript(
var matches = __hfQueryAll(selector);
return matches[0] || null;
};
var __hfGetElementById = function(id) {
var found = window.document.getElementById(id);
if (found && __hfContains(found)) return found;
var root = __hfFindRoot();
if (!root) return found || null;
var idValue = id + "";
if (root.id === idValue) return root;
if (typeof root.querySelector !== "function") return null;
if (typeof CSS !== "undefined" && CSS && typeof CSS.escape === "function") {
try {
return root.querySelector("#" + CSS.escape(idValue)) || null;
} catch {}
}
try {
return root.querySelector('[id="' + __hfEscapeAttr(idValue) + '"]') || null;
} catch {}
return null;
};
var __hfScopedDocument = typeof Proxy === "function"
? new Proxy(window.document, {
get: function(target, prop, receiver) {
if (prop === "querySelector") return __hfQueryOne;
if (prop === "querySelectorAll") return __hfQueryAll;
if (prop === "getElementById") {
return function(id) {
var found = target.getElementById(id);
return found && __hfContains(found) ? found : null;
};
}
if (prop === "getElementById") return __hfGetElementById;
var value = Reflect.get(target, prop, target);
return typeof value === "function" ? value.bind(target) : value;
},
Expand Down
Loading