|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | 2 | import { Window } from "happy-dom"; |
3 | | -import { createStudioManualEditsRenderBodyScript } from "./manualEditsRenderScript"; |
| 3 | +import { |
| 4 | + createStudioManualEditsRenderBodyScript, |
| 5 | + createStudioPositionSeekReapplyScript, |
| 6 | +} from "./manualEditsRenderScript"; |
4 | 7 |
|
5 | 8 | function runScript( |
6 | 9 | window: Window, |
@@ -380,3 +383,182 @@ describe("createStudioManualEditsRenderBodyScript", () => { |
380 | 383 | expect(card.style.getPropertyValue("translate")).toContain("--hf-studio-offset-x"); |
381 | 384 | }); |
382 | 385 | }); |
| 386 | + |
| 387 | +describe("createStudioPositionSeekReapplyScript", () => { |
| 388 | + function runPositionScript( |
| 389 | + window: Window, |
| 390 | + timers: { |
| 391 | + setInterval?: typeof globalThis.setInterval; |
| 392 | + clearInterval?: typeof globalThis.clearInterval; |
| 393 | + } = {}, |
| 394 | + ): void { |
| 395 | + Object.assign(window, { SyntaxError }); |
| 396 | + const script = createStudioPositionSeekReapplyScript(); |
| 397 | + const execute = new Function( |
| 398 | + "window", |
| 399 | + "document", |
| 400 | + "HTMLElement", |
| 401 | + "DOMMatrix", |
| 402 | + "setInterval", |
| 403 | + "clearInterval", |
| 404 | + script, |
| 405 | + ); |
| 406 | + execute( |
| 407 | + window, |
| 408 | + window.document, |
| 409 | + window.HTMLElement, |
| 410 | + globalThis.DOMMatrix, |
| 411 | + timers.setInterval ?? |
| 412 | + (((callback: TimerHandler) => { |
| 413 | + void callback; |
| 414 | + return 0 as never; |
| 415 | + }) as typeof globalThis.setInterval), |
| 416 | + timers.clearInterval ?? globalThis.clearInterval, |
| 417 | + ); |
| 418 | + } |
| 419 | + |
| 420 | + it("reapplies box-size after seek", () => { |
| 421 | + const window = new Window(); |
| 422 | + window.document.body.innerHTML = ` |
| 423 | + <div id="card" |
| 424 | + data-hf-studio-box-size="true" |
| 425 | + style="--hf-studio-width: 200px; --hf-studio-height: 100px; width: 200px; height: 100px"> |
| 426 | + </div> |
| 427 | + `; |
| 428 | + const card = window.document.getElementById("card") as unknown as HTMLElement; |
| 429 | + |
| 430 | + const originalSeek = () => { |
| 431 | + card.style.removeProperty("width"); |
| 432 | + card.style.removeProperty("height"); |
| 433 | + }; |
| 434 | + (window as unknown as { __hf: Record<string, unknown> }).__hf = { seek: originalSeek }; |
| 435 | + |
| 436 | + runPositionScript(window); |
| 437 | + const wrappedSeek = (window as unknown as { __hf: { seek: (t: number) => void } }).__hf.seek; |
| 438 | + wrappedSeek(1); |
| 439 | + |
| 440 | + expect(card.style.getPropertyValue("width")).toBe("200px"); |
| 441 | + expect(card.style.getPropertyValue("height")).toBe("100px"); |
| 442 | + }); |
| 443 | + |
| 444 | + it("strips GSAP translate from transform after reapplying path offset", () => { |
| 445 | + const window = new Window(); |
| 446 | + window.document.body.innerHTML = ` |
| 447 | + <div id="card" |
| 448 | + data-hf-studio-path-offset="true" |
| 449 | + data-hf-studio-original-translate="" |
| 450 | + style="--hf-studio-offset-x: 50px; --hf-studio-offset-y: 30px; translate: var(--hf-studio-offset-x, 0px) var(--hf-studio-offset-y, 0px)"> |
| 451 | + </div> |
| 452 | + `; |
| 453 | + const card = window.document.getElementById("card") as unknown as HTMLElement; |
| 454 | + |
| 455 | + const originalSeek = () => { |
| 456 | + card.style.setProperty("transform", "matrix(1, 0, 0, 1, 120, 60)"); |
| 457 | + }; |
| 458 | + (window as unknown as { __hf: Record<string, unknown> }).__hf = { seek: originalSeek }; |
| 459 | + |
| 460 | + runPositionScript(window); |
| 461 | + const wrappedSeek = (window as unknown as { __hf: { seek: (t: number) => void } }).__hf.seek; |
| 462 | + wrappedSeek(1); |
| 463 | + |
| 464 | + expect(card.style.getPropertyValue("translate")).toContain("--hf-studio-offset-x"); |
| 465 | + const transform = card.style.getPropertyValue("transform"); |
| 466 | + if (transform && transform !== "none") { |
| 467 | + const m = new DOMMatrix(transform); |
| 468 | + expect(m.m41).toBe(0); |
| 469 | + expect(m.m42).toBe(0); |
| 470 | + } |
| 471 | + }); |
| 472 | + |
| 473 | + it("preserves non-translate components when stripping GSAP transform", () => { |
| 474 | + const window = new Window(); |
| 475 | + window.document.body.innerHTML = ` |
| 476 | + <div id="card" |
| 477 | + data-hf-studio-path-offset="true" |
| 478 | + data-hf-studio-original-translate="" |
| 479 | + style="--hf-studio-offset-x: 10px; --hf-studio-offset-y: 20px; translate: var(--hf-studio-offset-x, 0px) var(--hf-studio-offset-y, 0px)"> |
| 480 | + </div> |
| 481 | + `; |
| 482 | + const card = window.document.getElementById("card") as unknown as HTMLElement; |
| 483 | + |
| 484 | + const originalSeek = () => { |
| 485 | + card.style.setProperty("transform", "matrix(0.5, 0, 0, 0.5, 80, 40)"); |
| 486 | + }; |
| 487 | + (window as unknown as { __hf: Record<string, unknown> }).__hf = { seek: originalSeek }; |
| 488 | + |
| 489 | + runPositionScript(window); |
| 490 | + const wrappedSeek = (window as unknown as { __hf: { seek: (t: number) => void } }).__hf.seek; |
| 491 | + wrappedSeek(1); |
| 492 | + |
| 493 | + const transform = card.style.getPropertyValue("transform"); |
| 494 | + expect(transform).toBeTruthy(); |
| 495 | + expect(transform).not.toContain("80"); |
| 496 | + expect(transform).not.toContain("40"); |
| 497 | + }); |
| 498 | + |
| 499 | + it("removes transform entirely when it becomes identity after stripping translate", () => { |
| 500 | + const window = new Window(); |
| 501 | + window.document.body.innerHTML = ` |
| 502 | + <div id="card" |
| 503 | + data-hf-studio-path-offset="true" |
| 504 | + data-hf-studio-original-translate="" |
| 505 | + style="--hf-studio-offset-x: 10px; --hf-studio-offset-y: 20px; translate: var(--hf-studio-offset-x, 0px) var(--hf-studio-offset-y, 0px)"> |
| 506 | + </div> |
| 507 | + `; |
| 508 | + const card = window.document.getElementById("card") as unknown as HTMLElement; |
| 509 | + |
| 510 | + const originalSeek = () => { |
| 511 | + card.style.setProperty("transform", "matrix(1, 0, 0, 1, 50, 25)"); |
| 512 | + }; |
| 513 | + (window as unknown as { __hf: Record<string, unknown> }).__hf = { seek: originalSeek }; |
| 514 | + |
| 515 | + runPositionScript(window); |
| 516 | + const wrappedSeek = (window as unknown as { __hf: { seek: (t: number) => void } }).__hf.seek; |
| 517 | + wrappedSeek(1); |
| 518 | + |
| 519 | + const transform = card.style.getPropertyValue("transform"); |
| 520 | + expect(!transform || transform === "none" || transform === "").toBe(true); |
| 521 | + }); |
| 522 | + |
| 523 | + it("no-ops when transform is 'none'", () => { |
| 524 | + const window = new Window(); |
| 525 | + window.document.body.innerHTML = ` |
| 526 | + <div id="card" |
| 527 | + data-hf-studio-path-offset="true" |
| 528 | + data-hf-studio-original-translate="" |
| 529 | + style="--hf-studio-offset-x: 10px; --hf-studio-offset-y: 20px; translate: var(--hf-studio-offset-x, 0px) var(--hf-studio-offset-y, 0px); transform: none"> |
| 530 | + </div> |
| 531 | + `; |
| 532 | + const card = window.document.getElementById("card") as unknown as HTMLElement; |
| 533 | + |
| 534 | + (window as unknown as { __hf: Record<string, unknown> }).__hf = { seek: () => {} }; |
| 535 | + runPositionScript(window); |
| 536 | + |
| 537 | + expect(card.style.getPropertyValue("transform")).toBe("none"); |
| 538 | + }); |
| 539 | + |
| 540 | + it("strips GSAP translate for rotation-only elements", () => { |
| 541 | + const window = new Window(); |
| 542 | + window.document.body.innerHTML = ` |
| 543 | + <div id="card" |
| 544 | + data-hf-studio-rotation="true" |
| 545 | + data-hf-studio-original-rotate="" |
| 546 | + style="--hf-studio-rotation: 45deg; rotate: var(--hf-studio-rotation, 0deg)"> |
| 547 | + </div> |
| 548 | + `; |
| 549 | + const card = window.document.getElementById("card") as unknown as HTMLElement; |
| 550 | + |
| 551 | + const originalSeek = () => { |
| 552 | + card.style.setProperty("transform", "matrix(1, 0, 0, 1, 100, 50)"); |
| 553 | + }; |
| 554 | + (window as unknown as { __hf: Record<string, unknown> }).__hf = { seek: originalSeek }; |
| 555 | + |
| 556 | + runPositionScript(window); |
| 557 | + const wrappedSeek = (window as unknown as { __hf: { seek: (t: number) => void } }).__hf.seek; |
| 558 | + wrappedSeek(1); |
| 559 | + |
| 560 | + expect(card.style.getPropertyValue("rotate")).toContain("--hf-studio-rotation"); |
| 561 | + const transform = card.style.getPropertyValue("transform"); |
| 562 | + expect(!transform || transform === "none" || transform === "").toBe(true); |
| 563 | + }); |
| 564 | +}); |
0 commit comments