Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
fix(core): a picked heading is named by its text, not just Heading (#4545)
Miguel Ángel22eb0ce5e59e
diff --git a/packages/core/src/runtime/picker.test.ts b/packages/core/src/runtime/picker.test.tsindex f8a74cc743..7f9e1c11cd 100644--- a/packages/core/src/runtime/picker.test.ts+++ b/packages/core/src/runtime/picker.test.ts@@ -42,11 +42,11 @@ function emulateHitTest(painted: () => Element[]): () => void {Object.defineProperty(document, "elementsFromPoint", { configurable: true, value: original });}-// The selectors getCandidatesAtPoint(10, 10) returns while the hit test yields `stack`.-function candidatesUnder(stack: () => Element[]): string[] {+// One field of each candidate getCandidatesAtPoint(10, 10) returns while the hit test yields `stack`.+function candidatesUnder(stack: () => Element[], field = "selector"): string[] {const restore = emulateHitTest(stack);try {- return (window as any).__HF_PICKER_API.getCandidatesAtPoint(10, 10).map((c: any) => c.selector);+ return (window as any).__HF_PICKER_API.getCandidatesAtPoint(10, 10).map((c: any) => c[field]);} finally {restore();}@@ -139,6 +139,21 @@ describe("createPickerModule", () => {expect(api.isActive()).toBe(false);});+ it("labels a heading by its text, and by its role only when it has none", () => {+ const picker = createPickerModule({ postMessage: createMockPostMessage() });+ picker.installPickerApi();+ const title = document.createElement("h2");+ title.textContent = " Launch \n day ";+ const empty = document.createElement("h4");+ const long = document.createElement("h1");+ long.textContent = `${"x".repeat(54)}😀 and more`;+ document.body.append(title, empty, long);++ expect(candidatesUnder(() => [title], "label")).toEqual(["Launch day"]);+ expect(candidatesUnder(() => [empty], "label")).toEqual(["Heading"]);+ expect(candidatesUnder(() => [long], "label")).toEqual([`${"x".repeat(54)}😀…`]);+ });+it("getCandidatesAtPoint returns empty for invalid coords", () => {const picker = createPickerModule({ postMessage: createMockPostMessage() });picker.installPickerApi();diff --git a/packages/core/src/runtime/picker.ts b/packages/core/src/runtime/picker.tsindex 1d198aeea0..7e6b409736 100644--- a/packages/core/src/runtime/picker.ts+++ b/packages/core/src/runtime/picker.ts@@ -197,11 +197,13 @@ export function createPickerModule(deps: PickerModuleDeps): PickerModule {function buildElementLabel(el: Element): string {const tag = el.tagName.toLowerCase();const text = (el.textContent ?? "").trim().replace(/\s+/g, " ");- const trimLabel = (value: string, maxChars: number) =>- value.length > maxChars ? `${value.slice(0, maxChars - 1)}…` : value;- if (tag === "h1" || tag === "h2" || tag === "h3") return "Heading";- if (tag === "p" || tag === "span" || tag === "div")- return text.length > 0 ? trimLabel(text, 56) : "Text";+ const trimLabel = (value: string, maxChars: number) => {+ const chars = Array.from(value);+ return chars.length > maxChars ? `${chars.slice(0, maxChars - 1).join("")}…` : value;+ };+ const heading = /^h[1-6]$/.test(tag);+ if (heading || tag === "p" || tag === "span" || tag === "div")+ return text.length > 0 ? trimLabel(text, 56) : heading ? "Heading" : "Text";if (tag === "img") return "Image";if (tag === "video") return "Video";if (tag === "audio") return "Audio";