diff --git a/packages/core/src/runtime/editingElement.ts b/packages/core/src/runtime/editingElement.ts index bd6520d318..9b6e9ca7a2 100644 --- a/packages/core/src/runtime/editingElement.ts +++ b/packages/core/src/runtime/editingElement.ts @@ -1,4 +1,5 @@ // How an element is addressed in source: one rule for Studio and the runtime's editing agent. +import { isHtmlElement } from "./domRealm"; /** Names an element's source file. The parent resolves it: `file`, else its composition map. */ export interface EditingElementSource { @@ -65,6 +66,52 @@ export const MAX_SOURCE_GROUPS = 64; export const MAX_DESCRIBE_HANDLES = 256; export const MAX_POINT_STACK = 64; export const MAX_QUERY_MATCHES = 2_048; +export const MAX_TIMELINE_TEXT = 200; + +const TEXT_TAGS = new Set(["div", "span", "p", "strong", "h1", "h2", "h3", "h4", "h5", "h6"]); + +function isTextBearingTag(tagName: string): boolean { + return TEXT_TAGS.has(tagName); +} + +export function isEditableTextLeaf(el: HTMLElement): boolean { + return isTextBearingTag(el.tagName.toLowerCase()) && el.children.length === 0; +} + +/** A text layer's words and computed look, as its timeline row draws them. */ +export interface TextLayerRead { + value: string; + fontFamily: string; + fontWeight: string; + color: string; + backgroundColor: string; +} + +/** A layer that paints only text on a flat colour, read; null for anything painted beyond that. */ +export function readTextLayer(el: Element): TextLayerRead | null { + if (!isHtmlElement(el) || !isTextBearingTag(el.localName)) return null; + if (!Array.from(el.children).every((child) => isHtmlElement(child) && isEditableTextLeaf(child))) + return null; + const words = (el.textContent ?? "").replace(/\s+/g, " ").trim(); + if (!words) return null; + const style = el.ownerDocument.defaultView?.getComputedStyle(el); + if (!style || (style.backgroundImage || "none") !== "none") return null; + const { fontFamily, fontWeight, color, backgroundColor } = style; + return { value: words, fontFamily, fontWeight, color, backgroundColor }; +} + +/** A text layer as the runtime's clip list sends it: the words and each style value capped. */ +export function readSentTextLayer(el: Element): TextLayerRead | null { + const read = readTextLayer(el); + if (!read) return null; + return { + value: read.value.slice(0, MAX_TIMELINE_TEXT), + fontFamily: read.fontFamily.slice(0, MAX_ELEMENT_VALUE), + fontWeight: read.fontWeight.slice(0, MAX_ELEMENT_VALUE), + color: read.color.slice(0, MAX_ELEMENT_VALUE), + backgroundColor: read.backgroundColor.slice(0, MAX_ELEMENT_VALUE), + }; +} // fallow-ignore-next-line complexity function escapeCssIdentifier(value: string): string { diff --git a/packages/core/src/runtime/editingPort.ts b/packages/core/src/runtime/editingPort.ts index 657acf8bf8..5d66027615 100644 --- a/packages/core/src/runtime/editingPort.ts +++ b/packages/core/src/runtime/editingPort.ts @@ -77,12 +77,15 @@ export { buildStableSelector, escapeCssString, getPreferredClassSelector, + isEditableTextLeaf, readElementSource, + readTextLayer, EDITING_ELEMENT_HANDLE, MAX_DESCRIBE_HANDLES, MAX_ELEMENT_ENTRIES, MAX_ELEMENT_TEXT, MAX_ELEMENT_VALUE, + MAX_TIMELINE_TEXT, MAX_POINT_STACK, MAX_QUERY_MATCHES, MAX_SOURCE_GROUPS, @@ -92,6 +95,7 @@ export { type EditingElementMatch, type EditingElementSource, type EditingRect, + type TextLayerRead, } from "./editingElement"; export { elementCornerPoints, diff --git a/packages/core/src/runtime/timeline.test.ts b/packages/core/src/runtime/timeline.test.ts index 13c3251476..7f4c0bf80e 100644 --- a/packages/core/src/runtime/timeline.test.ts +++ b/packages/core/src/runtime/timeline.test.ts @@ -136,6 +136,31 @@ describe("collectRuntimeTimelinePayload", () => { expect(result.clips[0].kind).toBe("element"); }); + it("sends a plain text layer's words and look, capped, and no text for any other layer", () => { + const root = document.createElement("div"); + root.setAttribute("data-composition-id", "main"); + root.setAttribute("data-duration", "10"); + document.body.appendChild(root); + const title = document.createElement("h1"); + title.id = "title"; + title.setAttribute("data-start", "0"); + title.setAttribute("data-duration", "5"); + title.style.color = "rgb(255, 255, 255)"; + title.textContent = ` Hello\n ${"x".repeat(500)} `; + const card = document.createElement("div"); + card.id = "card"; + card.setAttribute("data-start", "0"); + card.setAttribute("data-duration", "5"); + card.innerHTML = " Caption"; + root.append(title, card); + + const [titleClip, cardClip] = collectRuntimeTimelinePayload(defaultParams).clips; + + expect(titleClip.text?.value).toBe(`Hello ${"x".repeat(194)}`); + expect(titleClip.text?.color).toBe("rgb(255, 255, 255)"); + expect(cardClip.text).toBeNull(); + }); + it("parses inline z-index for timeline clips", () => { const root = document.createElement("div"); root.setAttribute("data-composition-id", "main"); diff --git a/packages/core/src/runtime/timeline.ts b/packages/core/src/runtime/timeline.ts index f37af327c1..94757fc876 100644 --- a/packages/core/src/runtime/timeline.ts +++ b/packages/core/src/runtime/timeline.ts @@ -28,6 +28,7 @@ import { isSceneLikeCompositionId } from "../slideshow/index.js"; import { COMPOSITION_CONTRACT_VERSION } from "../compositionContract.js"; import { runtimeProtocolMetadata } from "./protocol.js"; import { isElementNode, isMediaElement } from "./domRealm"; +import { readSentTextLayer } from "./editingElement"; import { LOOP_INFLATED_TIMELINE_SECONDS } from "./compositionLength"; import { cutToHostSlots } from "../mediaTiming"; @@ -502,6 +503,7 @@ export function collectRuntimeTimelinePayload(params: { timelineLabel: node.getAttribute("data-timeline-label"), timelineGroup: node.getAttribute("data-timeline-group"), timelinePriority: parseNum(node.getAttribute("data-timeline-priority")), + text: readSentTextLayer(node), }; }; const addClip = (clip: RuntimeTimelineClip) => { @@ -632,6 +634,7 @@ export function collectRuntimeTimelinePayload(params: { timelineLabel: el.getAttribute("data-timeline-label"), timelineGroup: el.getAttribute("data-timeline-group"), timelinePriority: parseNum(el.getAttribute("data-timeline-priority")), + text: readSentTextLayer(el), }); gsapClipIds.add(el.id); } @@ -689,6 +692,7 @@ export function collectRuntimeTimelinePayload(params: { timelineLabel: el.getAttribute("data-timeline-label"), timelineGroup: el.getAttribute("data-timeline-group"), timelinePriority: parseNum(el.getAttribute("data-timeline-priority")), + text: readSentTextLayer(el), }); gsapClipIds.add(el.id); } diff --git a/packages/core/src/runtime/types.ts b/packages/core/src/runtime/types.ts index 1b0caff6fb..c529a9a2d2 100644 --- a/packages/core/src/runtime/types.ts +++ b/packages/core/src/runtime/types.ts @@ -1,6 +1,7 @@ import type { HfColorGradingTarget } from "../colorGrading"; import type { PreviewTarget } from "../studioPreviewMark"; import type { RuntimeAnalyticsEvent } from "./analytics"; +import type { TextLayerRead } from "./editingElement"; import type { AudioFades } from "../audioFade"; import type { RateSpec } from "../speedRamp"; import type { VolumeKeyframe } from "./mediaVolumeEnvelope"; @@ -97,6 +98,7 @@ export type RuntimeTimelineClip = RuntimeTimelineClipIdentity & { timelineLabel: string | null; timelineGroup: string | null; timelinePriority: number | null; + text: TextLayerRead | null; }; export type RuntimeTimelineScene = { diff --git a/packages/studio/src/components/editor/domEditingDom.ts b/packages/studio/src/components/editor/domEditingDom.ts index ddc8ad3e9e..b6a9775cec 100644 --- a/packages/studio/src/components/editor/domEditingDom.ts +++ b/packages/studio/src/components/editor/domEditingDom.ts @@ -8,6 +8,7 @@ import { buildStableSelector, escapeCssString, getPreferredClassSelector, + isEditableTextLeaf, isElementVisibleThroughAncestors, readElementSource, type EditingElementSource, @@ -34,13 +35,7 @@ export function isHtmlElement(value: unknown): value is HTMLElement { // module's public surface (6 studio callers import parsePx from it). export { parsePx } from "@hyperframes/core/editing"; -export function isTextBearingTag(tagName: string): boolean { - return ["div", "span", "p", "strong", "h1", "h2", "h3", "h4", "h5", "h6"].includes(tagName); -} - -export function isEditableTextLeaf(el: HTMLElement): boolean { - return isTextBearingTag(el.tagName.toLowerCase()) && el.children.length === 0; -} +export { isEditableTextLeaf }; export { getLogicalOpacity }; // One addressing rule for Studio and the runtime that describes elements to it. diff --git a/packages/studio/src/player/components/TextClipContent.test.ts b/packages/studio/src/player/components/TextClipContent.test.ts index 99b8719f09..94a6361d28 100644 --- a/packages/studio/src/player/components/TextClipContent.test.ts +++ b/packages/studio/src/player/components/TextClipContent.test.ts @@ -1,5 +1,7 @@ +import { createElement } from "react"; +import { renderToStaticMarkup } from "react-dom/server"; import { describe, expect, it } from "vitest"; -import { textClipBackground } from "./TextClipContent"; +import { TextClipContent, textClipBackground } from "./TextClipContent"; describe("textClipBackground", () => { it("puts dark text on a light strip and light text on a dark one", () => { @@ -17,3 +19,16 @@ describe("textClipBackground", () => { ).toBe("rgb(230, 57, 70)"); }); }); + +describe("TextClipContent", () => { + it("shows a film's words as plain text, never as markup", () => { + const value = 'Title'; + const html = renderToStaticMarkup(createElement(TextClipContent, { text: { value } })); + + expect(html).not.toContain(""); + expect(html).toContain( + "<img src="x" onerror="alert(1)"><b>Title</b>", + ); + }); +}); diff --git a/packages/studio/src/player/lib/playbackTypes.ts b/packages/studio/src/player/lib/playbackTypes.ts index 72e35e50a3..d42916cbb5 100644 --- a/packages/studio/src/player/lib/playbackTypes.ts +++ b/packages/studio/src/player/lib/playbackTypes.ts @@ -41,6 +41,7 @@ export interface TimelineLike { } export interface ClipManifestClip extends RuntimeTimelineClipIdentity { + text?: unknown; zIndex?: number; stackingContextId?: string | null; compositionAncestors?: string[]; diff --git a/packages/studio/src/player/lib/timelineDOM.test.ts b/packages/studio/src/player/lib/timelineDOM.test.ts index 6e3af94ce3..9764ccce9c 100644 --- a/packages/studio/src/player/lib/timelineDOM.test.ts +++ b/packages/studio/src/player/lib/timelineDOM.test.ts @@ -11,6 +11,7 @@ import { clipTimingStart, resolveClipTimingBasis } from "../../hooks/gsapShared" import { toAuthoredStart } from "../store/timelineElement"; import { computeResizePreview } from "../components/timelineClipDragPreview"; import type { TimelineElement } from "../store/playerStore"; +import { readTimelineText } from "./timelineText"; import { MAX_PLAYBACK_RATE, MIN_PLAYBACK_RATE, @@ -314,6 +315,66 @@ describe("parseTimelineFromDOM — hfId from data-hf-id", () => { }); }); +describe("a sealed preview's text rows", () => { + const LOOK = { + fontFamily: "Inter", + fontWeight: "700", + color: "rgb(255, 255, 255)", + backgroundColor: "rgba(0, 0, 0, 0)", + }; + const rowFor = (text: unknown) => + createTimelineElementFromManifestClip({ + clip: { + id: "title", + label: "Title", + kind: "element", + tagName: "h1", + start: 0, + duration: 5, + track: 0, + compositionId: null, + parentCompositionId: null, + compositionSrc: null, + assetUrl: null, + text, + }, + fallbackIndex: 0, + }); + + it("draws a text layer from the words and look in the runtime's clip message", () => { + expect(rowFor({ value: "Title", ...LOOK }).text).toEqual({ + value: "Title", + fontFamily: "Inter", + fontWeight: "700", + color: "rgb(255, 255, 255)", + background: undefined, + }); + }); + + it("keeps the clip and drops a text field that is missing, not text, or over the runtime's cap", () => { + for (const text of [ + { value: "Title" }, + { ...LOOK, value: 5 }, + { ...LOOK, value: "x".repeat(201) }, + { ...LOOK, value: "Title", color: "x".repeat(2_001) }, + "Title", + ]) { + const row = rowFor(text); + expect(row.id).toBe("title"); + expect(row.text).toBeUndefined(); + } + }); + + it("leaves an open preview's own text rows uncapped", () => { + const title = document.createElement("h1"); + title.textContent = "x".repeat(500); + document.body.appendChild(title); + + expect(readTimelineText(title)?.value).toHaveLength(500); + title.remove(); + }); +}); + describe("group info cache", () => { const parseMember = (doc: Document) => createTimelineElementFromManifestClip({ diff --git a/packages/studio/src/player/lib/timelineDOM.ts b/packages/studio/src/player/lib/timelineDOM.ts index ffa1db6550..f584cf512b 100644 --- a/packages/studio/src/player/lib/timelineDOM.ts +++ b/packages/studio/src/player/lib/timelineDOM.ts @@ -17,6 +17,7 @@ import { readMasterFxChain } from "@hyperframes/core/audio-groups"; import { linkScopeOf } from "@hyperframes/core/media-link"; import { createRuntimeStartTimeResolver } from "@hyperframes/core/runtime/start-resolver"; import { groupInfoFor } from "./timelineGroupInfo"; +import { receivedTimelineText } from "./timelineText"; import { transitionLabelsForDocument } from "./timelineTransitionMetadata"; import { resolveMediaElement, @@ -178,6 +179,7 @@ export function createTimelineElementFromManifestClip(params: { entry.zIndex = readTimelineElementZIndex(hostEl); } if (clip.assetUrl) entry.src = clip.assetUrl; + entry.text ??= receivedTimelineText(clip.text); if (clip.kind === "composition" && clip.compositionId) { entry.playbackStart ??= 0; entry.playbackRate ??= 1; diff --git a/packages/studio/src/player/lib/timelineText.ts b/packages/studio/src/player/lib/timelineText.ts index b21a7d5eeb..2d8a08b209 100644 --- a/packages/studio/src/player/lib/timelineText.ts +++ b/packages/studio/src/player/lib/timelineText.ts @@ -1,33 +1,45 @@ -import { isHtmlElement } from "@hyperframes/core/runtime/dom-realm"; +import { + MAX_ELEMENT_VALUE, + MAX_TIMELINE_TEXT, + readTextLayer, + type TextLayerRead, +} from "@hyperframes/core/runtime/editing-port"; import { parseCssColor } from "../../components/editor/colorValue"; import type { TimelineText } from "../store/timelineElement"; -import { - getCuratedComputedStyles, - isEditableTextLeaf, - isTextBearingTag, -} from "../../components/editor/domEditingDom"; -/** A layer that paints only text and a flat colour (a text tag whose children are all text leaves, - * no background image): its words and look. Anything painted beyond that keeps its picture. */ -export function readTimelineText(el: Element): TimelineText | undefined { - if (!isHtmlElement(el) || !isTextBearingTag(el.localName)) return undefined; - const children = Array.from(el.children); - if (!children.every((child) => isHtmlElement(child) && isEditableTextLeaf(child))) - return undefined; - const value = el.textContent?.replace(/\s+/g, " ").trim(); - if (!value) return undefined; - const style = getCuratedComputedStyles(el); - if ((style["background-image"] ?? "none") !== "none") return undefined; - const background = style["background-color"]; +function timelineTextOf(read: TextLayerRead | null): TimelineText | undefined { + if (!read) return undefined; + const { backgroundColor, ...look } = read; return { - value, - fontFamily: style["font-family"], - fontWeight: style["font-weight"], - color: style.color, - background: background && parseCssColor(background)?.alpha === 1 ? background : undefined, + ...look, + background: parseCssColor(backgroundColor)?.alpha === 1 ? backgroundColor : undefined, }; } +const isSentText = (value: unknown, max: number): value is string => + typeof value === "string" && value.length <= max; + +/** The runtime's text read from a clip message: null unless every field is text within the runtime's caps. */ +function receivedTextLayer(raw: unknown): TextLayerRead | null { + if (typeof raw !== "object" || raw === null) return null; + const { value, fontFamily, fontWeight, color, backgroundColor } = raw as Record; + return isSentText(value, MAX_TIMELINE_TEXT) && + isSentText(fontFamily, MAX_ELEMENT_VALUE) && + isSentText(fontWeight, MAX_ELEMENT_VALUE) && + isSentText(color, MAX_ELEMENT_VALUE) && + isSentText(backgroundColor, MAX_ELEMENT_VALUE) + ? { value, fontFamily, fontWeight, color, backgroundColor } + : null; +} + +export function readTimelineText(el: Element): TimelineText | undefined { + return timelineTextOf(readTextLayer(el)); +} + +export function receivedTimelineText(raw: unknown): TimelineText | undefined { + return timelineTextOf(receivedTextLayer(raw)); +} + export function sameTimelineText(a?: TimelineText, b?: TimelineText): boolean { if (!a || !b) return a === b; return (