|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { createGhostClone } from './clone.js'; |
| 3 | +import { getFlashColors } from './tracker.js'; |
| 4 | + |
| 5 | +function createMockStyle(): any { |
| 6 | + const store: Record<string, string> = {}; |
| 7 | + const styleObj = { |
| 8 | + setProperty(name: string, value: string) { |
| 9 | + store[name] = value; |
| 10 | + const camel = name.replace(/-([a-z])/g, (_, g) => g.toUpperCase()); |
| 11 | + store[camel] = value; |
| 12 | + (styleObj as any)[camel] = value; |
| 13 | + }, |
| 14 | + }; |
| 15 | + return new Proxy(styleObj, { |
| 16 | + get(target, prop: string) { |
| 17 | + if (prop in target) return (target as any)[prop]; |
| 18 | + return store[prop]; |
| 19 | + }, |
| 20 | + set(_target, prop: string, value: string) { |
| 21 | + store[prop] = value; |
| 22 | + const kebab = prop.replace(/[A-Z]/g, (m) => `-${m.toLowerCase()}`); |
| 23 | + store[kebab] = value; |
| 24 | + return true; |
| 25 | + }, |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +class MockNode { |
| 30 | + public nodeType: number; |
| 31 | + public textContent: string; |
| 32 | + public parentNode: MockNode | null = null; |
| 33 | + public parentElement: MockElement | null = null; |
| 34 | + public childNodes: MockNode[] = []; |
| 35 | + |
| 36 | + constructor(nodeType: number, textContent = '') { |
| 37 | + this.nodeType = nodeType; |
| 38 | + this.textContent = textContent; |
| 39 | + } |
| 40 | + |
| 41 | + public appendChild(child: MockNode) { |
| 42 | + child.parentNode = this; |
| 43 | + if (this instanceof MockElement) { |
| 44 | + child.parentElement = this; |
| 45 | + } |
| 46 | + this.childNodes.push(child); |
| 47 | + } |
| 48 | + |
| 49 | + public replaceChild(newChild: MockNode, oldChild: MockNode) { |
| 50 | + const index = this.childNodes.indexOf(oldChild); |
| 51 | + if (index !== -1) { |
| 52 | + newChild.parentNode = this; |
| 53 | + if (this instanceof MockElement) { |
| 54 | + newChild.parentElement = this; |
| 55 | + } |
| 56 | + this.childNodes[index] = newChild; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public contains(target: MockNode): boolean { |
| 61 | + if (target === this) return true; |
| 62 | + for (const child of this.childNodes) { |
| 63 | + if (child.contains(target)) return true; |
| 64 | + } |
| 65 | + return false; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +class MockElement extends MockNode { |
| 70 | + public tagName: string; |
| 71 | + public style = createMockStyle(); |
| 72 | + public attributes: Record<string, string> = {}; |
| 73 | + public isConnected = true; |
| 74 | + |
| 75 | + constructor(tagName: string) { |
| 76 | + super(1, ''); |
| 77 | + this.tagName = tagName.toUpperCase(); |
| 78 | + } |
| 79 | + |
| 80 | + public setAttribute(name: string, value: string) { |
| 81 | + this.attributes[name] = value; |
| 82 | + } |
| 83 | + |
| 84 | + public getAttribute(name: string): string | null { |
| 85 | + return this.attributes[name] ?? null; |
| 86 | + } |
| 87 | + |
| 88 | + public removeAttribute(name: string) { |
| 89 | + delete this.attributes[name]; |
| 90 | + } |
| 91 | + |
| 92 | + public getBoundingClientRect() { |
| 93 | + return { |
| 94 | + left: 10, |
| 95 | + top: 20, |
| 96 | + width: 100, |
| 97 | + height: 30, |
| 98 | + bottom: 50, |
| 99 | + right: 110, |
| 100 | + x: 10, |
| 101 | + y: 20, |
| 102 | + toJSON: () => {}, |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + public cloneNode(deep = true): MockElement { |
| 107 | + const clone = new (this.constructor as any)(this.tagName); |
| 108 | + clone.attributes = { ...this.attributes }; |
| 109 | + Object.assign(clone.style, this.style); |
| 110 | + if (deep) { |
| 111 | + for (const child of this.childNodes) { |
| 112 | + const childClone = (child as any).cloneNode ? (child as any).cloneNode(true) : child; |
| 113 | + clone.appendChild(childClone); |
| 114 | + } |
| 115 | + } |
| 116 | + return clone; |
| 117 | + } |
| 118 | + |
| 119 | + public querySelectorAll(selector: string): MockElement[] { |
| 120 | + const results: MockElement[] = []; |
| 121 | + const walk = (node: MockNode) => { |
| 122 | + for (const child of node.childNodes) { |
| 123 | + if (child instanceof MockElement) { |
| 124 | + if (selector === '*' || (selector === '[id]' && child.getAttribute('id'))) { |
| 125 | + results.push(child); |
| 126 | + } else if (selector.toLowerCase() === child.tagName.toLowerCase()) { |
| 127 | + results.push(child); |
| 128 | + } |
| 129 | + walk(child); |
| 130 | + } |
| 131 | + } |
| 132 | + }; |
| 133 | + walk(this); |
| 134 | + return results; |
| 135 | + } |
| 136 | + |
| 137 | + public querySelector(selector: string): MockElement | null { |
| 138 | + return this.querySelectorAll(selector)[0] ?? null; |
| 139 | + } |
| 140 | + |
| 141 | + public closest(selector: string): MockElement | null { |
| 142 | + if (selector.toLowerCase() === this.tagName.toLowerCase()) return this; |
| 143 | + return this.parentElement ? this.parentElement.closest(selector) : null; |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +class MockSVGElement extends MockElement { |
| 148 | + public ownerSVGElement: MockSVGElement | null = null; |
| 149 | +} |
| 150 | + |
| 151 | +class MockTextNode extends MockNode { |
| 152 | + constructor(text: string) { |
| 153 | + super(3, text); |
| 154 | + } |
| 155 | + |
| 156 | + public cloneNode(): MockTextNode { |
| 157 | + return new MockTextNode(this.textContent); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +describe('createGhostClone', () => { |
| 162 | + const originalDocument = globalThis.document; |
| 163 | + const originalWindow = globalThis.window; |
| 164 | + const originalSVGElement = (globalThis as any).SVGElement; |
| 165 | + const originalHTMLElement = (globalThis as any).HTMLElement; |
| 166 | + |
| 167 | + beforeEach(() => { |
| 168 | + (globalThis as any).HTMLElement = MockElement; |
| 169 | + (globalThis as any).SVGElement = MockSVGElement; |
| 170 | + (globalThis as any).NodeFilter = { SHOW_ALL: 1 }; |
| 171 | + |
| 172 | + const mockDoc: any = { |
| 173 | + createElement: (tag: string) => new MockElement(tag), |
| 174 | + createTreeWalker: (root: MockNode) => { |
| 175 | + const all: MockNode[] = []; |
| 176 | + const collect = (n: MockNode) => { |
| 177 | + all.push(n); |
| 178 | + for (const c of n.childNodes) collect(c); |
| 179 | + }; |
| 180 | + collect(root); |
| 181 | + let idx = 0; |
| 182 | + return { |
| 183 | + currentNode: all[0], |
| 184 | + nextNode: () => { |
| 185 | + idx++; |
| 186 | + return all[idx] ?? null; |
| 187 | + }, |
| 188 | + }; |
| 189 | + }, |
| 190 | + }; |
| 191 | + |
| 192 | + const mockWin: any = { |
| 193 | + getComputedStyle: (_el: any) => ({ |
| 194 | + fontFamily: 'ui-monospace, monospace', |
| 195 | + fontSize: '14px', |
| 196 | + fontWeight: '500', |
| 197 | + fontStyle: 'normal', |
| 198 | + lineHeight: '20px', |
| 199 | + letterSpacing: '0px', |
| 200 | + textTransform: 'none', |
| 201 | + textAlign: 'left', |
| 202 | + textIndent: '0px', |
| 203 | + whiteSpace: 'nowrap', |
| 204 | + wordBreak: 'normal', |
| 205 | + tabSize: '2', |
| 206 | + boxSizing: 'border-box', |
| 207 | + paddingTop: '4px', |
| 208 | + paddingRight: '8px', |
| 209 | + paddingBottom: '4px', |
| 210 | + paddingLeft: '8px', |
| 211 | + borderRadius: '6px', |
| 212 | + display: 'block', |
| 213 | + stroke: 'none', |
| 214 | + fill: 'none', |
| 215 | + strokeWidth: '0px', |
| 216 | + }), |
| 217 | + }; |
| 218 | + |
| 219 | + globalThis.document = mockDoc; |
| 220 | + globalThis.window = mockWin; |
| 221 | + }); |
| 222 | + |
| 223 | + afterEach(() => { |
| 224 | + globalThis.document = originalDocument; |
| 225 | + globalThis.window = originalWindow; |
| 226 | + (globalThis as any).SVGElement = originalSVGElement; |
| 227 | + (globalThis as any).HTMLElement = originalHTMLElement; |
| 228 | + }); |
| 229 | + |
| 230 | + it('returns null when element has 0 width or height', () => { |
| 231 | + const el = new MockElement('div'); |
| 232 | + vi.spyOn(el, 'getBoundingClientRect').mockReturnValue({ |
| 233 | + left: 0, |
| 234 | + top: 0, |
| 235 | + width: 0, |
| 236 | + height: 0, |
| 237 | + bottom: 0, |
| 238 | + right: 0, |
| 239 | + x: 0, |
| 240 | + y: 0, |
| 241 | + toJSON: () => {}, |
| 242 | + }); |
| 243 | + |
| 244 | + expect(createGhostClone(el as unknown as Node, 1)).toBeNull(); |
| 245 | + }); |
| 246 | + |
| 247 | + it('gives SVGs text-like ghost treatment with stroke/fill/color flashing', () => { |
| 248 | + const svg = new MockSVGElement('svg'); |
| 249 | + svg.setAttribute('stroke', 'currentColor'); |
| 250 | + const path = new MockSVGElement('path'); |
| 251 | + path.ownerSVGElement = svg; |
| 252 | + svg.appendChild(path); |
| 253 | + |
| 254 | + const colors = getFlashColors(1); |
| 255 | + const clone = createGhostClone(svg as unknown as Node, 1); |
| 256 | + expect(clone).not.toBeNull(); |
| 257 | + expect(clone).toBeInstanceOf(MockSVGElement); |
| 258 | + |
| 259 | + const svgClone = clone as unknown as MockSVGElement; |
| 260 | + expect(svgClone.style.background).toBe('transparent'); |
| 261 | + expect(svgClone.style.outline).toBe('none'); |
| 262 | + expect(svgClone.style.color).toBe(colors.color); |
| 263 | + expect(svgClone.style.stroke).toBe(colors.color); |
| 264 | + }); |
| 265 | + |
| 266 | + it('finds SVG root and flashes icon when an inner path updates', () => { |
| 267 | + const svg = new MockSVGElement('svg'); |
| 268 | + const path = new MockSVGElement('path'); |
| 269 | + path.setAttribute('fill', 'currentColor'); |
| 270 | + path.ownerSVGElement = svg; |
| 271 | + svg.appendChild(path); |
| 272 | + |
| 273 | + const colors = getFlashColors(2); |
| 274 | + const clone = createGhostClone(path as unknown as Node, 2); |
| 275 | + expect(clone).not.toBeNull(); |
| 276 | + expect(clone).toBeInstanceOf(MockSVGElement); |
| 277 | + expect((clone as any).tagName.toLowerCase()).toBe('svg'); |
| 278 | + |
| 279 | + const svgClone = clone as unknown as MockSVGElement; |
| 280 | + expect(svgClone.style.color).toBe(colors.color); |
| 281 | + const pathClone = svgClone.querySelector('path'); |
| 282 | + expect(pathClone?.style.fill).toBe(colors.color); |
| 283 | + }); |
| 284 | + |
| 285 | + it('strips ids, names, frame sources and autoplay from the clone', () => { |
| 286 | + const div = new MockElement('div'); |
| 287 | + div.setAttribute('id', 'panel'); |
| 288 | + const radio = new MockElement('input'); |
| 289 | + radio.setAttribute('name', 'choice'); |
| 290 | + const frame = new MockElement('iframe'); |
| 291 | + frame.setAttribute('src', 'https://example.com'); |
| 292 | + const video = new MockElement('video'); |
| 293 | + video.setAttribute('autoplay', ''); |
| 294 | + div.appendChild(radio); |
| 295 | + div.appendChild(frame); |
| 296 | + div.appendChild(video); |
| 297 | + |
| 298 | + const clone = createGhostClone(div as unknown as Node, 1) as unknown as MockElement; |
| 299 | + expect(clone.getAttribute('id')).toBeNull(); |
| 300 | + expect(clone.querySelector('input')?.getAttribute('name')).toBeNull(); |
| 301 | + expect(clone.querySelector('iframe')?.getAttribute('src')).toBe('about:blank'); |
| 302 | + expect(clone.querySelector('video')?.getAttribute('autoplay')).toBeNull(); |
| 303 | + |
| 304 | + expect(div.getAttribute('id')).toBe('panel'); |
| 305 | + expect(radio.getAttribute('name')).toBe('choice'); |
| 306 | + }); |
| 307 | + |
| 308 | + it('colors text inside div clones with the same update color and keeps box outline/tint', () => { |
| 309 | + const div = new MockElement('div'); |
| 310 | + const span = new MockElement('span'); |
| 311 | + span.textContent = '// entry-server.tsx'; |
| 312 | + div.appendChild(span); |
| 313 | + |
| 314 | + const colors = getFlashColors(1); |
| 315 | + const clone = createGhostClone(div as unknown as Node, 1); |
| 316 | + expect(clone).not.toBeNull(); |
| 317 | + expect(clone).toBeInstanceOf(MockElement); |
| 318 | + |
| 319 | + const htmlClone = clone as unknown as MockElement; |
| 320 | + expect(htmlClone.style.backgroundColor).toBe(colors.background); |
| 321 | + expect(htmlClone.style.outline).toBe(`1.5px solid ${colors.color}`); |
| 322 | + expect(htmlClone.style.color).toBe(colors.color); |
| 323 | + expect(htmlClone.style.fontFamily).toBe('ui-monospace, monospace'); |
| 324 | + |
| 325 | + const spanClone = htmlClone.querySelector('span'); |
| 326 | + expect(spanClone).not.toBeNull(); |
| 327 | + expect(spanClone?.style.color).toBe(colors.color); |
| 328 | + }); |
| 329 | + |
| 330 | + it('preserves text layout while highlighting only target text for text nodes', () => { |
| 331 | + const p = new MockElement('p'); |
| 332 | + p.appendChild(new MockTextNode('Prefix ')); |
| 333 | + const target = new MockTextNode('Counter 42'); |
| 334 | + p.appendChild(target); |
| 335 | + p.appendChild(new MockTextNode(' Suffix')); |
| 336 | + |
| 337 | + const colors = getFlashColors(1); |
| 338 | + const clone = createGhostClone(target as unknown as Node, 1); |
| 339 | + expect(clone).not.toBeNull(); |
| 340 | + |
| 341 | + const htmlClone = clone as unknown as MockElement; |
| 342 | + expect(htmlClone.style.background).toBe('transparent'); |
| 343 | + expect(htmlClone.style.outline).toBe('none'); |
| 344 | + expect(htmlClone.style.fontFamily).toBe('ui-monospace, monospace'); |
| 345 | + |
| 346 | + const spans = htmlClone.querySelectorAll('span'); |
| 347 | + const highlighted = spans.find((s) => s.style.color === colors.color); |
| 348 | + expect(highlighted).toBeDefined(); |
| 349 | + expect(highlighted?.textContent).toBe('Counter 42'); |
| 350 | + expect(highlighted?.style.color).toBe(colors.color); |
| 351 | + }); |
| 352 | +}); |
0 commit comments