The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 08:15 UTC
fix(shared): preserve surrogate pairs in truncated CSV cells (#27497)
Co-authored-by: maria-rcks <maria@kuuro.net>
maco committed
Nov 1, 2026 at 03:39 UTC
c018660572d1dbe326ef0e72d3f14dd69fb7c41f
2 files changed
+43
-2
packages/shared/src/delimitedPreview.test.ts
+33
@@ -59,6 +59,39 @@ describe("delimited file previews", () => {
59
expect(table.rows[0]?.[0]).toHaveLength(2000);
60
expect(parseDelimitedPreview('a,"unfinished', ",").truncated).toBe(true);
61
});
62
+ describe.each([",", "\t"] as const)("Unicode cells with delimiter %j", (delimiter) => {
63
+ it.each([false, true])("does not split surrogate pairs in quoted=%s cells", (quoted) => {
64
+ const prefix = "x".repeat(1999);
65
+ const value = `${prefix}😀tail`;
66
+ const input = `${quoted ? `"${value}"` : value}${delimiter}next\nlast`;
67
+ expect(parseDelimitedPreview(input, delimiter)).toEqual({
68
+ rows: [[prefix, "next"], ["last"]],
69
+ truncated: true,
70
+ });
71
+ });
72
+ it("keeps pairs that fit and truncates later characters", () => {
73
+ const value = `${"x".repeat(1998)}😀`;
74
+ expect(parseDelimitedPreview(value, delimiter)).toEqual({
75
+ rows: [[value]],
76
+ truncated: false,
77
+ });
78
+ expect(parseDelimitedPreview(`${value}tail`, delimiter)).toEqual({
79
+ rows: [[value]],
80
+ truncated: true,
81
+ });
82
+ });
83
+ it("keeps escaped quotes and BMP text at the same limit", () => {
84
+ const prefix = "x".repeat(1999);
85
+ expect(parseDelimitedPreview(`"${prefix}""tail"`, delimiter)).toEqual({
86
+ rows: [[`${prefix}"`]],
87
+ truncated: true,
88
+ });
89
+ expect(parseDelimitedPreview(`${prefix}éextra`, delimiter)).toEqual({
90
+ rows: [[`${prefix}é`]],
91
+ truncated: true,
92
+ });
93
+ });
94
+ });
95
it("reads worker pages beyond the old row, column and cell limits", () => {
96
const text = Array.from({ length: 128 }, (_, row) =>
97
Array.from({ length: 40 }, (_, column) => `${row}:${column}`).join(","),
packages/shared/src/delimitedPreview.ts
+10
-2
@@ -24,6 +24,14 @@ export function parseDelimitedPreview(
24
let truncated = false;
25
let rowStart = text.charCodeAt(0) === 0xfeff ? 1 : 0;
26
const endCell = () => {
27
+ if (cell.length > limits.maxCellLength) {
28
+ let end = limits.maxCellLength;
29
+ const before = cell.charCodeAt(end - 1);
30
+ const after = cell.charCodeAt(end);
31
+ if (before >= 0xd800 && before <= 0xdbff && after >= 0xdc00 && after <= 0xdfff) end--;
32
+ cell = cell.slice(0, end);
33
+ truncated = true;
34
+ }
35
if (row.length < limits.maxColumns) row.push(cell);
36
else truncated = true;
37
cell = "";
@@ -32,7 +40,7 @@ export function parseDelimitedPreview(
40
const char = text[index];
41
if (char === '"') {
42
if (quoted && text[index + 1] === '"') {
35
- if (cell.length < limits.maxCellLength) cell += '"';
43
+ if (cell.length <= limits.maxCellLength) cell += '"';
44
else truncated = true;
45
index++;
46
continue;
@@ -52,7 +60,7 @@ export function parseDelimitedPreview(
60
if (rows.length === limits.maxRows)
61
return { rows, truncated: truncated || index < text.length - 1 };
62
}
55
- } else if (cell.length < limits.maxCellLength) cell += char;
63
+ } else if (cell.length <= limits.maxCellLength) cell += char;
64
else truncated = true;
65
}
66
if (rowStart < text.length) {