Skip to content
Closed
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
40 changes: 40 additions & 0 deletions packages/core/src/compiler/compositionScoping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,46 @@ window.__timelines.scene = tl;
expect(gsapTargets).toEqual([["Scene"], ["Scene"]]);
});

it("scopes each selector in a GSAP target array to the composition root", () => {
const { document } = parseHTML(`
<div data-composition-id="scene">
<h1 class="title">Scene title</h1>
<p class="subtitle">Scene subtitle</p>
</div>
<div data-composition-id="other">
<h1 class="title">Other title</h1>
<p class="subtitle">Other subtitle</p>
</div>
`);
const targetCompositions: Array<string | null> = [];
const fakeWindow = {
document,
__timelines: {},
gsap: {
to(targets: Array<string | Element>) {
const resolvedTargets = targets.flatMap((target) =>
typeof target === "string" ? Array.from(document.querySelectorAll(target)) : [target],
);
targetCompositions.push(
...resolvedTargets.map(
(target) =>
target.closest("[data-composition-id]")?.getAttribute("data-composition-id") ??
null,
),
);
},
},
};
const wrapped = wrapScopedCompositionScript(
`gsap.to(['.title', '.subtitle'], { opacity: 1 });`,
"scene",
);

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

expect(targetCompositions).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>
Expand Down
11 changes: 9 additions & 2 deletions packages/core/src/compiler/compositionScoping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,15 @@ export function wrapScopedCompositionScript(
})
: window;
var __hfResolveGsapTarget = function(target) {
if (typeof target !== "string") return target;
return __hfQueryAll(target);
if (typeof target === "string") return __hfQueryAll(target);
if (!Array.isArray(target)) return target;
return target.reduce(function(resolved, item) {
if (typeof item === "string") {
return resolved.concat(Array.prototype.slice.call(__hfQueryAll(item)));
}
resolved.push(item);
return resolved;
}, []);
};
var __hfScopeTimeline = function(timeline) {
if (!timeline || timeline.__hfScopedCompositionRoot === __hfFindRoot()) return timeline;
Expand Down
Loading