Skip to content

Commit 5a48321

Browse files
committed
fix(core): applyPositionEdits fails silently across iframe realms
applyPositionEdits(doc) guarded each element with `instanceof HTMLElement` — but `doc` is frequently an iframe's document (the SDK's edit preview, any host embedding a composition), whose elements are HTMLElement instances of THAT frame's realm, never this module's. The check silently no-ops on every single element cross-realm, so bulk position edits never apply inside an iframe. Use the document's own realm's HTMLElement constructor (doc.defaultView); duck-type on `.style` when defaultView is unavailable (a detached/synthetic document). The single-element applyPositionEditToElement was already realm-safe — only the bulk wrapper had the bug. Added a regression test using a real jsdom iframe, confirmed it fails on the old `instanceof HTMLElement` check and passes with the fix.
1 parent 31f0810 commit 5a48321

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

‎packages/core/src/runtime/positionEdits.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,32 @@ describe("applyPositionEdits", () => {
115115
b.remove();
116116
});
117117

118+
it("applies edits to elements from a DIFFERENT realm (an iframe's document)", () => {
119+
// Regression test: a module-scope `instanceof HTMLElement` check fails for
120+
// elements from another window's realm even though they're genuine,
121+
// stylable HTMLElements — exactly the case for any iframe-hosted editor
122+
// (the SDK's edit preview, a host embedding a composition).
123+
const iframe = document.createElement("iframe");
124+
document.body.appendChild(iframe);
125+
const iframeDoc = iframe.contentDocument;
126+
if (!iframeDoc) throw new Error("iframe.contentDocument unavailable in this test env");
127+
128+
// Sanity check the premise: the iframe's HTMLElement is NOT this realm's.
129+
const iframeWindow = iframe.contentWindow as (Window & typeof globalThis) | null;
130+
expect(iframeWindow?.HTMLElement).not.toBe(globalThis.HTMLElement);
131+
132+
const el = iframeDoc.createElement("div");
133+
el.setAttribute("data-x", "50");
134+
el.setAttribute("data-y", "-10");
135+
el.setAttribute(EDIT_BASE_X_ATTR, "0");
136+
el.setAttribute(EDIT_BASE_Y_ATTR, "0");
137+
iframeDoc.body.appendChild(el);
138+
139+
expect(applyPositionEdits(iframeDoc)).toBe(1);
140+
expect(el.style.getPropertyValue("translate")).toBe("50px -10px");
141+
iframe.remove();
142+
});
143+
118144
it("skips re-apply when the written translate was consumed externally (GSAP fold)", () => {
119145
const el = makeElement({
120146
"data-x": "10",

‎packages/core/src/runtime/positionEdits.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,21 @@ export function applyPositionEditToElement(el: HTMLElement, opts?: { force?: boo
156156
*/
157157
export function applyPositionEdits(doc: Document): number {
158158
const marked = doc.querySelectorAll(`[${EDIT_BASE_X_ATTR}], [${EDIT_BASE_Y_ATTR}]`);
159+
// Not `instanceof HTMLElement`: `doc` is frequently an iframe's document (the
160+
// SDK's edit preview, a host embedding a composition), and its elements are
161+
// HTMLElement instances of THAT frame's realm — never this module's. A
162+
// module-scope `instanceof HTMLElement` check silently no-ops on every element
163+
// cross-realm. Use the document's own realm's constructor; duck-type on
164+
// `.style` when defaultView is unavailable (a detached/synthetic document).
165+
const RealmHTMLElement = doc.defaultView?.HTMLElement;
159166
let applied = 0;
160167
for (let i = 0; i < marked.length; i++) {
161168
const el = marked[i];
162-
if (!(el instanceof HTMLElement)) continue;
163-
applyPositionEditToElement(el);
169+
const isStylable = RealmHTMLElement
170+
? el instanceof RealmHTMLElement
171+
: typeof (el as HTMLElement).style?.setProperty === "function";
172+
if (!isStylable) continue;
173+
applyPositionEditToElement(el as HTMLElement);
164174
applied += 1;
165175
}
166176
return applied;

0 commit comments

Comments
 (0)