Skip to content

Commit da10573

Browse files
committed
fix(web): declare namespaces on generated spreadsheet columns
1 parent 0e88e8f commit da10573

2 files changed

Lines changed: 89 additions & 72 deletions

File tree

‎apps/web/src/components/files/AttachmentFilePreview.browser.test.tsx‎

Lines changed: 82 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,79 +1038,90 @@ it.each(["utf-16le", "utf-16be", "windows-1252"])(
10381038
);
10391039

10401040
it.each([
1041-
["", "worksheets/./sheet1.xml", false],
1042-
["x:", "../xl/worksheets/sheet1.xml", false],
1043-
["", "/xl/worksheets/sheet1.xml", true],
1044-
])("preserves %sworksheet metadata with relationship target %s", async (prefix, target, empty) => {
1045-
const { default: JSZip } = await import("jszip");
1046-
const archive = new JSZip();
1047-
const ns = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";
1048-
archive.file(
1049-
"xl/workbook.xml",
1050-
'<workbook xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets><sheet name="Chart" sheetId="2" r:id="c"/><sheet name="Sheet" sheetId="1" r:id="s"/></sheets></workbook>',
1051-
);
1052-
archive.file(
1053-
"xl/_rels/workbook.xml.rels",
1054-
`<Relationships><Relationship Id="s" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="${target}"/><Relationship Id="c" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet" Target="chartsheets/sheet1.xml"/></Relationships>`,
1055-
);
1056-
archive.file(
1057-
"xl/worksheets/sheet1.xml",
1058-
`<${prefix}worksheet xmlns${prefix ? ":x" : ""}="${ns}"><${prefix}dimension ref="XFD1048576"/><${prefix}sheetViews><${prefix}sheetView showGridLines="0"/></${prefix}sheetViews><${prefix}sheetFormatPr defaultColWidth="30"/><${prefix}cols><${prefix}col min="${OFFICE_PREVIEW_MAX_COLUMNS - 1}" max="${OFFICE_PREVIEW_MAX_COLUMNS + 10}" width="20"/><${prefix}col min="${OFFICE_PREVIEW_MAX_COLUMNS + 1}" max="${OFFICE_PREVIEW_MAX_COLUMNS + 30}" hidden="1"/></${prefix}cols><${prefix}sheetData><${prefix}row ht="42" customHeight="1" hidden="1"><${prefix}c r="A50" t="inlineStr"><${prefix}is><${prefix}t>${"value".repeat(1000)}</${prefix}t></${prefix}is></${prefix}c></${prefix}row></${prefix}sheetData><${prefix}mergeCells><${prefix}mergeCell ref="XFD1048576"/></${prefix}mergeCells></${prefix}worksheet>`
1059-
.replace(/<((?:x:)?(?:col|dimension|mergeCell))\b([^>]*)\/>/g, prefix ? "<$1$2></$1>" : "$&")
1060-
.replace(/<sheetData>[\s\S]*?<\/sheetData>/, (data) => (empty ? "<sheetData/>" : data))
1061-
.replace(/="([^"]*)"/g, (attribute, value: string) =>
1062-
prefix || empty
1063-
? ` = '${value.replace(/\d/g, (digit) => `&#${digit.charCodeAt(0)};`)}'`
1064-
: attribute,
1065-
),
1066-
);
1067-
const bytes = await archive.generateAsync({ type: "arraybuffer" });
1068-
const worker = new Worker(new URL("./officePreview.worker.ts", import.meta.url), {
1069-
type: "module",
1070-
});
1071-
try {
1072-
const result = await new Promise<OfficeWorkerResult>((resolve) => {
1073-
worker.addEventListener(
1074-
"message",
1075-
(event: MessageEvent<OfficeWorkerResult>) => resolve(event.data),
1076-
{ once: true },
1077-
);
1078-
worker.postMessage({ type: "open", kind: "xlsx", bytes }, [bytes]);
1079-
});
1080-
expect(result.type).toBe("xlsx");
1081-
if (result.type !== "xlsx") throw new Error("Expected bounded workbook");
1082-
// The engine counts the chart sheet before it.
1083-
expect(result.view?.hiddenGridlines).toEqual([1]);
1084-
const bounded = await JSZip.loadAsync(result.bytes);
1085-
const sheet = new DOMParser().parseFromString(
1086-
await bounded.file("xl/worksheets/sheet1.xml")!.async("string"),
1087-
"application/xml",
1041+
["", "worksheets/./sheet1.xml", false, false],
1042+
["x:", "../xl/worksheets/sheet1.xml", false, false],
1043+
["", "/xl/worksheets/sheet1.xml", true, false],
1044+
["", "worksheets/sheet1.xml", false, true],
1045+
["", "worksheets/sheet1.xml", true, true],
1046+
])(
1047+
"preserves %sworksheet metadata with relationship target %s",
1048+
async (prefix, target, empty, localPrefix) => {
1049+
const { default: JSZip } = await import("jszip");
1050+
const archive = new JSZip();
1051+
const ns = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";
1052+
archive.file(
1053+
"xl/workbook.xml",
1054+
'<workbook xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets><sheet name="Chart" sheetId="2" r:id="c"/><sheet name="Sheet" sheetId="1" r:id="s"/></sheets></workbook>',
10881055
);
1089-
expect(sheet.querySelector("parsererror")).toBeNull();
1090-
const row = sheet.getElementsByTagNameNS(ns, "row")[0];
1091-
if (empty) expect(row).toBeUndefined();
1092-
else {
1093-
expect(row!.getAttribute("ht")).toBe("42");
1094-
expect(row!.getAttribute("hidden")).toBe("1");
1095-
expect(row!.getElementsByTagNameNS(ns, "c")[0]!.getAttribute("r")).toBe("A50");
1096-
expect(row!.getElementsByTagNameNS(ns, "t")[0]!.textContent).toHaveLength(4096);
1056+
archive.file(
1057+
"xl/_rels/workbook.xml.rels",
1058+
`<Relationships><Relationship Id="s" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="${target}"/><Relationship Id="c" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet" Target="chartsheets/sheet1.xml"/></Relationships>`,
1059+
);
1060+
archive.file(
1061+
"xl/worksheets/sheet1.xml",
1062+
`<${prefix}worksheet xmlns${prefix ? ":x" : ""}="${ns}"><${prefix}dimension ref="XFD1048576"/><${prefix}sheetViews><${prefix}sheetView showGridLines="0"/></${prefix}sheetViews><${prefix}sheetFormatPr defaultColWidth="30"/><${prefix}cols><${prefix}col min="${OFFICE_PREVIEW_MAX_COLUMNS - 1}" max="${OFFICE_PREVIEW_MAX_COLUMNS + 10}" width="20"/><${prefix}col min="${OFFICE_PREVIEW_MAX_COLUMNS + 1}" max="${OFFICE_PREVIEW_MAX_COLUMNS + 30}" hidden="1"/></${prefix}cols><${prefix}sheetData><${prefix}row ht="42" customHeight="1" hidden="1"><${prefix}c r="A50" t="inlineStr"><${prefix}is><${prefix}t>${"value".repeat(1000)}</${prefix}t></${prefix}is></${prefix}c></${prefix}row></${prefix}sheetData><${prefix}mergeCells><${prefix}mergeCell ref="XFD1048576"/></${prefix}mergeCells></${prefix}worksheet>`
1063+
.replace(
1064+
/<((?:x:)?(?:col|dimension|mergeCell))\b([^>]*)\/>/g,
1065+
prefix ? "<$1$2></$1>" : "$&",
1066+
)
1067+
.replace(/<sheetData>[\s\S]*?<\/sheetData>/, (data) => (empty ? "<sheetData/>" : data))
1068+
.replace(/<(\/?)sheetData\b/g, (tag, closing: string) =>
1069+
localPrefix ? `<${closing}x:sheetData${closing ? "" : ` xmlns:x="${ns}"`}` : tag,
1070+
)
1071+
.replace(/="([^"]*)"/g, (attribute, value: string) =>
1072+
prefix || empty
1073+
? ` = '${value.replace(/\d/g, (digit) => `&#${digit.charCodeAt(0)};`)}'`
1074+
: attribute,
1075+
),
1076+
);
1077+
const bytes = await archive.generateAsync({ type: "arraybuffer" });
1078+
const worker = new Worker(new URL("./officePreview.worker.ts", import.meta.url), {
1079+
type: "module",
1080+
});
1081+
try {
1082+
const result = await new Promise<OfficeWorkerResult>((resolve) => {
1083+
worker.addEventListener(
1084+
"message",
1085+
(event: MessageEvent<OfficeWorkerResult>) => resolve(event.data),
1086+
{ once: true },
1087+
);
1088+
worker.postMessage({ type: "open", kind: "xlsx", bytes }, [bytes]);
1089+
});
1090+
expect(result.type).toBe("xlsx");
1091+
if (result.type !== "xlsx") throw new Error("Expected bounded workbook");
1092+
// The engine counts the chart sheet before it.
1093+
expect(result.view?.hiddenGridlines).toEqual([1]);
1094+
const bounded = await JSZip.loadAsync(result.bytes);
1095+
const sheet = new DOMParser().parseFromString(
1096+
await bounded.file("xl/worksheets/sheet1.xml")!.async("string"),
1097+
"application/xml",
1098+
);
1099+
expect(sheet.querySelector("parsererror")).toBeNull();
1100+
const row = sheet.getElementsByTagNameNS(ns, "row")[0];
1101+
if (empty) expect(row).toBeUndefined();
1102+
else {
1103+
expect(row!.getAttribute("ht")).toBe("42");
1104+
expect(row!.getAttribute("hidden")).toBe("1");
1105+
expect(row!.getElementsByTagNameNS(ns, "c")[0]!.getAttribute("r")).toBe("A50");
1106+
expect(row!.getElementsByTagNameNS(ns, "t")[0]!.textContent).toHaveLength(4096);
1107+
}
1108+
expect(sheet.getElementsByTagNameNS(ns, "dimension")).toHaveLength(0);
1109+
expect(sheet.getElementsByTagNameNS(ns, "mergeCell")).toHaveLength(0);
1110+
const columns = [...sheet.getElementsByTagNameNS(ns, "col")].map((column) =>
1111+
["min", "max", "width"].map((name) => column.getAttribute(name)),
1112+
);
1113+
// The engine ignores `defaultColWidth`, so the columns it sizes are spelled out.
1114+
expect(columns).toEqual([
1115+
[String(OFFICE_PREVIEW_MAX_COLUMNS - 1), String(OFFICE_PREVIEW_MAX_COLUMNS), "20"],
1116+
// Hidden columns keep their span past the limit, so they stay hidden.
1117+
[String(OFFICE_PREVIEW_MAX_COLUMNS + 1), String(OFFICE_PREVIEW_MAX_COLUMNS + 30), null],
1118+
["1", String(OFFICE_PREVIEW_MAX_COLUMNS - 2), "30"],
1119+
]);
1120+
} finally {
1121+
worker.terminate();
10971122
}
1098-
expect(sheet.getElementsByTagNameNS(ns, "dimension")).toHaveLength(0);
1099-
expect(sheet.getElementsByTagNameNS(ns, "mergeCell")).toHaveLength(0);
1100-
const columns = [...sheet.getElementsByTagNameNS(ns, "col")].map((column) =>
1101-
["min", "max", "width"].map((name) => column.getAttribute(name)),
1102-
);
1103-
// The engine ignores `defaultColWidth`, so the columns it sizes are spelled out.
1104-
expect(columns).toEqual([
1105-
[String(OFFICE_PREVIEW_MAX_COLUMNS - 1), String(OFFICE_PREVIEW_MAX_COLUMNS), "20"],
1106-
// Hidden columns keep their span past the limit, so they stay hidden.
1107-
[String(OFFICE_PREVIEW_MAX_COLUMNS + 1), String(OFFICE_PREVIEW_MAX_COLUMNS + 30), null],
1108-
["1", String(OFFICE_PREVIEW_MAX_COLUMNS - 2), "30"],
1109-
]);
1110-
} finally {
1111-
worker.terminate();
1112-
}
1113-
});
1123+
},
1124+
);
11141125

