Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 158 additions & 0 deletions pkgs/id/web/src/world-surface.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,164 @@ describe("world screen presenter", () => {
}
});

it("measures the #1581 remainder classes like Rust unicode-width 0.2.2", () => {
const root = document.createElement("div");
const expectWide = (symbol: string) => {
const screen: ScreenSurface = {
kind: "screen",
version: 1,
width: 2,
height: 1,
cells: [
{ symbol, fg: 0, bg: 0, bold: false, columns: 2 },
{ symbol: "", fg: 0, bg: 0, bold: false, columns: 0 },
],
};
expect(renderWorldSurface(root, screen)).toBe(true);
expect(renderWorldSurface(root, makeScreen([{ symbol, fg: 0, bg: 0, bold: false, columns: 1 }]))).toBe(false);
};
// ⬛ U+2B1B, ⬜ U+2B1C, ⭕ U+2B55: wide in unicode-width, previously
// missing from WIDE_RANGES and measured 1.
for (const symbol of ["\u{2B1B}", "\u{2B1C}", "\u{2B55}"]) expectWide(symbol);
// ☝🏻 U+261D U+1F3FB: a modifier sequence measures 2 even on a
// narrow (width-1) base.
expectWide("\u{261D}\u{1F3FB}");
// Composed Hangul jamo: LV U+1100 U+1161 and LVT + U+11A8 measure
// as the precomposed syllable — 2, not the per-component sum.
expectWide("\u{1100}\u{1161}");
expectWide("\u{1100}\u{1161}\u{11A8}");

// A lone spacing mark (U+0903, category Mc) measures 1, not 0:
// renders in 1 column and is refused in 2.
expect(renderWorldSurface(root, makeScreen([{ symbol: "\u{0903}", fg: 0, bg: 0, bold: false, columns: 1 }]))).toBe(
true,
);
const markInWide: ScreenSurface = {
kind: "screen",
version: 1,
width: 2,
height: 1,
cells: [
{ symbol: "\u{0903}", fg: 0, bg: 0, bold: false, columns: 2 },
{ symbol: "", fg: 0, bg: 0, bold: false, columns: 0 },
],
};
expect(renderWorldSurface(root, markInWide)).toBe(false);

// Spacing marks with Grapheme_Extend measure 0 in a cluster, like
// unicode-width 0.2: Bengali কা (U+0995 U+09BE) and Oriya କା
// (U+0B15 U+0B3E) are single graphemes measuring 1 column in total
// — they render in 1 column and are refused in 2.
for (const symbol of ["\u{0995}\u{09BE}", "\u{0B15}\u{0B3E}"]) {
expect(renderWorldSurface(root, makeScreen([{ symbol, fg: 0, bg: 0, bold: false, columns: 1 }]))).toBe(true);
const clusterInWide: ScreenSurface = {
kind: "screen",
version: 1,
width: 2,
height: 1,
cells: [
{ symbol, fg: 0, bg: 0, bold: false, columns: 2 },
{ symbol: "", fg: 0, bg: 0, bold: false, columns: 0 },
],
};
expect(renderWorldSurface(root, clusterInWide)).toBe(false);
}

// One grapheme of 18 UTF-16 units (e + 17 combining acutes, width
// 1) renders: there is no symbol-length cap, matching the Rust
// validator.
const longGrapheme = `e${"\u0301".repeat(17)}`;
expect(longGrapheme.length).toBe(18);
expect(renderWorldSurface(root, makeScreen([{ symbol: longGrapheme, fg: 0, bg: 0, bold: false, columns: 1 }]))).toBe(
true,
);
});

