|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 2 | +import { readMoverFrames } from "./frames"; |
| 3 | +import { type Affine, IDENTITY, translate } from "./pose"; |
| 4 | + |
| 5 | +/** Fixed layout for jsdom: an element's offset box and its client rect. */ |
| 6 | +function laidOut(el: HTMLElement, w: number, h: number, rect: [number, number, number, number]) { |
| 7 | + Object.defineProperty(el, "offsetWidth", { value: w, configurable: true }); |
| 8 | + Object.defineProperty(el, "offsetHeight", { value: h, configurable: true }); |
| 9 | + const [left, top, right, bottom] = rect; |
| 10 | + el.getBoundingClientRect = () => |
| 11 | + ({ left, top, right, bottom, width: right - left, height: bottom - top }) as DOMRect; |
| 12 | +} |
| 13 | + |
| 14 | +const rect = ([left, top, right, bottom]: [number, number, number, number]) => |
| 15 | + ({ left, top, right, bottom, width: right - left, height: bottom - top }) as DOMRect; |
| 16 | + |
| 17 | +/** jsdom has no Range rects: the text inside a node measures as `of(node)`. */ |
| 18 | +function textRects(of: (node: Node) => [number, number, number, number]) { |
| 19 | + let node: Node | null = null; |
| 20 | + const proto = Range.prototype as unknown as Record<string, unknown>; |
| 21 | + proto.selectNodeContents = (n: Node) => { |
| 22 | + node = n; |
| 23 | + }; |
| 24 | + proto.getBoundingClientRect = () => rect(node ? of(node) : [0, 0, 0, 0]); |
| 25 | +} |
| 26 | + |
| 27 | +function read(mover: HTMLElement, scene: HTMLElement, pose: Affine) { |
| 28 | + return readMoverFrames({ |
| 29 | + movers: [mover], |
| 30 | + reader: { pose: () => pose, reset: () => {} } as never, |
| 31 | + seek: () => {}, |
| 32 | + t: 0.5, |
| 33 | + reads: 2, |
| 34 | + shutterSeconds: 1 / 45, |
| 35 | + taps: 4, |
| 36 | + frameW: 1920, |
| 37 | + frameH: 1080, |
| 38 | + scene, |
| 39 | + }).frames[0]; |
| 40 | +} |
| 41 | + |
| 42 | +const RANGE_STUBS = ["selectNodeContents", "getBoundingClientRect"]; |
| 43 | +const realRange = RANGE_STUBS.map( |
| 44 | + (k) => (Range.prototype as unknown as Record<string, unknown>)[k], |
| 45 | +); |
| 46 | + |
| 47 | +describe("readMoverFrames", () => { |
| 48 | + beforeEach(() => textRects(() => [0, 0, 0, 0])); |
| 49 | + afterEach(() => { |
| 50 | + RANGE_STUBS.forEach((k, i) => { |
| 51 | + (Range.prototype as unknown as Record<string, unknown>)[k] = realRange[i]; |
| 52 | + }); |
| 53 | + document.body.innerHTML = ""; |
| 54 | + }); |
| 55 | + |
| 56 | + // A wordmark in a 540 px box whose one-line text runs to 1528 px: the box is the layout, the |
| 57 | + // text is the picture. Cropping to the box drew "HyperFra" for "HyperFrames Studio". |
| 58 | + it("sizes the redraw to content that overflows the mover's box", () => { |
| 59 | + const scene = document.createElement("div"); |
| 60 | + const brand = document.createElement("div"); |
| 61 | + const word = document.createElement("div"); |
| 62 | + brand.append(word); |
| 63 | + scene.append(brand); |
| 64 | + document.body.append(scene); |
| 65 | + laidOut(scene, 1920, 1080, [0, 0, 1920, 1080]); |
| 66 | + laidOut(brand, 540, 240, [100, 50, 640, 290]); |
| 67 | + laidOut(word, 1528, 240, [100, 50, 1628, 290]); |
| 68 | + const pose = translate(IDENTITY, 100, 50); |
| 69 | + const reader = { pose: () => pose, reset: () => {} }; |
| 70 | + let x = 0; |
| 71 | + const { frames } = readMoverFrames({ |
| 72 | + movers: [brand], |
| 73 | + reader: reader as never, |
| 74 | + seek: (t) => { |
| 75 | + x = t; |
| 76 | + }, |
| 77 | + t: 0.5, |
| 78 | + reads: 2, |
| 79 | + shutterSeconds: 1 / 45, |
| 80 | + taps: 4, |
| 81 | + frameW: 1920, |
| 82 | + frameH: 1080, |
| 83 | + scene, |
| 84 | + }); |
| 85 | + expect(x).toBe(0.5); |
| 86 | + expect(frames[0]?.w).toBeGreaterThanOrEqual(1528); |
| 87 | + expect(frames[0]?.h).toBeGreaterThanOrEqual(240); |
| 88 | + }); |
| 89 | + |
| 90 | + it("keeps the box as it is when its content stays inside", () => { |
| 91 | + const scene = document.createElement("div"); |
| 92 | + const box = document.createElement("div"); |
| 93 | + const child = document.createElement("span"); |
| 94 | + box.append(child); |
| 95 | + scene.append(box); |
| 96 | + document.body.append(scene); |
| 97 | + laidOut(scene, 1920, 1080, [0, 0, 1920, 1080]); |
| 98 | + laidOut(box, 200, 100, [10, 10, 210, 110]); |
| 99 | + laidOut(child, 50, 20, [20, 20, 70, 40]); |
| 100 | + const pose = translate(IDENTITY, 10, 10); |
| 101 | + const { frames } = readMoverFrames({ |
| 102 | + movers: [box], |
| 103 | + reader: { pose: () => pose, reset: () => {} } as never, |
| 104 | + seek: () => {}, |
| 105 | + t: 0.5, |
| 106 | + reads: 2, |
| 107 | + shutterSeconds: 1 / 45, |
| 108 | + taps: 4, |
| 109 | + frameW: 1920, |
| 110 | + frameH: 1080, |
| 111 | + scene, |
| 112 | + }); |
| 113 | + expect(frames[0]).toMatchObject({ w: 200, h: 100, now: pose }); |
| 114 | + }); |
| 115 | + // A block child is as wide as the mover however long its text: only the text's own rects show it. |
| 116 | + it("sizes the redraw to text that overflows a block child", () => { |
| 117 | + const scene = document.createElement("div"); |
| 118 | + const brand = document.createElement("div"); |
| 119 | + const word = document.createElement("div"); |
| 120 | + word.textContent = "HyperFrames Studio"; |
| 121 | + brand.append(word); |
| 122 | + scene.append(brand); |
| 123 | + document.body.append(scene); |
| 124 | + laidOut(scene, 1920, 1080, [0, 0, 1920, 1080]); |
| 125 | + laidOut(brand, 540, 110, [100, 50, 640, 160]); |
| 126 | + laidOut(word, 540, 110, [100, 50, 640, 160]); |
| 127 | + textRects(() => [100, 50, 981, 160]); |
| 128 | + expect(read(brand, scene, translate(IDENTITY, 100, 50))?.w).toBe(881); |
| 129 | + }); |
| 130 | + |
| 131 | + // Upright client rects of a rotated mover's children cover its corners, not its content. |
| 132 | + it("keeps a rotated mover's box when its content stays inside", () => { |
| 133 | + const scene = document.createElement("div"); |
| 134 | + const box = document.createElement("div"); |
| 135 | + const fill = document.createElement("div"); |
| 136 | + box.append(fill); |
| 137 | + scene.append(box); |
| 138 | + document.body.append(scene); |
| 139 | + const c = Math.SQRT1_2; |
| 140 | + const upright: [number, number, number, number] = [1059, 600, 1341, 883]; |
| 141 | + laidOut(scene, 1920, 1080, [0, 0, 1920, 1080]); |
| 142 | + laidOut(box, 200, 200, upright); |
| 143 | + laidOut(fill, 200, 200, upright); |
| 144 | + textRects(() => upright); |
| 145 | + const frame = read(box, scene, { a: c, b: c, c: -c, d: c, e: 1200, f: 600 }); |
| 146 | + expect(frame).toMatchObject({ w: 200, h: 200 }); |
| 147 | + }); |
| 148 | + |
| 149 | + // Its unrotated text at local y 90-110 runs to x 260; rotated, that lands in the upright rect's corner. |
| 150 | + it("sizes a rotated mover's redraw to text that overflows it", () => { |
| 151 | + const scene = document.createElement("div"); |
| 152 | + const box = document.createElement("div"); |
| 153 | + box.setAttribute("style", "transform: rotate(45deg)"); |
| 154 | + box.textContent = "HyperFrames"; |
| 155 | + scene.append(box); |
| 156 | + document.body.append(scene); |
| 157 | + const c = Math.SQRT1_2; |
| 158 | + const rotated: Affine = { a: c, b: c, c: -c, d: c, e: 1200, f: 600 }; |
| 159 | + const unrotated = translate(IDENTITY, 1100, 600); |
| 160 | + const off = () => box.getAttribute("style")?.includes("transform: none") ?? false; |
| 161 | + laidOut(scene, 1920, 1080, [0, 0, 1920, 1080]); |
| 162 | + laidOut(box, 200, 200, [0, 0, 0, 0]); |
| 163 | + box.getBoundingClientRect = () => rect(off() ? [1100, 600, 1300, 800] : [1059, 600, 1341, 883]); |
| 164 | + textRects(() => (off() ? [1100, 690, 1360, 710] : [1122, 663, 1320, 862])); |
| 165 | + const frame = readMoverFrames({ |
| 166 | + movers: [box], |
| 167 | + reader: { pose: () => (off() ? unrotated : rotated), reset: () => {} } as never, |
| 168 | + seek: () => {}, |
| 169 | + t: 0.5, |
| 170 | + reads: 2, |
| 171 | + shutterSeconds: 1 / 45, |
| 172 | + taps: 4, |
| 173 | + frameW: 1920, |
| 174 | + frameH: 1080, |
| 175 | + scene, |
| 176 | + }).frames[0]; |
| 177 | + expect(frame).toMatchObject({ w: 260, h: 200 }); |
| 178 | + expect(box.getAttribute("style")).toBe("transform: rotate(45deg)"); |
| 179 | + }); |
| 180 | + |
| 181 | + // transform: none alone would move an absolute child to an outer containing block while measured. |
| 182 | + it("keeps a rotated mover the containing block while it is measured", () => { |
| 183 | + const scene = document.createElement("div"); |
| 184 | + const box = document.createElement("div"); |
| 185 | + const tag = document.createElement("div"); |
| 186 | + box.setAttribute("style", "transform: rotate(45deg)"); |
| 187 | + box.append(tag); |
| 188 | + scene.append(box); |
| 189 | + document.body.append(scene); |
| 190 | + const c = Math.SQRT1_2; |
| 191 | + const held = () => box.getAttribute("style")?.includes("will-change: transform") ?? false; |
| 192 | + laidOut(scene, 1920, 1080, [0, 0, 1920, 1080]); |
| 193 | + laidOut(box, 200, 200, [1100, 600, 1300, 800]); |
| 194 | + laidOut(tag, 100, 20, [0, 0, 0, 0]); |
| 195 | + tag.getBoundingClientRect = () => rect(held() ? [1400, 690, 1500, 710] : [300, 90, 400, 110]); |
| 196 | + const rotated: Affine = { a: c, b: c, c: -c, d: c, e: 1200, f: 600 }; |
| 197 | + const unrotated = translate(IDENTITY, 1100, 600); |
| 198 | + const off = () => box.getAttribute("style")?.includes("transform: none") ?? false; |
| 199 | + const frame = readMoverFrames({ |
| 200 | + movers: [box], |
| 201 | + reader: { pose: () => (off() ? unrotated : rotated), reset: () => {} } as never, |
| 202 | + seek: () => {}, |
| 203 | + t: 0.5, |
| 204 | + reads: 2, |
| 205 | + shutterSeconds: 1 / 45, |
| 206 | + taps: 4, |
| 207 | + frameW: 1920, |
| 208 | + frameH: 1080, |
| 209 | + scene, |
| 210 | + }).frames[0]; |
| 211 | + expect(frame).toMatchObject({ w: 400, h: 200, origin: [0, 0] }); |
| 212 | + }); |
| 213 | + |
| 214 | + it("leaves a mover turned only by an ancestor untouched", () => { |
| 215 | + const scene = document.createElement("div"); |
| 216 | + const box = document.createElement("div"); |
| 217 | + scene.append(box); |
| 218 | + document.body.append(scene); |
| 219 | + const c = Math.SQRT1_2; |
| 220 | + const styles: Array<string | null> = []; |
| 221 | + laidOut(scene, 1920, 1080, [0, 0, 1920, 1080]); |
| 222 | + laidOut(box, 200, 200, [1059, 600, 1341, 883]); |
| 223 | + const measure = box.getBoundingClientRect; |
| 224 | + box.getBoundingClientRect = () => { |
| 225 | + styles.push(box.getAttribute("style")); |
| 226 | + return measure(); |
| 227 | + }; |
| 228 | + read(box, scene, { a: c, b: c, c: -c, d: c, e: 1200, f: 600 }); |
| 229 | + expect(styles.length).toBeGreaterThan(0); |
| 230 | + expect(styles.every((s) => s === null)).toBe(true); |
| 231 | + }); |
| 232 | +}); |
0 commit comments