11151126
it("reports the final slide at maximum scroll after page visibility changes", async () => {
11161127
const { OfficePages } = await import("./office/OfficePages");

‎apps/web/src/components/files/officeArchive.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ function boundedWorksheet(
225225
let dataStart = 0;
226226
let dataTag = 0;
227227
let dataPrefix = "";
228+
let dataNamespace = "";
228229
let dataEnd = 0;
229230
let defaultWidth: number | undefined;
230231
let gridlines: boolean | undefined;
@@ -304,6 +305,10 @@ function boundedWorksheet(
304305
dataStart = parser.position;
305306
dataTag = xml.lastIndexOf("<", parser.position - 1);
306307
dataPrefix = tag.prefix ? `${tag.prefix}:` : "";
308+
dataNamespace = tag.uri
309+
.replaceAll("&", "&amp;")
310+
.replaceAll('"', "&quot;")
311+
.replaceAll("<", "&lt;");
307312
emptyData = tag.isSelfClosing;
308313
}
309314
// A dimension past the limits loses nothing by itself; the cells past them say whether content is lost.
@@ -460,7 +465,8 @@ function boundedWorksheet(
460465
}
461466
if (next <= OFFICE_PREVIEW_MAX_COLUMNS)
462467
defaultColumns += `<${dataPrefix}col min="${next}" max="${OFFICE_PREVIEW_MAX_COLUMNS}" width="${defaultWidth}"/>`;
463-
if (defaultColumns) defaultColumns = `<${dataPrefix}cols>${defaultColumns}</${dataPrefix}cols>`;
468+
if (defaultColumns)
469+
defaultColumns = `<${dataPrefix}cols xmlns${dataPrefix ? `:${dataPrefix.slice(0, -1)}` : ""}="${dataNamespace}">${defaultColumns}</${dataPrefix}cols>`;
464470
}
465471
const head = (part: string) =>
466472
metadata(part.slice(0, dataTag) + defaultColumns + part.slice(dataTag));

0 commit comments

Comments
 (0)