Skip to content

Commit 73dfe35

Browse files
fix(preview): show the correct scene instead of every scene at once on some loads (#3848)
* fix(runtime): show the right scene when the preview DOM comes from another window The Studio preview sometimes builds the composition body in the editor window and adopts it into the preview frame's document. Adopted nodes keep the prototypes of the realm that created them, so `node instanceof HTMLElement` is false for every element in the composition even though the elements are ordinary HTML sitting in that document. Every guard in the runtime written as `if (!(node instanceof HTMLElement))` then skipped the whole document, silently: nothing threw, nothing logged, and readiness still reported success. The timed-element visibility pass wrote no inline visibility at all, so on those loads the preview painted every scene on top of every other and the editor drew off-canvas markers for elements the user could not see. The auto-stamp pass stopped stamping too, which is why the composition came up one timeline clip short. Replace every realm-sensitive element check in the runtime with structural predicates that ask what a node IS (node type, namespace, tag name) rather than which window's constructor made it. Two local `doc.defaultView.HTMLElement` workarounds are deleted with it: they fix only the case where the nodes belong to the document's own realm, and adoption is exactly what breaks that. * fix(studio): scrub the music track the timeline named, not the first audio `resolveScrubAudioEl` tested `byId instanceof HTMLAudioElement` on a node from the preview iframe's document. That node is an instance of the IFRAME's `HTMLAudioElement`, never this module's, so the check was false on every load and the `musicId` hint was dead. Scrub fell through to the first `<audio>` in the document, which the comment right above it warns can be the voiceover, so dragging the playhead could preview the wrong track. Ask what the node is instead. Also close the gaps an independent review found in the runtime fix: - the cross-realm predicate test had no `<audio>` and no `<img>`, so reverting `isAudioElement` or `isImageElement` to `instanceof` left it green. It no longer does, and the audio case also pins `isMediaElement`, which composes from it and gates the media sync path. - nothing stopped the runtime regressing. `lint-runtime-preview-guards.ts` gains a second check kind: patterns that must be ABSENT under a directory, seeded with DOM-typed `instanceof` under `src/runtime`, pointing at domRealm.ts. Comment lines and tests are exempt, both on purpose. - domRealm.ts stated the adoption mechanism as settled fact. The mixed prototypes are measured; how the nodes get into the frame is not identified, and the docstring now says which is which. Its ownership claim is scoped to the runtime, since packages/studio still hand-rolls its own checks.
1 parent 0408fbc commit 73dfe35

22 files changed

Lines changed: 580 additions & 105 deletions

‎packages/core/scripts/lint-runtime-preview-guards.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,33 @@ type GuardCheckResult = {
1313
failed: GuardSpec[];
1414
};
1515

16+
type BanSpec = {
17+
id: string;
18+
description: string;
19+
directory: string;
20+
pattern: RegExp;
21+
remedy: string;
22+
};
23+
24+
type BanHit = { id: string; filePath: string; line: number; text: string; remedy: string };
25+
26+
/**
27+
* Source shapes that must not come back. Unlike GUARD_SPECS (a pattern that must
28+
* be PRESENT in one file), these are patterns that must be ABSENT everywhere
29+
* under a directory.
30+
*/
31+
const BAN_SPECS: BanSpec[] = [
32+
{
33+
id: "realm_bound_dom_instanceof",
34+
description:
35+
"`instanceof` against a DOM interface answers which realm built the node, not what the node is. The composition DOM is not always built by the realm the runtime runs in, so these checks can return false for every element in the document — silently, with nothing thrown.",
36+
directory: "src/runtime",
37+
pattern:
38+
/\binstanceof\s+(?:Element|Node|Text|Document|DocumentFragment|ShadowRoot|HTML[A-Za-z]*Element|SVG[A-Za-z]*Element)\b/,
39+
remedy: "Use a structural predicate from src/runtime/domRealm.ts instead.",
40+
},
41+
];
42+
1643
// Keep only temporary source-shape guards that do not yet have behavioral
1744
// coverage. Timeline replacement and early-play rebinding are exercised by
1845
// init.test.ts and timelineRebindPolicy.test.ts instead of regexes.
@@ -75,13 +102,65 @@ function checkGuards(guards: GuardSpec[]): GuardCheckResult {
75102
return { passed, failed };
76103
}
77104

105+
/** Tests assert the banned shape on purpose, to prove the predicates match it. */
106+
function isScannableSource(entry: fs.Dirent): boolean {
107+
return entry.isFile() && entry.name.endsWith(".ts") && !entry.name.endsWith(".test.ts");
108+
}
109+
110+
function collectSourceFiles(dir: string): string[] {
111+
return fs
112+
.readdirSync(dir, { withFileTypes: true, recursive: true })
113+
.filter(isScannableSource)
114+
.map((entry) => path.join(entry.parentPath, entry.name));
115+
}
116+
117+
/** A comment may legitimately name the banned shape while explaining why it is banned. */
118+
function isCommentLine(line: string): boolean {
119+
const trimmed = line.trimStart();
120+
return trimmed.startsWith("//") || trimmed.startsWith("*") || trimmed.startsWith("/*");
121+
}
122+
123+
function checkBans(bans: BanSpec[]): BanHit[] {
124+
const hits: BanHit[] = [];
125+
for (const ban of bans) {
126+
const root = resolveFilePath(ban.directory);
127+
if (!fs.existsSync(root)) continue;
128+
for (const file of collectSourceFiles(root)) {
129+
const lines = fs.readFileSync(file, "utf8").split("\n");
130+
lines.forEach((line, index) => {
131+
if (isCommentLine(line) || !ban.pattern.test(line)) return;
132+
hits.push({
133+
id: ban.id,
134+
filePath: path.relative(process.cwd(), file),
135+
line: index + 1,
136+
text: line.trim().slice(0, 160),
137+
remedy: ban.remedy,
138+
});
139+
});
140+
}
141+
}
142+
return hits;
143+
}
144+
78145
function main(): void {
146+
const bannedHits = checkBans(BAN_SPECS);
147+
if (bannedHits.length > 0) {
148+
console.error(
149+
JSON.stringify({
150+
event: "runtime_preview_guards_lint_failed",
151+
bannedShapes: bannedHits,
152+
}),
153+
);
154+
process.exit(1);
155+
}
156+
79157
const result = checkGuards(GUARD_SPECS);
80158
if (result.failed.length === 0) {
81159
console.log(
82160
JSON.stringify({
83161
event: "runtime_preview_guards_lint_passed",
84162
checkedGuards: GUARD_SPECS.length,
163+
checkedBans: BAN_SPECS.length,
85164
}),
86165
);
87166
return;

‎packages/core/src/generated/position-edits-render-inline.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// AUTO-GENERATED by scripts/build-position-edits-render.ts - do not edit
22
const POSITION_EDITS_RENDER_IIFE: string =
3-
'"use strict";(()=>{function A(){return globalThis}function P(e,t){if(typeof window>"u")return;let n=A(),r=n.__hf?.onSwallowed;if(r)try{r({label:e,error:t})}catch(i){}(n.__hfDebug||n.__HYPERFRAMES_DEBUG)&&console.debug(`[hyperframes] ${e} swallowed:`,t)}var T=null;function R(e,t){if(T)try{T({source:"hf-preview",type:"analytics",event:e,properties:t??{}})}catch(n){P("runtime.analytics.site1",n)}}var w="data-hf-edit-base-x",k="data-hf-edit-base-y",y="data-hf-edit-original-translate",S=e=>{let t=parseFloat(e??"");return Number.isFinite(t)?t:0},V=e=>{let t=[],n=0,r="";for(let i of e.trim())i==="("&&(n+=1),i===")"&&(n=Math.max(0,n-1)),/\\s/.test(i)&&n===0?(r&&t.push(r),r=""):r+=i;return r&&t.push(r),t},$=/^-?(?:\\d+(?:\\.\\d+)?|\\.\\d+)px$/,E=(e,t)=>$.test(e)&&$.test(t)?`${parseFloat(e)+parseFloat(t)}px`:`calc(${e} + ${t})`,I=(e,t,n)=>{if(!e||e==="none")return`${t} ${n}`;let[r,i,u]=V(e);if(r===void 0)return`${t} ${n}`;if(i===void 0)return`${E(r,t)} ${n}`;let d=u===void 0?"":` ${u}`;return`${E(r,t)} ${E(i,n)}${d}`},O=e=>{try{e.ownerDocument.defaultView?.gsap?.getProperty?.(e,"x")}catch{}},G=e=>{let t=e.style.getPropertyValue("translate").trim();if(t)return t==="none"?"":t;try{let n=e.ownerDocument.defaultView,r=n?n.getComputedStyle(e).getPropertyValue("translate").trim():"";return r==="none"?"":r}catch{return""}},h=new WeakMap;function H(e,t){let n=h.get(e);if(!t?.force&&n!==void 0&&e.style.getPropertyValue("translate")!==n){R("position_edit_fold_skipped",{hfId:e.getAttribute("data-hf-id")});return}let r=S(e.getAttribute("data-x"))-S(e.getAttribute(w)),i=S(e.getAttribute("data-y"))-S(e.getAttribute(k));e.hasAttribute(y)||e.setAttribute(y,G(e)),n===void 0&&O(e);let u=e.getAttribute(y)??"",d=I(u,`${r}px`,`${i}px`);e.style.setProperty("translate",d),h.set(e,e.style.getPropertyValue("translate"))}function v(e,t){let n=e.defaultView?.HTMLElement,r=e.defaultView?.SVGElement,i=a=>n||r?n!==void 0&&a instanceof n||r!==void 0&&a instanceof r:typeof a.style?.setProperty=="function",u=e.querySelectorAll(`[${y}]:not([${w}]):not([${k}])`);for(let a=0;a<u.length;a++){let l=u[a];if(l===void 0||!i(l))continue;let b=l.getAttribute(y)??"";b===""?l.style.removeProperty("translate"):l.style.setProperty("translate",b),l.removeAttribute(y),h.delete(l)}let d=e.querySelectorAll(`[${w}], [${k}]`),m=0;for(let a=0;a<d.length;a++){let l=d[a];l===void 0||!i(l)||(H(l,t),m+=1)}return m}var x="__hfPositionEditsSeekReapplyWrapped",D=new WeakSet,F=new WeakMap,W=new WeakMap;function L(e){let t=e,n=()=>{try{v(t.document)}catch{}},r=o=>typeof o=="function"&&(D.has(o)||!!o[x]),i=o=>{D.add(o);try{Object.defineProperty(o,x,{value:!0})}catch{}},u=o=>{if(typeof o!="function"||r(o))return o;let s=function(...c){let f=o.apply(this,c);return n(),f};return i(s),s},d=(o,s)=>{let c=F.get(o);if(c?.has(s))return!0;let f=Object.getOwnPropertyDescriptor(o,s);if(f?.configurable===!1){let p=o[s];return typeof p=="function"&&(o[s]=u(p),n()),!1}let g=o[s],_=f?.set;return Object.defineProperty(o,s,{configurable:!0,enumerable:f?.enumerable??!0,get:()=>g,set:p=>{g=u(p),_?.call(o,p)}}),g=u(g),c??(c=new Set),c.add(s),F.set(o,c),n(),!0},m=(o,s)=>{let c=W.get(t),f=Object.getOwnPropertyDescriptor(t,o);if(!c?.has(o)){if(f?.configurable===!1){let p=t[o];return p?d(p,s):!1}let _=t[o];Object.defineProperty(t,o,{configurable:!0,enumerable:f?.enumerable??!0,get:()=>_,set:p=>{_=p,_&&d(_,s)}}),c??(c=new Set),c.add(o),W.set(t,c)}let g=t[o];return g?d(g,s):!1},a=()=>{let o=m("__hf","seek"),s=m("__player","renderSeek");return o&&s};if(a())return;let l=120,b=t.setInterval(()=>{if(a()){t.clearInterval(b);return}l-=1,l<=0&&t.clearInterval(b)},50)}function M(){document.querySelector(`[${w}], [${k}]`)&&(v(document),L(window))}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",M,{once:!0}):M();})();\n';
3+
'"use strict";(()=>{function T(){return globalThis}function P(e,t){if(typeof window>"u")return;let o=T(),r=o.__hf?.onSwallowed;if(r)try{r({label:e,error:t})}catch(s){}(o.__hfDebug||o.__HYPERFRAMES_DEBUG)&&console.debug(`[hyperframes] ${e} swallowed:`,t)}var x=null;function R(e,t){if(x)try{x({source:"hf-preview",type:"analytics",event:e,properties:t??{}})}catch(o){P("runtime.analytics.site1",o)}}var W="http://www.w3.org/1999/xhtml";function N(e){return e!=null&&e.nodeType===1}function _(e){return N(e)&&(e.namespaceURI===W||e.namespaceURI==="http://www.w3.org/2000/svg")}var w="data-hf-edit-base-x",y="data-hf-edit-base-y",m="data-hf-edit-original-translate",k=e=>{let t=parseFloat(e??"");return Number.isFinite(t)?t:0},O=e=>{let t=[],o=0,r="";for(let s of e.trim())s==="("&&(o+=1),s===")"&&(o=Math.max(0,o-1)),/\\s/.test(s)&&o===0?(r&&t.push(r),r=""):r+=s;return r&&t.push(r),t},L=/^-?(?:\\d+(?:\\.\\d+)?|\\.\\d+)px$/,b=(e,t)=>L.test(e)&&L.test(t)?`${parseFloat(e)+parseFloat(t)}px`:`calc(${e} + ${t})`,V=(e,t,o)=>{if(!e||e==="none")return`${t} ${o}`;let[r,s,a]=O(e);if(r===void 0)return`${t} ${o}`;if(s===void 0)return`${b(r,t)} ${o}`;let i=a===void 0?"":` ${a}`;return`${b(r,t)} ${b(s,o)}${i}`},j=e=>{try{e.ownerDocument.defaultView?.gsap?.getProperty?.(e,"x")}catch{}},G=e=>{let t=e.style.getPropertyValue("translate").trim();if(t)return t==="none"?"":t;try{let o=e.ownerDocument.defaultView,r=o?o.getComputedStyle(e).getPropertyValue("translate").trim():"";return r==="none"?"":r}catch{return""}},E=new WeakMap;function B(e,t){let o=E.get(e);if(!t?.force&&o!==void 0&&e.style.getPropertyValue("translate")!==o){R("position_edit_fold_skipped",{hfId:e.getAttribute("data-hf-id")});return}let r=k(e.getAttribute("data-x"))-k(e.getAttribute(w)),s=k(e.getAttribute("data-y"))-k(e.getAttribute(y));e.hasAttribute(m)||e.setAttribute(m,G(e)),o===void 0&&j(e);let a=e.getAttribute(m)??"",i=V(a,`${r}px`,`${s}px`);e.style.setProperty("translate",i),E.set(e,e.style.getPropertyValue("translate"))}function S(e,t){let o=e.querySelectorAll(`[${m}]:not([${w}]):not([${y}])`);for(let a=0;a<o.length;a++){let i=o[a];if(i===void 0||!_(i))continue;let g=i.getAttribute(m)??"";g===""?i.style.removeProperty("translate"):i.style.setProperty("translate",g),i.removeAttribute(m),E.delete(i)}let r=e.querySelectorAll(`[${w}], [${y}]`),s=0;for(let a=0;a<r.length;a++){let i=r[a];i===void 0||!_(i)||(B(i,t),s+=1)}return s}var $="__hfPositionEditsSeekReapplyWrapped",D=new WeakSet,M=new WeakMap,F=new WeakMap;function H(e){let t=e,o=()=>{try{S(t.document)}catch{}},r=n=>typeof n=="function"&&(D.has(n)||!!n[$]),s=n=>{D.add(n);try{Object.defineProperty(n,$,{value:!0})}catch{}},a=n=>{if(typeof n!="function"||r(n))return n;let l=function(...u){let d=n.apply(this,u);return o(),d};return s(l),l},i=(n,l)=>{let u=M.get(n);if(u?.has(l))return!0;let d=Object.getOwnPropertyDescriptor(n,l);if(d?.configurable===!1){let c=n[l];return typeof c=="function"&&(n[l]=a(c),o()),!1}let p=n[l],f=d?.set;return Object.defineProperty(n,l,{configurable:!0,enumerable:d?.enumerable??!0,get:()=>p,set:c=>{p=a(c),f?.call(n,c)}}),p=a(p),u??(u=new Set),u.add(l),M.set(n,u),o(),!0},g=(n,l)=>{let u=F.get(t),d=Object.getOwnPropertyDescriptor(t,n);if(!u?.has(n)){if(d?.configurable===!1){let c=t[n];return c?i(c,l):!1}let f=t[n];Object.defineProperty(t,n,{configurable:!0,enumerable:d?.enumerable??!0,get:()=>f,set:c=>{f=c,f&&i(f,l)}}),u??(u=new Set),u.add(n),F.set(t,u)}let p=t[n];return p?i(p,l):!1},h=()=>{let n=g("__hf","seek"),l=g("__player","renderSeek");return n&&l};if(h())return;let v=120,A=t.setInterval(()=>{if(h()){t.clearInterval(A);return}v-=1,v<=0&&t.clearInterval(A)},50)}function I(){document.querySelector(`[${w}], [${y}]`)&&(S(document),H(window))}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",I,{once:!0}):I();})();\n';
44

55
/** Returns the pre-built position-edits render IIFE as a string constant. */
66
export function getPositionEditsRenderScript(): string {

‎packages/core/src/runtime/adapters/css.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { RuntimeDeterministicAdapter } from "../types";
22
import { swallow } from "../diagnostics";
3+
import { isHtmlElement } from "../domRealm";
34

45
export function createCssAdapter(params?: {
56
resolveStartSeconds?: (element: Element) => number;
@@ -107,7 +108,7 @@ export function createCssAdapter(params?: {
107108
entries = [];
108109
const all = document.querySelectorAll("*");
109110
for (const rawEl of all) {
110-
if (!(rawEl instanceof HTMLElement)) continue;
111+
if (!isHtmlElement(rawEl)) continue;
111112
const style = window.getComputedStyle(rawEl);
112113
if (!style.animationName || style.animationName === "none") continue;
113114
entries.push({

‎packages/core/src/runtime/adapters/video-texture-compat.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
import { findInjectedRenderFrame } from "../renderFrameSibling.js";
17+
import { isImageElement, isVideoElement } from "../domRealm";
1718

1819
/**
1920
* Resolve the decoded render-frame `<img>` for a source `<video>`, if the
@@ -28,7 +29,7 @@ import { findInjectedRenderFrame } from "../renderFrameSibling.js";
2829
function resolveRenderFrameImage(video: HTMLVideoElement): HTMLImageElement | null {
2930
const sibling = video.nextElementSibling;
3031
if (
31-
sibling instanceof HTMLImageElement &&
32+
isImageElement(sibling) &&
3233
sibling.classList.contains("__render_frame__") &&
3334
sibling.complete &&
3435
sibling.naturalWidth > 0
@@ -60,7 +61,7 @@ export function patchVideoTextureCompat(): void {
6061
destination: unknown,
6162
copySize: unknown,
6263
) {
63-
if (source?.source instanceof HTMLVideoElement) {
64+
if (isVideoElement(source?.source)) {
6465
const img = resolveRenderFrameImage(source.source);
6566
if (img) {
6667
return orig.call(this, { ...source, source: img }, destination, copySize);
@@ -98,7 +99,7 @@ export function patchWebGLVideoTextureCompat(): void {
9899
const patched = function (this: unknown, ...args: unknown[]) {
99100
const lastIndex = args.length - 1;
100101
const last = args[lastIndex];
101-
if (last instanceof HTMLVideoElement) {
102+
if (isVideoElement(last)) {
102103
const img = resolveRenderFrameImage(last);
103104
if (img) args[lastIndex] = img;
104105
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
isScalarVariableValue as isScalar,
3333
isSafeMediaUrl,
3434
} from "@hyperframes/parsers/composition";
35+
import { isHtmlElement } from "./domRealm";
3536

3637
// data-var-src only rebinds media `src` on media elements. A user-controlled
3738
// variable value assigned to a src is an XSS surface on tags whose src executes
@@ -143,7 +144,7 @@ function applyCssCustomProperties(doc: Document, cache: ScopeValuesCache): void
143144
const values = valuesForElement(root, cache);
144145
for (const [id, value] of Object.entries(values)) {
145146
const css = cssValueFor(value);
146-
if (css !== null && root instanceof HTMLElement) {
147+
if (css !== null && isHtmlElement(root)) {
147148
root.style.setProperty(`--${id}`, sanitizeCssValue(css));
148149
}
149150
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
* 2. `wordIndex` — fallback, DOM traversal order across .caption-group > span
1111
*/
1212

13+
import { isHtmlElement } from "./domRealm";
14+
1315
interface CaptionOverride {
1416
wordId?: string;
1517
wordIndex?: number;
@@ -70,7 +72,7 @@ function declaredCaptionState(tween: GsapTween): "dim" | "active" | undefined {
7072
}
7173

7274
function resolveCaptionWordElement(el: Element | null): HTMLElement | null {
73-
if (!(el instanceof HTMLElement)) return null;
75+
if (!isHtmlElement(el)) return null;
7476
if (el.dataset.captionWrapper !== "true") return el;
7577

7678
const inner = el.querySelector<HTMLElement>(":scope > span");
@@ -83,7 +85,7 @@ function getCaptionWordElements(): HTMLElement[] {
8385

8486
for (const group of groups) {
8587
for (const child of group.children) {
86-
if (!(child instanceof HTMLElement)) continue;
88+
if (!isHtmlElement(child)) continue;
8789

8890
const wordEl =
8991
child.dataset.captionWrapper === "true"

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import type { RuntimeTimelineLike } from "./types";
1414
import { parseStrictFiniteTimingNumber, resolveNaturalMediaTimelineDuration } from "./playbackRate";
15+
import { isMediaElement } from "./domRealm";
1516

1617
export interface ClipNode {
1718
readonly id: string;
@@ -66,7 +67,7 @@ function durationFromTimeline(
6667
}
6768

6869
function durationFromMedia(el: Element): number | null {
69-
if (!(el instanceof HTMLMediaElement) || !Number.isFinite(el.duration)) return null;
70+
if (!isMediaElement(el) || !Number.isFinite(el.duration)) return null;
7071
return resolveNaturalMediaTimelineDuration(el, el.duration);
7172
}
7273

0 commit comments

Comments
 (0)