it("measures U+FE0E text-presentation sequences as width 1, like Rust unicode-width 0.2.2 (#1581)", () => {
const root = document.createElement("div");
const expectNarrow = (symbol: string) => {
expect(renderWorldSurface(root, makeScreen([{ symbol, fg: 0, bg: 0, bold: false, columns: 1 }]))).toBe(true);
const wideScreen: ScreenSurface = {
kind: "screen",
version: 1,
width: 2,
height: 1,
cells: [
{ symbol, fg: 0, bg: 0, bold: false, columns: 2 },
{ symbol: "", fg: 0, bg: 0, bold: false, columns: 0 },
],
};
expect(renderWorldSurface(root, wideScreen)).toBe(false);
};
// Default-emoji-presentation bases narrow to 1 with U+FE0E (VS15):
// ⬛︎ U+2B1B, ⬜︎ U+2B1C, ⭕︎ U+2B55 (the classes the wide fix added),
// ⌚︎ U+231A, ⭐︎ U+2B50, and astral 🕐︎ U+1F550.
for (const symbol of [
"\u{2B1B}\u{FE0E}",
"\u{2B1C}\u{FE0E}",
"\u{2B55}\u{FE0E}",
"\u{231A}\u{FE0E}",
"\u{2B50}\u{FE0E}",
"\u{1F550}\u{FE0E}",
// A trailing ZWJ does not undo the narrowing: the ZWJ shortcut
// must apply text-presentation narrowing first, so
// U+2B1B U+FE0E U+200D still measures 1 (Codex review on #2945).
"\u{2B1B}\u{FE0E}\u{200D}",
"\u{1F550}\u{FE0E}\u{200D}",
]) {
expectNarrow(symbol);
}
// The same bases alone (no U+FE0E) are wide: ⌚ U+231A and
// ⭐ U+2B50 render in 2 columns and are refused in 1.
for (const symbol of ["\u{231A}", "\u{2B50}"]) {
const screen: ScreenSurface = {
kind: "screen",
version: 1,
width: 2,
height: 1,
cells: [
{ symbol, fg: 0, bg: 0, bold: false, columns: 2 },
{ symbol: "", fg: 0, bg: 0, bold: false, columns: 0 },
],
};
expect(renderWorldSurface(root, screen)).toBe(true);
expect(renderWorldSurface(root, makeScreen([{ symbol, fg: 0, bg: 0, bold: false, columns: 1 }]))).toBe(false);
}
// A combining mark between the base and U+FE0E breaks the narrowing:
// U+2B1B U+0301 U+FE0E still measures 2 under unicode-width 0.2.2.
const interrupted = "\u{2B1B}\u{0301}\u{FE0E}";
const interruptedScreen: ScreenSurface = {
kind: "screen",
version: 1,
width: 2,
height: 1,
cells: [
{ symbol: interrupted, fg: 0, bg: 0, bold: false, columns: 2 },
{ symbol: "", fg: 0, bg: 0, bold: false, columns: 0 },
],
};
expect(renderWorldSurface(root, interruptedScreen)).toBe(true);
expect(renderWorldSurface(root, makeScreen([{ symbol: interrupted, fg: 0, bg: 0, bold: false, columns: 1 }]))).toBe(
false,
);
// Wide code points outside the text-presentation set keep width 2
// with U+FE0E: 😀︎ U+1F600 U+FE0E and 漢︎ U+6F22 U+FE0E.
for (const symbol of ["\u{1F600}\u{FE0E}", "\u{6F22}\u{FE0E}"]) {
const screen: ScreenSurface = {
kind: "screen",
version: 1,
width: 2,
height: 1,
cells: [
{ symbol, fg: 0, bg: 0, bold: false, columns: 2 },
{ symbol: "", fg: 0, bg: 0, bold: false, columns: 0 },
],
};
expect(renderWorldSurface(root, screen)).toBe(true);
expect(renderWorldSurface(root, makeScreen([{ symbol, fg: 0, bg: 0, bold: false, columns: 1 }]))).toBe(false);
}
});

it("measures astral-plane, ZWJ and variation-selector glyphs as width 2, like Rust unicode-width (#761)", () => {
const root = document.createElement("div");
// Each of these is exactly one grapheme whose Rust unicode-width 0.2 width is 2:
Expand Down
Loading