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
53 changes: 53 additions & 0 deletions packages/core/src/compiler/compositionScoping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,59 @@ body { margin: 0; }
expect(fakeWindow.__captured).toEqual({ title: "Pro", price: "$29" });
});

it("routes the documented window.__hyperframes.getVariables() to the scoped variant too", () => {
// Regression: the docs (variables-and-media.md) show `window.__hyperframes.
// getVariables()`, but inside a sub-comp the scoped `window` proxy used to
// fall through to the HOST page's base __hyperframes, returning the wrong
// (or empty) variables — the bare `__hyperframes` param was the only form
// that worked. Both spellings must now resolve to this comp's variables.
const { document } = parseHTML(`<div data-composition-id="card-1"></div>`);
const fakeWindow: Record<string, unknown> = {
document,
__timelines: {},
__hfVariablesByComp: {
"card-1": { title: "Pro", price: "$29" },
"card-2": { title: "Enterprise", price: "Custom" },
},
__hyperframes: {
getVariables: () => ({ title: "TOP-LEVEL-LEAK" }),
fitTextFontSize: () => undefined,
},
};
const wrapped = wrapScopedCompositionScript(
`window.__captured = window.__hyperframes.getVariables();`,
"card-1",
);

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

expect(fakeWindow.__captured).toEqual({ title: "Pro", price: "$29" });
});

it("preserves non-getVariables members on window.__hyperframes (only getVariables is rescoped)", () => {
const { document } = parseHTML(`<div data-composition-id="card-1"></div>`);
let fitCalled = false;
const fakeWindow: Record<string, unknown> = {
document,
__timelines: {},
__hfVariablesByComp: { "card-1": { title: "Pro" } },
__hyperframes: {
getVariables: () => ({ title: "TOP-LEVEL-LEAK" }),
fitTextFontSize: () => {
fitCalled = true;
},
},
};
const wrapped = wrapScopedCompositionScript(
`window.__hyperframes.fitTextFontSize();`,
"card-1",
);

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

expect(fitCalled).toBe(true);
});

it("scoped getVariables reads from the runtime composition id when it differs", () => {
const { document } = parseHTML(`<div data-composition-id="scene"></div>`);
const fakeWindow: Record<string, unknown> = {
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/compiler/compositionScoping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,16 @@
? new Proxy(window, {
get: function(target, prop, receiver) {
if (prop === "__timelines") return __hfGetTimelineRegistry();
// Inside a sub-composition, __hyperframes is passed as a bare script
// param bound to the SCOPED variant (per-comp getVariables). But
// authors routinely write the documented window.__hyperframes.
// getVariables() form, which would otherwise fall through to the host
// page's base __hyperframes and return the WRONG (or empty) variables
// for this instance. Route it to the scoped variant too so both
// spellings resolve to this composition's own variables.
// (__hfScopedHyperframes is a hoisted var assigned below, before any
// sub-comp script -- the only code that reads this -- runs.)
if (prop === "__hyperframes") return __hfScopedHyperframes;
return Reflect.get(target, prop, target);
},
set: function(target, prop, value, receiver) {
Expand Down Expand Up @@ -489,7 +499,7 @@
var __hfBaseHyperframes = window.__hyperframes;
var __hfScopedHyperframes = !__hfBaseHyperframes
? __hfBaseHyperframes
: Object.assign({}, __hfBaseHyperframes, {

Check warning

Code scanning / CodeQL

Unsafe code constructed from library input Medium

This string concatenation which depends on
library input
is later
interpreted as code
.
This string concatenation which depends on
library input
is later
interpreted as code
.
This string concatenation which depends on
library input
is later
interpreted as code
.
This string concatenation which depends on
library input
is later
interpreted as code
.
This string concatenation which depends on
library input
is later
interpreted as code
.
This string concatenation which depends on
library input
is later
interpreted as code
.
This string concatenation which depends on
library input
is later
interpreted as code
.
This string concatenation which depends on
library input
is later
interpreted as code
.
This string concatenation which depends on
library input
is later
interpreted as code
.
This string concatenation which depends on
library input
is later
interpreted as code
.
This string concatenation which depends on
library input
is later
interpreted as code
.
This string concatenation which depends on
library input
is later
interpreted as code
.
This string concatenation which depends on
library input
is later
interpreted as code
.
This string concatenation which depends on
library input
is later
interpreted as code
.
This string concatenation which depends on
library input
is later
interpreted as code
.
This string concatenation which depends on
library input
is later
interpreted as code
.
This string concatenation which depends on
library input
is later
interpreted as code
.
getVariables: function() {
var byComp = window.__hfVariablesByComp;
var scoped = byComp && __hfTimelineCompId ? byComp[__hfTimelineCompId] : null;
Expand Down
Loading