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
2 changes: 1 addition & 1 deletion packages/core/src/compositionReadiness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function isActiveAtFirstFrame(
while (current) {
if (isTimedElement(current)) {
if (
!isRuntimeElementVisibleAt(current as HTMLElement, {
!isRuntimeElementVisibleAt(current, {
currentTime: 0,
compositionDuration: Number.POSITIVE_INFINITY,
canonicalFps: 30,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Generated by scripts/build-position-edits-render.ts; do not edit.
const POSITION_EDITS_RENDER_IIFE: string =
'"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 k(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",_=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$/,E=(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`${E(r,t)} ${o}`;let i=a===void 0?"":` ${a}`;return`${E(r,t)} ${E(s,o)}${i}`},j=e=>{try{e.ownerDocument.defaultView?.gsap?.getProperty?.(e,"x")}catch{}},C=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""}},b=new WeakMap;function G(e,t){let o=b.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=_(e.getAttribute("data-x"))-_(e.getAttribute(w)),s=_(e.getAttribute("data-y"))-_(e.getAttribute(y));e.hasAttribute(m)||e.setAttribute(m,C(e)),o===void 0&&j(e);let a=e.getAttribute(m)??"",i=V(a,`${r}px`,`${s}px`);e.style.setProperty("translate",i),b.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||!k(i))continue;let g=i.getAttribute(m)??"";g===""?i.style.removeProperty("translate"):i.style.setProperty("translate",g),i.removeAttribute(m),b.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||!k(i)||(G(i,t),s+=1)}return s}var $="__hfPositionEditsSeekReapplyWrapped",D=new WeakSet,M=new WeakMap,H=new WeakMap;function F(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=H.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),H.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),F(window))}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",I,{once:!0}):I();})();\n';
'"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 k(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",_=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},$=/^-?(?:\\d+(?:\\.\\d+)?|\\.\\d+)px$/,E=(e,t)=>$.test(e)&&$.test(t)?`${parseFloat(e)+parseFloat(t)}px`:`calc(${e} + ${t})`,C=(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`${E(r,t)} ${o}`;let i=a===void 0?"":` ${a}`;return`${E(r,t)} ${E(s,o)}${i}`},V=e=>{try{e.ownerDocument.defaultView?.gsap?.getProperty?.(e,"x")}catch{}},j=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""}},b=new WeakMap;function B(e,t){let o=b.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=_(e.getAttribute("data-x"))-_(e.getAttribute(w)),s=_(e.getAttribute("data-y"))-_(e.getAttribute(y));e.hasAttribute(m)||e.setAttribute(m,j(e)),o===void 0&&V(e);let a=e.getAttribute(m)??"",i=C(a,`${r}px`,`${s}px`);e.style.setProperty("translate",i),b.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||!k(i))continue;let g=i.getAttribute(m)??"";g===""?i.style.removeProperty("translate"):i.style.setProperty("translate",g),i.removeAttribute(m),b.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||!k(i)||(B(i,t),s+=1)}return s}var L="__hfPositionEditsSeekReapplyWrapped",D=new WeakSet,F=new WeakMap,M=new WeakMap;function H(e){let t=e,o=()=>{try{S(t.document)}catch{}},r=n=>typeof n=="function"&&(D.has(n)||!!n[L]),s=n=>{D.add(n);try{Object.defineProperty(n,L,{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=F.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),F.set(n,u),o(),!0},g=(n,l)=>{let u=M.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),M.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';

/** Returns the pre-built position-edits render IIFE as a string constant. */
export function getPositionEditsRenderScript(): string {
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/runtime/domRealm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,15 @@ export function isLinkElement(value: unknown): value is HTMLLinkElement {
return isHtmlTag(value, "link");
}

/** HTML or SVG: TypeScript narrows an `Element` by `HTMLElement | SVGElement` to `HTMLElement` alone. */
export type StylableElement = Element & ElementCSSInlineStyle;

/**
* An element that carries a CSS `style` map: HTML **or** SVG. Position edits and
* transform writes apply to both (authored SVG `<text>`/shapes honour the CSS
* `translate` longhand exactly as HTML does).
*/
export function isStylableElement(value: unknown): value is HTMLElement | SVGElement {
export function isStylableElement(value: unknown): value is StylableElement {
return (
isElementNode(value) &&
((value as NodeLike).namespaceURI === XHTML_NS ||
Expand Down
40 changes: 40 additions & 0 deletions packages/core/src/runtime/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,46 @@ describe("initSandboxRuntimeModular", () => {
expect(earlyClip.style.visibility).toBe("hidden");
});

it("shows only the full-frame <svg> clip whose window holds the seek", () => {
const root = buildRoot();
const SVG_NS = "http://www.w3.org/2000/svg";
const windows = [
[0, 4],
[4, 8],
[12, 8],
];
const layers = windows.map(([start, duration]) => {
const svg = document.createElementNS(SVG_NS, "svg");
svg.setAttribute("data-start", String(start));
svg.setAttribute("data-duration", String(duration));
// A timed shape that outlives its layer must still go with it.
const shape = document.createElementNS(SVG_NS, "rect");
shape.setAttribute("data-start", "0");
shape.setAttribute("data-duration", "20");
svg.appendChild(shape);
root.appendChild(svg);
return { svg, shape };
});
window.__timelines = { main: createMockTimeline(20) };
initSandboxRuntimeModular();

for (const [time, shown] of [
[1, 0],
[5, 1],
[16, 2],
]) {
window.__player?.renderSeek(time);
expect(
layers.map(({ svg }) => svg.style.visibility),
`layers at ${time}s`,
).toEqual(layers.map((_, i) => (i === shown ? "visible" : "hidden")));
expect(
layers.map(({ shape }) => shape.style.visibility),
`shapes at ${time}s`,
).toEqual(layers.map((_, i) => (i === shown ? "visible" : "hidden")));
}
});

it("never shows two back-to-back clips at their shared boundary", () => {
const root = buildRoot();
const first = addClip(root, 0, 2.5);
Expand Down
24 changes: 13 additions & 11 deletions packages/core/src/runtime/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ import {
isHtmlElement,
isImageElement,
isMediaElement,
isStylableElement,
isVideoElement,
type StylableElement,
} from "./domRealm";
import { audibleVideoNeedsWebAudio, isAudibleVideoElement } from "../audibleVideo";

Expand Down Expand Up @@ -2556,7 +2558,7 @@ export function initSandboxRuntimeModular(): void {
// instead of overlapping it. Positioned clips keep `visibility:hidden` (cheaper,
// and avoids disturbing absolute media playback). Computed once per element.
let timedClipInFlow = new WeakMap<Element, boolean>();
const isTimedClipInFlow = (el: HTMLElement): boolean => {
const isTimedClipInFlow = (el: Element): boolean => {
const cached = timedClipInFlow.get(el);
if (cached !== undefined) return cached;
const pos = window.getComputedStyle(el).position;
Expand All @@ -2572,7 +2574,7 @@ export function initSandboxRuntimeModular(): void {
// show. `visibility:hidden` doesn't have this problem (a child can override it
// with `visibility:visible`), so containers keep that and only leaves leave-flow.
let timedClipIsLeaf = new WeakMap<Element, boolean>();
const isTimedClipLeaf = (el: HTMLElement): boolean => {
const isTimedClipLeaf = (el: Element): boolean => {
const cached = timedClipIsLeaf.get(el);
if (cached !== undefined) return cached;
const leaf = el.querySelector("[data-start]") === null;
Expand All @@ -2594,8 +2596,8 @@ export function initSandboxRuntimeModular(): void {
// visibility pass applied, so showing the element puts exactly that back. Keyed on
// what was applied, not on `isTimedClipInFlow`: that answer can flip between the hide
// and the show pass once `applyClipLayout` force-absolutizes the clip.
const displayBeforeHide = new WeakMap<HTMLElement, { value: string; priority: string }>();
const hideByDisplay = (el: HTMLElement, plainNoneMayBeLeftover: boolean) => {
const displayBeforeHide = new WeakMap<StylableElement, { value: string; priority: string }>();
const hideByDisplay = (el: StylableElement, plainNoneMayBeLeftover: boolean) => {
if (!displayBeforeHide.has(el)) {
const value = el.style.getPropertyValue("display");
const priority = el.style.getPropertyPriority("display");
Expand All @@ -2605,14 +2607,14 @@ export function initSandboxRuntimeModular(): void {
}
el.style.display = "none";
};
const restoreDisplay = (el: HTMLElement) => {
const restoreDisplay = (el: StylableElement) => {
const before = displayBeforeHide.get(el);
if (!before) return;
displayBeforeHide.delete(el);
if (before.value) el.style.setProperty("display", before.value, before.priority);
else el.style.removeProperty("display");
};
const dataHiddenDisplayNodes = new WeakSet<HTMLElement>();
const dataHiddenDisplayNodes = new WeakSet<StylableElement>();
// A data-hidden toggle on (or affecting) an audio element must re-schedule
// WebAudio playback so the hidden clip's source is dropped/restored mid-
// playback. Batched to one call per syncTimedElementVisibility pass, not
Expand All @@ -2631,7 +2633,7 @@ export function initSandboxRuntimeModular(): void {
isMediaElement(el) &&
el.hasAttribute("data-start") &&
(joinsWebAudio(el) || webAudio.routesElement(el));
const nodeAffectsAudio = (node: HTMLElement): boolean =>
const nodeAffectsAudio = (node: Element): boolean =>
affectsAudio(node) || Array.from(node.querySelectorAll(WEB_AUDIO_MEDIA)).some(affectsAudio);

// An `<hf-audio-group>` carries no `data-start`, so it is never among
Expand Down Expand Up @@ -2672,7 +2674,7 @@ export function initSandboxRuntimeModular(): void {
0,
timingRevision,
);
return (node: HTMLElement, time: number) =>
return (node: Element, time: number) =>
isRuntimeElementVisibleAt(node, {
currentTime: time,
compositionDuration,
Expand All @@ -2690,7 +2692,7 @@ export function initSandboxRuntimeModular(): void {
rootComp: HTMLElement | null,
) => {
for (let node = from; node && node !== rootComp; node = node.parentElement)
if (isHtmlElement(node) && node.hasAttribute("data-start") && !visibleAt(node, time))
if (isStylableElement(node) && node.hasAttribute("data-start") && !visibleAt(node, time))
return false;
return true;
};
Expand All @@ -2701,7 +2703,7 @@ export function initSandboxRuntimeModular(): void {
// Unskipped while within the window on either side of the playhead, so a step or shuttle back
// across a cut finds the clip it left still loaded; the edge checks catch a clip shorter than it.
const dueSoon = (
node: HTMLElement,
node: Element,
visibleAt: ReturnType<typeof timedVisibilityAt>,
t: number,
ahead = LOOKAHEAD_SECONDS,
Expand Down Expand Up @@ -2802,7 +2804,7 @@ export function initSandboxRuntimeModular(): void {
let decidedTimedClip = false;
const visibleAt = timedVisibilityAt(timingRevision);
for (const rawNode of visibilityNodes) {
if (!isHtmlElement(rawNode)) continue;
if (!isStylableElement(rawNode)) continue;

if (rawNode.hasAttribute("data-hidden")) {
if (!dataHiddenDisplayNodes.has(rawNode)) {
Expand Down
10 changes: 1 addition & 9 deletions packages/core/src/runtime/positionEdits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,12 @@
*/

import { emitAnalyticsEvent } from "./analytics";
import { isStylableElement } from "./domRealm";
import { isStylableElement, type StylableElement } from "./domRealm";

export const EDIT_BASE_X_ATTR = "data-hf-edit-base-x";
export const EDIT_BASE_Y_ATTR = "data-hf-edit-base-y";
export const EDIT_ORIGINAL_TRANSLATE_ATTR = "data-hf-edit-original-translate";

/**
* Elements a position edit can apply to: HTML elements AND SVG graphics (authored `<text>` labels,
* shapes, groups). Both expose `.style` and honor the CSS `translate` longhand in modern Chrome, so
* the same delta→translate compose logic works for either. (SVG was previously excluded by an
* `instanceof HTMLElement`-only guard, which silently dropped every move on an SVG element.)
*/
type StylableElement = HTMLElement | SVGElement;

const num = (value: string | null): number => {
const n = parseFloat(value ?? "");
return Number.isFinite(n) ? n : 0;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/runtime/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { LOOP_INFLATED_TIMELINE_SECONDS } from "./compositionLength";
import { cutToHostSlots } from "../mediaTiming";

export function isRuntimeElementVisibleAt(
rawNode: HTMLElement,
rawNode: Element,
options: {
currentTime: number;
compositionDuration: number;
Expand Down
Loading