|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | | -import { removeElementFromHtml } from "./sourceMutation.js"; |
| 2 | +import { removeElementFromHtml, patchElementInHtml } from "./sourceMutation.js"; |
3 | 3 |
|
4 | 4 | describe("removeElementFromHtml", () => { |
5 | 5 | it("removes a self-closing element by id", () => { |
@@ -28,3 +28,122 @@ describe("removeElementFromHtml", () => { |
28 | 28 | expect(removeElementFromHtml(html, { id: "photo" })).toBe(`<div id="rest"></div>`); |
29 | 29 | }); |
30 | 30 | }); |
| 31 | + |
| 32 | +describe("patchElementInHtml", () => { |
| 33 | + const FIXTURE = `<!doctype html><html><head></head><body> |
| 34 | +<div id="root" data-composition-id="main"> |
| 35 | + <div class="layer" data-composition-id="overlay" data-composition-src="compositions/overlay.html"> |
| 36 | + <div class="chrome"> |
| 37 | + <span class="brand">HyperFrames</span> |
| 38 | + </div> |
| 39 | + </div> |
| 40 | + <div id="hero" class="hero-heading" style="font-size: 48px">Hello World</div> |
| 41 | +</div> |
| 42 | +</body></html>`; |
| 43 | + |
| 44 | + it("patches inline style by id", () => { |
| 45 | + const result = patchElementInHtml(FIXTURE, { id: "hero" }, [ |
| 46 | + { type: "inline-style", property: "color", value: "red" }, |
| 47 | + ]); |
| 48 | + |
| 49 | + expect(result).toMatch(/color:\s*red/); |
| 50 | + expect(result).toContain('id="hero"'); |
| 51 | + }); |
| 52 | + |
| 53 | + it("patches inline style by class selector", () => { |
| 54 | + const result = patchElementInHtml(FIXTURE, { selector: ".hero-heading" }, [ |
| 55 | + { type: "inline-style", property: "font-size", value: "72px" }, |
| 56 | + ]); |
| 57 | + |
| 58 | + expect(result).toMatch(/font-size:\s*72px/); |
| 59 | + }); |
| 60 | + |
| 61 | + it("patches data attribute", () => { |
| 62 | + const result = patchElementInHtml(FIXTURE, { id: "hero" }, [ |
| 63 | + { type: "attribute", property: "hf-studio-path-offset", value: "true" }, |
| 64 | + ]); |
| 65 | + |
| 66 | + expect(result).toContain('data-hf-studio-path-offset="true"'); |
| 67 | + }); |
| 68 | + |
| 69 | + it("patches html attribute", () => { |
| 70 | + const result = patchElementInHtml(FIXTURE, { id: "hero" }, [ |
| 71 | + { type: "html-attribute", property: "title", value: "greeting" }, |
| 72 | + ]); |
| 73 | + |
| 74 | + expect(result).toContain('title="greeting"'); |
| 75 | + }); |
| 76 | + |
| 77 | + it("patches text content", () => { |
| 78 | + const result = patchElementInHtml(FIXTURE, { id: "hero" }, [ |
| 79 | + { type: "text-content", property: "", value: "New Title" }, |
| 80 | + ]); |
| 81 | + |
| 82 | + expect(result).toContain("New Title"); |
| 83 | + expect(result).not.toContain("Hello World"); |
| 84 | + }); |
| 85 | + |
| 86 | + it("applies multiple operations in one call", () => { |
| 87 | + const result = patchElementInHtml(FIXTURE, { id: "hero" }, [ |
| 88 | + { type: "inline-style", property: "color", value: "blue" }, |
| 89 | + { type: "inline-style", property: "font-size", value: "96px" }, |
| 90 | + { type: "attribute", property: "hf-studio-path-offset", value: "true" }, |
| 91 | + ]); |
| 92 | + |
| 93 | + expect(result).toMatch(/color:\s*blue/); |
| 94 | + expect(result).toMatch(/font-size:\s*96px/); |
| 95 | + expect(result).toContain('data-hf-studio-path-offset="true"'); |
| 96 | + }); |
| 97 | + |
| 98 | + it("finds element by composition-id selector", () => { |
| 99 | + const result = patchElementInHtml(FIXTURE, { selector: '[data-composition-id="overlay"]' }, [ |
| 100 | + { type: "inline-style", property: "opacity", value: "0.5" }, |
| 101 | + ]); |
| 102 | + |
| 103 | + expect(result).toMatch(/opacity:\s*0\.5/); |
| 104 | + }); |
| 105 | + |
| 106 | + it("finds element by class with selectorIndex", () => { |
| 107 | + const html = `<div class="item">A</div><div class="item">B</div>`; |
| 108 | + const result = patchElementInHtml(html, { selector: ".item", selectorIndex: 1 }, [ |
| 109 | + { type: "text-content", property: "", value: "Changed" }, |
| 110 | + ]); |
| 111 | + |
| 112 | + expect(result).toContain("A"); |
| 113 | + expect(result).toContain("Changed"); |
| 114 | + expect(result).not.toContain(">B<"); |
| 115 | + }); |
| 116 | + |
| 117 | + it("returns unchanged html when target not found", () => { |
| 118 | + const result = patchElementInHtml(FIXTURE, { id: "nonexistent" }, [ |
| 119 | + { type: "inline-style", property: "color", value: "red" }, |
| 120 | + ]); |
| 121 | + |
| 122 | + expect(result).toBe(FIXTURE); |
| 123 | + }); |
| 124 | + |
| 125 | + it("removes inline style when value is null", () => { |
| 126 | + const result = patchElementInHtml(FIXTURE, { id: "hero" }, [ |
| 127 | + { type: "inline-style", property: "font-size", value: null }, |
| 128 | + ]); |
| 129 | + |
| 130 | + expect(result).not.toContain("font-size"); |
| 131 | + }); |
| 132 | + |
| 133 | + it("removes attribute when value is null", () => { |
| 134 | + const result = patchElementInHtml(FIXTURE, { selector: '[data-composition-id="overlay"]' }, [ |
| 135 | + { type: "html-attribute", property: "data-composition-src", value: null }, |
| 136 | + ]); |
| 137 | + |
| 138 | + expect(result).not.toContain("data-composition-src"); |
| 139 | + }); |
| 140 | + |
| 141 | + it("patches fragment html without doctype", () => { |
| 142 | + const fragment = `<div id="card" style="padding: 8px"><span>Title</span></div>`; |
| 143 | + const result = patchElementInHtml(fragment, { id: "card" }, [ |
| 144 | + { type: "inline-style", property: "padding", value: "16px" }, |
| 145 | + ]); |
| 146 | + |
| 147 | + expect(result).toMatch(/padding:\s*16px/); |
| 148 | + }); |
| 149 | +}); |
0 commit comments