|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import { buildTerminalCopyModeSnapshot } from "./copy-mode-snapshot"; |
| 4 | + |
| 5 | +function createRect(top: number, left: number, width: number, height: number): DOMRect { |
| 6 | + return { |
| 7 | + top, |
| 8 | + left, |
| 9 | + width, |
| 10 | + height, |
| 11 | + right: left + width, |
| 12 | + bottom: top + height, |
| 13 | + x: left, |
| 14 | + y: top, |
| 15 | + toJSON() {}, |
| 16 | + } as DOMRect; |
| 17 | +} |
| 18 | + |
| 19 | +function parseSnapshotHtml(html: string): HTMLDivElement { |
| 20 | + const root = document.createElement("div"); |
| 21 | + root.innerHTML = html; |
| 22 | + return root; |
| 23 | +} |
| 24 | + |
| 25 | +describe("buildTerminalCopyModeSnapshot", () => { |
| 26 | + it("clones visible xterm rows into selectable overlay markup", () => { |
| 27 | + const container = document.createElement("div"); |
| 28 | + const rowsElement = document.createElement("div"); |
| 29 | + rowsElement.className = "xterm-rows"; |
| 30 | + rowsElement.innerHTML = ` |
| 31 | + <div style="position: absolute; top: 12px; transform: translateY(12px); height: 20px; line-height: 20px;"> |
| 32 | + <span style="color: rgb(255, 0, 0); font-weight: 700; font-style: italic;">hi there</span> |
| 33 | + </div> |
| 34 | + <div style="position: absolute; top: 32px; transform: translateY(32px); height: 20px; line-height: 20px;"> |
| 35 | + <span>ab</span> |
| 36 | + </div> |
| 37 | + `; |
| 38 | + container.append(rowsElement); |
| 39 | + document.body.append(container); |
| 40 | + |
| 41 | + container.getBoundingClientRect = () => createRect(10, 20, 100, 40); |
| 42 | + rowsElement.getBoundingClientRect = () => createRect(30, 35, 80, 40); |
| 43 | + |
| 44 | + const snapshot = buildTerminalCopyModeSnapshot({ |
| 45 | + rowsElement, |
| 46 | + cols: 5, |
| 47 | + fontFamily: "JetBrains Mono", |
| 48 | + fontSize: 14, |
| 49 | + lineHeightPx: 20, |
| 50 | + }); |
| 51 | + const clonedRoot = parseSnapshotHtml(snapshot?.html ?? ""); |
| 52 | + const clonedRows = Array.from(clonedRoot.children) as HTMLElement[]; |
| 53 | + const [firstRow, secondRow] = clonedRows; |
| 54 | + const firstSpan = firstRow?.querySelector("span"); |
| 55 | + const paddingSpan = secondRow?.lastElementChild; |
| 56 | + |
| 57 | + expect(snapshot).toEqual({ |
| 58 | + html: expect.any(String), |
| 59 | + text: "hi there\nab ", |
| 60 | + fontFamily: "JetBrains Mono", |
| 61 | + fontSize: 14, |
| 62 | + lineHeightPx: 20, |
| 63 | + }); |
| 64 | + expect(clonedRows).toHaveLength(2); |
| 65 | + expect(firstRow.style.position).toBe("static"); |
| 66 | + expect(firstRow.style.top).toBe("0px"); |
| 67 | + expect(firstRow.style.left).toBe("0px"); |
| 68 | + expect(firstRow.style.transform).toBe("none"); |
| 69 | + expect(firstRow.style.height).toBe("20px"); |
| 70 | + expect(firstRow.style.lineHeight).toBe("20px"); |
| 71 | + expect(firstRow.style.whiteSpace).toBe("pre"); |
| 72 | + expect(firstSpan).not.toBeNull(); |
| 73 | + expect((firstSpan as HTMLElement).style.color).toBe("rgb(255, 0, 0)"); |
| 74 | + expect((firstSpan as HTMLElement).style.fontWeight).toBe("700"); |
| 75 | + expect((firstSpan as HTMLElement).style.fontStyle).toBe("italic"); |
| 76 | + expect(firstSpan?.textContent).toBe("hi\u00a0there"); |
| 77 | + expect(paddingSpan?.textContent).toBe("\u00a0\u00a0\u00a0"); |
| 78 | + expect(paddingSpan?.previousElementSibling?.textContent).toBe("ab"); |
| 79 | + |
| 80 | + container.remove(); |
| 81 | + }); |
| 82 | + |
| 83 | + it("pads plain text lines to terminal columns", () => { |
| 84 | + const container = document.createElement("div"); |
| 85 | + const rowsElement = document.createElement("div"); |
| 86 | + rowsElement.innerHTML = `<div><span>a</span></div>`; |
| 87 | + container.append(rowsElement); |
| 88 | + |
| 89 | + container.getBoundingClientRect = () => |
| 90 | + ({ |
| 91 | + top: 0, |
| 92 | + left: 0, |
| 93 | + width: 10, |
| 94 | + height: 10, |
| 95 | + right: 10, |
| 96 | + bottom: 10, |
| 97 | + x: 0, |
| 98 | + y: 0, |
| 99 | + toJSON() {}, |
| 100 | + }) as DOMRect; |
| 101 | + rowsElement.getBoundingClientRect = () => |
| 102 | + ({ |
| 103 | + top: 0, |
| 104 | + left: 0, |
| 105 | + width: 10, |
| 106 | + height: 10, |
| 107 | + right: 10, |
| 108 | + bottom: 10, |
| 109 | + x: 0, |
| 110 | + y: 0, |
| 111 | + toJSON() {}, |
| 112 | + }) as DOMRect; |
| 113 | + |
| 114 | + const snapshot = buildTerminalCopyModeSnapshot({ |
| 115 | + rowsElement, |
| 116 | + cols: 3, |
| 117 | + fontFamily: "mono", |
| 118 | + fontSize: 12, |
| 119 | + lineHeightPx: 18, |
| 120 | + }); |
| 121 | + |
| 122 | + expect(snapshot?.text).toBe("a "); |
| 123 | + }); |
| 124 | + |
| 125 | + it("pads full-width CJK text using terminal cell width", () => { |
| 126 | + const container = document.createElement("div"); |
| 127 | + const rowsElement = document.createElement("div"); |
| 128 | + rowsElement.innerHTML = `<div><span>你</span></div>`; |
| 129 | + container.append(rowsElement); |
| 130 | + |
| 131 | + container.getBoundingClientRect = () => createRect(0, 0, 10, 10); |
| 132 | + rowsElement.getBoundingClientRect = () => createRect(0, 0, 10, 10); |
| 133 | + |
| 134 | + const snapshot = buildTerminalCopyModeSnapshot({ |
| 135 | + rowsElement, |
| 136 | + cols: 4, |
| 137 | + fontFamily: "mono", |
| 138 | + fontSize: 12, |
| 139 | + lineHeightPx: 18, |
| 140 | + }); |
| 141 | + const clonedRoot = parseSnapshotHtml(snapshot?.html ?? ""); |
| 142 | + const row = clonedRoot.firstElementChild as HTMLElement | null; |
| 143 | + const paddingSpan = row?.lastElementChild; |
| 144 | + |
| 145 | + expect(snapshot?.text).toBe("你 "); |
| 146 | + expect(paddingSpan?.textContent).toBe("\u00a0\u00a0"); |
| 147 | + }); |
| 148 | + |
| 149 | + it("pads combining text using terminal cell width", () => { |
| 150 | + const container = document.createElement("div"); |
| 151 | + const rowsElement = document.createElement("div"); |
| 152 | + rowsElement.innerHTML = `<div><span>e\u0301</span></div>`; |
| 153 | + container.append(rowsElement); |
| 154 | + |
| 155 | + container.getBoundingClientRect = () => createRect(0, 0, 10, 10); |
| 156 | + rowsElement.getBoundingClientRect = () => createRect(0, 0, 10, 10); |
| 157 | + |
| 158 | + const snapshot = buildTerminalCopyModeSnapshot({ |
| 159 | + rowsElement, |
| 160 | + cols: 3, |
| 161 | + fontFamily: "mono", |
| 162 | + fontSize: 12, |
| 163 | + lineHeightPx: 18, |
| 164 | + }); |
| 165 | + const clonedRoot = parseSnapshotHtml(snapshot?.html ?? ""); |
| 166 | + const row = clonedRoot.firstElementChild as HTMLElement | null; |
| 167 | + const paddingSpan = row?.lastElementChild; |
| 168 | + |
| 169 | + expect(snapshot?.text).toBe("e\u0301 "); |
| 170 | + expect(paddingSpan?.textContent).toBe("\u00a0\u00a0"); |
| 171 | + }); |
| 172 | + |
| 173 | + it("returns null when rowsElement is missing", () => { |
| 174 | + const snapshot = buildTerminalCopyModeSnapshot({ |
| 175 | + rowsElement: null, |
| 176 | + cols: 80, |
| 177 | + fontFamily: "mono", |
| 178 | + fontSize: 12, |
| 179 | + lineHeightPx: 18, |
| 180 | + }); |
| 181 | + |
| 182 | + expect(snapshot).toBeNull(); |
| 183 | + }); |
| 184 | +}); |
0 commit comments