дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-11-28 21:42:27 +02:00
Symbols that fall back to a wider font (U+23F2/U+23F1 via STIX Two Math on macOS, drawn with negative letter-spacing) and non-PUA Nerd Font symbols were clipped by the next cell's background: only Private Use Area icons were lifted. Spans measured wider than their cell are lifted too, the class survives link-hover redraws, the rule is shared with embedded terminals, and full-width Nerd Font faces come before their Mono variants in the fallback list. Co-authored-by: dengliu <[email protected]>
141 рядки
5.2 KiB
TypeScript
141 рядки
5.2 KiB
TypeScript
// @vitest-environment jsdom
|
|
import { describe, expect, it } from "vitest";
|
|
import type { Terminal } from "@xterm/xterm";
|
|
import { ICON_SPAN_CLASS, installDomRowCache } from "./domRowCache";
|
|
|
|
/** Mirrors the shape of xterm 6's private DomRenderer closely enough for the cache. */
|
|
function fakeRendererClass() {
|
|
return class FakeDomRenderer {
|
|
lines: string[];
|
|
_rowElements: HTMLElement[];
|
|
underlines = 0;
|
|
_bufferService = {
|
|
cols: 10,
|
|
buffer: { x: 0, y: 0, ybase: 0, ydisp: 0, lines: { get: (i: number) => this.lines[i] } },
|
|
};
|
|
_coreService = { decPrivateModes: {} };
|
|
_optionsService = { rawOptions: { cursorBlink: false, cursorStyle: "block", cursorInactiveStyle: "outline" } };
|
|
dimensions = { css: { cell: { width: 8 } } };
|
|
_widthCache = {};
|
|
_rowFactory = {
|
|
defaultSpacing: 0,
|
|
createRow: (line: string) =>
|
|
line.split(" ").map((word) => {
|
|
const span = document.createElement("span");
|
|
const [text = "", spacing] = word.split("@");
|
|
span.textContent = text;
|
|
if (spacing) span.style.letterSpacing = spacing;
|
|
return span;
|
|
}),
|
|
};
|
|
|
|
constructor(lines: string[]) {
|
|
this.lines = lines;
|
|
this._rowElements = lines.map(() => document.createElement("div"));
|
|
}
|
|
|
|
renderRows(start: number, end: number) {
|
|
for (let y = start; y <= end; y++) {
|
|
this._rowElements[y]?.replaceChildren(...this._rowFactory.createRow(this.lines[y] ?? ""));
|
|
}
|
|
}
|
|
|
|
clear() {
|
|
for (const row of this._rowElements) row.replaceChildren();
|
|
}
|
|
|
|
_setCellUnderline() {
|
|
this.underlines++;
|
|
for (const [y, row] of this._rowElements.entries()) {
|
|
row.replaceChildren(...this._rowFactory.createRow((this.lines[y] ?? "").toUpperCase()));
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
function rowAt(renderer: { _rowElements: HTMLElement[] }, y: number): HTMLElement {
|
|
const row = renderer._rowElements[y];
|
|
if (!row) throw new Error(`no row ${y}`);
|
|
return row;
|
|
}
|
|
|
|
function terminalWith(renderer: object | undefined): Terminal {
|
|
return { _core: { _renderService: { _renderer: { value: renderer } } } } as unknown as Terminal;
|
|
}
|
|
|
|
describe("installDomRowCache", () => {
|
|
it("leaves unchanged rows in place and replaces changed ones", () => {
|
|
const Renderer = fakeRendererClass();
|
|
const renderer = new Renderer(["a b", "c d"]);
|
|
installDomRowCache(terminalWith(renderer));
|
|
|
|
renderer.renderRows(0, 1);
|
|
const [first, second] = renderer._rowElements.map((row) => row.firstChild);
|
|
renderer.lines[1] = "c e";
|
|
renderer.renderRows(0, 1);
|
|
|
|
expect(rowAt(renderer, 0).firstChild).toBe(first);
|
|
expect(rowAt(renderer, 1).firstChild).not.toBe(second);
|
|
expect(rowAt(renderer, 1).textContent).toBe("ce");
|
|
});
|
|
|
|
it("lifts spans with Nerd Font icons or wider-than-cell fallback glyphs over the next cell's background", () => {
|
|
const Renderer = fakeRendererClass();
|
|
// PUA icons (U+E606, U+F0035), non-PUA Nerd Font symbols (U+26A1), and fallback symbols
|
|
// measured wider than one cell (U+23F2 with negative letter-spacing) lift; tiny subpixel
|
|
// DPR rounding (-0.1px) and narrow glyphs (+1.2px) stay normal.
|
|
const renderer = new Renderer(["\uE606 py \u{F0035} \u26A1 \[email protected] [email protected] [email protected] ~/src"]);
|
|
installDomRowCache(terminalWith(renderer));
|
|
renderer.renderRows(0, 0);
|
|
const lifted = [...rowAt(renderer, 0).children].map((span) => span.classList.contains(ICON_SPAN_CLASS));
|
|
expect(lifted).toEqual([true, false, true, true, true, false, false, false]);
|
|
|
|
// Hovering a link (_setCellUnderline) rebuilds the row directly; overhanging spans stay lifted.
|
|
renderer._setCellUnderline();
|
|
const liftedOnHover = [...rowAt(renderer, 0).children].map((span) => span.classList.contains(ICON_SPAN_CLASS));
|
|
expect(liftedOnHover).toEqual([true, false, true, true, true, false, false, false]);
|
|
});
|
|
|
|
it("re-renders rows after clear() and link underlines rebuilt them", () => {
|
|
const Renderer = fakeRendererClass();
|
|
const renderer = new Renderer(["a b"]);
|
|
installDomRowCache(terminalWith(renderer));
|
|
renderer.renderRows(0, 0);
|
|
|
|
renderer.clear();
|
|
renderer.renderRows(0, 0);
|
|
expect(rowAt(renderer, 0).textContent).toBe("ab");
|
|
|
|
renderer._setCellUnderline();
|
|
expect(renderer.underlines).toBe(1);
|
|
expect(rowAt(renderer, 0).textContent).toBe("AB");
|
|
renderer.renderRows(0, 0);
|
|
expect(rowAt(renderer, 0).textContent).toBe("ab");
|
|
});
|
|
|
|
it("installs once per renderer class", () => {
|
|
const Renderer = fakeRendererClass();
|
|
const a = new Renderer(["x"]);
|
|
installDomRowCache(terminalWith(a));
|
|
const patched = Renderer.prototype.renderRows;
|
|
installDomRowCache(terminalWith(new Renderer(["y"])));
|
|
|
|
expect(Renderer.prototype.renderRows).toBe(patched);
|
|
a._setCellUnderline();
|
|
expect(a.underlines).toBe(1);
|
|
});
|
|
|
|
it("does nothing before open() or when xterm's renderer no longer matches", () => {
|
|
expect(() => installDomRowCache(terminalWith(undefined))).not.toThrow();
|
|
|
|
class OtherRenderer {
|
|
renderRows() {}
|
|
clear() {}
|
|
_setCellUnderline() {}
|
|
}
|
|
const original = OtherRenderer.prototype.renderRows;
|
|
installDomRowCache(terminalWith(new OtherRenderer()));
|
|
expect(OtherRenderer.prototype.renderRows).toBe(original);
|
|
});
|
|
});
|