|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { parseHTML } from "linkedom"; |
| 3 | +import { inlineSubCompositions } from "./inlineSubCompositions"; |
| 4 | + |
| 5 | +// Fixtures reference GSAP CDN but are never loaded in a real browser — resolveHtml is mocked. |
| 6 | + |
| 7 | +/** |
| 8 | + * Minimal sub-composition HTML that uses `#intro` as its CSS and GSAP scope. |
| 9 | + * This is the pattern that breaks when the producer path strips the inner root. |
| 10 | + */ |
| 11 | +const SUB_COMP_HTML = `<template id="intro-template"> |
| 12 | + <div id="intro" data-composition-id="intro" data-width="1920" data-height="1080"> |
| 13 | + <div class="title" style="opacity:0;">HELLO WORLD</div> |
| 14 | + <style> |
| 15 | + #intro { position:relative; width:1920px; height:1080px; background:#111; } |
| 16 | + #intro .title { font-size:120px; color:#fff; } |
| 17 | + </style> |
| 18 | + <script> |
| 19 | + (function() { |
| 20 | + window.__timelines = window.__timelines || {}; |
| 21 | + var tl = gsap.timeline({ paused: true }); |
| 22 | + tl.fromTo('#intro .title', { opacity:0 }, { opacity:1, duration:0.5 }, 0.2); |
| 23 | + window.__timelines['intro'] = tl; |
| 24 | + })(); |
| 25 | + </script> |
| 26 | + </div> |
| 27 | +</template>`; |
| 28 | + |
| 29 | +function makeHostDocument(compId: string) { |
| 30 | + const { document } = parseHTML(`<!DOCTYPE html> |
| 31 | +<html><body> |
| 32 | + <div data-composition-id="main"> |
| 33 | + <div data-composition-id="${compId}" data-composition-src="intro.html" |
| 34 | + data-start="0" data-duration="4" data-track-index="0"></div> |
| 35 | + </div> |
| 36 | +</body></html>`); |
| 37 | + return document; |
| 38 | +} |
| 39 | + |
| 40 | +describe("inlineSubCompositions – #ID selector scoping divergence", () => { |
| 41 | + it("producer path (no flattenInnerRoot): strips inner root, losing #id attribute", () => { |
| 42 | + const document = makeHostDocument("intro"); |
| 43 | + const host = document.querySelector('[data-composition-src="intro.html"]')!; |
| 44 | + |
| 45 | + const result = inlineSubCompositions(document, [host], { |
| 46 | + resolveHtml: () => SUB_COMP_HTML, |
| 47 | + parseHtml: (html) => parseHTML(html).document, |
| 48 | + }); |
| 49 | + |
| 50 | + // The producer path takes innerHTML when compId matches, stripping the |
| 51 | + // wrapper <div id="intro" ...>. The host element should NOT contain a |
| 52 | + // child with id="intro" — the id attribute is lost. |
| 53 | + const innerRootById = host.querySelector("#intro"); |
| 54 | + expect(innerRootById).toBeNull(); |
| 55 | + |
| 56 | + // The host itself still has data-composition-id="intro" (from the |
| 57 | + // original markup), but no element inside has id="intro". |
| 58 | + expect(host.getAttribute("data-composition-id")).toBe("intro"); |
| 59 | + |
| 60 | + // CSS was scoped: #intro selectors should be rewritten to use |
| 61 | + // data-hf-authored-id attribute selector so they still resolve. |
| 62 | + const scopedCss = result.styles.join("\n"); |
| 63 | + expect(scopedCss).toContain('[data-hf-authored-id="intro"]'); |
| 64 | + expect(scopedCss).not.toContain("#intro"); |
| 65 | + }); |
| 66 | + |
| 67 | + it("producer path: scoped CSS rewrites #id selectors to [data-hf-authored-id] attribute", () => { |
| 68 | + const document = makeHostDocument("intro"); |
| 69 | + const host = document.querySelector('[data-composition-src="intro.html"]')!; |
| 70 | + |
| 71 | + const result = inlineSubCompositions(document, [host], { |
| 72 | + resolveHtml: () => SUB_COMP_HTML, |
| 73 | + parseHtml: (html) => parseHTML(html).document, |
| 74 | + }); |
| 75 | + |
| 76 | + // The CSS scoper rewrites `#intro` to `[data-hf-authored-id="intro"]` |
| 77 | + // so that the selector resolves against the flattened structure. |
| 78 | + const scopedCss = result.styles.join("\n"); |
| 79 | + expect(scopedCss).toContain('[data-hf-authored-id="intro"]'); |
| 80 | + expect(scopedCss).toContain('[data-hf-authored-id="intro"] .title'); |
| 81 | + }); |
| 82 | + |
| 83 | + it("producer path: scoped scripts rewrite #intro selectors for GSAP targets", () => { |
| 84 | + const document = makeHostDocument("intro"); |
| 85 | + const host = document.querySelector('[data-composition-src="intro.html"]')!; |
| 86 | + |
| 87 | + const result = inlineSubCompositions(document, [host], { |
| 88 | + resolveHtml: () => SUB_COMP_HTML, |
| 89 | + parseHtml: (html) => parseHTML(html).document, |
| 90 | + }); |
| 91 | + |
| 92 | + // The wrapped script should contain the authored root id normalization |
| 93 | + // logic so that runtime querySelector('#intro .title') maps to the |
| 94 | + // data-hf-authored-id attribute selector. |
| 95 | + const wrappedScript = result.scripts.join("\n"); |
| 96 | + expect(wrappedScript).toContain("__hfAuthoredRootId"); |
| 97 | + expect(wrappedScript).toContain('"intro"'); |
| 98 | + }); |
| 99 | + |
| 100 | + it("bundler path (with flattenInnerRoot): preserves inner root as a child element", () => { |
| 101 | + const document = makeHostDocument("intro"); |
| 102 | + const host = document.querySelector('[data-composition-src="intro.html"]')!; |
| 103 | + |
| 104 | + // Simulate the bundler's flattenInnerRoot: clone the element, add |
| 105 | + // data-hf-authored-id, strip timing attrs (simplified here). |
| 106 | + function flattenInnerRoot(innerRoot: Element): Element { |
| 107 | + const clone = innerRoot.cloneNode(true) as Element; |
| 108 | + const authoredId = clone.getAttribute("id"); |
| 109 | + if (authoredId) { |
| 110 | + clone.setAttribute("data-hf-authored-id", authoredId); |
| 111 | + clone.removeAttribute("id"); |
| 112 | + } |
| 113 | + clone.removeAttribute("data-start"); |
| 114 | + clone.removeAttribute("data-duration"); |
| 115 | + return clone; |
| 116 | + } |
| 117 | + |
| 118 | + const result = inlineSubCompositions(document, [host], { |
| 119 | + resolveHtml: () => SUB_COMP_HTML, |
| 120 | + parseHtml: (html) => parseHTML(html).document, |
| 121 | + flattenInnerRoot, |
| 122 | + }); |
| 123 | + |
| 124 | + // With flattenInnerRoot, the inner root is preserved as a child of the |
| 125 | + // host via outerHTML. The data-hf-authored-id attribute is present. |
| 126 | + const authoredRoot = host.querySelector('[data-hf-authored-id="intro"]'); |
| 127 | + expect(authoredRoot).not.toBeNull(); |
| 128 | + |
| 129 | + // CSS is still rewritten to use the attribute selector. |
| 130 | + const scopedCss = result.styles.join("\n"); |
| 131 | + expect(scopedCss).toContain('[data-hf-authored-id="intro"]'); |
| 132 | + }); |
| 133 | + |
| 134 | + it("producer path propagates data-hf-authored-id to host when inner root has id", () => { |
| 135 | + const document = makeHostDocument("intro"); |
| 136 | + const host = document.querySelector('[data-composition-src="intro.html"]')!; |
| 137 | + |
| 138 | + inlineSubCompositions(document, [host], { |
| 139 | + resolveHtml: () => SUB_COMP_HTML, |
| 140 | + parseHtml: (html) => parseHTML(html).document, |
| 141 | + }); |
| 142 | + |
| 143 | + // The inner root's id="intro" is stripped (innerHTML), but the producer |
| 144 | + // now propagates it as data-hf-authored-id on the host element so that |
| 145 | + // rewritten #ID selectors ([data-hf-authored-id="intro"]) resolve. |
| 146 | + expect(host.getAttribute("data-hf-authored-id")).toBe("intro"); |
| 147 | + |
| 148 | + // The original #intro element is still gone — innerHTML stripped it. |
| 149 | + const introById = host.querySelector("#intro"); |
| 150 | + expect(introById).toBeNull(); |
| 151 | + |
| 152 | + expect(host.getAttribute("data-composition-id")).toBe("intro"); |
| 153 | + }); |
| 154 | +}); |
0 commit comments