Skip to content

Commit c8e8b58

Browse files
authored
fix(web): improve Excel spreadsheet previews (#34429)
1 parent b015ffd commit c8e8b58

14 files changed

Lines changed: 534 additions & 113 deletions

‎apps/web/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"@betteroffice/fonts": "0.4.0",
1818
"@betteroffice/fonts-cjk": "0.4.0",
1919
"@betteroffice/pptx": "0.2.0",
20-
"@betteroffice/xlsx": "0.3.0",
20+
"@betteroffice/xlsx": "0.5.1",
2121
"@clerk/clerk-js": "catalog:",
2222
"@clerk/electron": "catalog:",
2323
"@clerk/react": "catalog:",

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

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

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

11081127
it("reports the final slide at maximum scroll after page visibility changes", async () => {
11091128
const { OfficePages } = await import("./office/OfficePages");
@@ -1717,7 +1736,7 @@ it.each(["repeated binary", "XML relationship alias", "duplicate relationship ID
17171736
},
17181737
);
17191738

1720-
it.each(["XFD200", "XFD1048576", "AX500", "AZ9999999"])(
1739+
it.each(["XFD200", "XFD300", "XFD1048576", "AX500", "AZ9999999"])(
17211740
"bounds spreadsheet cells at %s before padding",
17221741
async (ref) => {
17231742
const { default: JSZip } = await import("jszip");
@@ -1733,7 +1752,7 @@ it.each(["XFD200", "XFD1048576", "AX500", "AZ9999999"])(
17331752
);
17341753
archive.file(
17351754
"xl/custom.xml",
1736-
`<worksheet><dimension ref="A1:${ref}"/><sheetData><row r="1"><c r="A1"><v>7</v></c></row><row r="${ref.replace(/\D/g, "")}"><c r="${ref}"><v>9</v></c></row></sheetData></worksheet>`,
1755+
`<worksheet><dimension ref="A1:${ref}"/><sheetData><row r="1"><c r="A1"><v>7</v></c></row><row r="${ref.replace(/\D/g, "")}">${ref === "XFD300" ? `<c r="${ref}" s="1"/>` : `<c r="${ref}"><v>9</v></c>`}</row></sheetData></worksheet>`,
17371756
);
17381757
const bytes = await archive.generateAsync({ type: "arraybuffer" });
17391758
const worker = new Worker(new URL("./officePreview.worker.ts", import.meta.url), {
@@ -1763,7 +1782,7 @@ it.each(["XFD200", "XFD1048576", "AX500", "AZ9999999"])(
17631782
);
17641783
expect(sheet.querySelector('c[r="A1"] v')?.textContent).toBe("7");
17651784
expect(sheet.querySelectorAll("row").length).toBeLessThanOrEqual(500);
1766-
expect(result.truncated).toBe(ref !== "AX500");
1785+
expect(result.truncated).toBe(ref === "XFD200" || ref === "XFD300");
17671786
if (ref === "AX500") expect(sheet.querySelector('c[r="AX500"] v')?.textContent).toBe("9");
17681787
else expect(sheet.querySelector('c[r="' + ref + '"]')).toBeNull();
17691788
}
@@ -1773,6 +1792,62 @@ it.each(["XFD200", "XFD1048576", "AX500", "AZ9999999"])(
17731792
},
17741793
);
17751794

1795+
it.each(['<u val="double"/>', '<u val="double"></u>'])(
1796+
"shows workbook formats and underline %s",
1797+
async (underline) => {
1798+
const { default: JSZip } = await import("jszip");
1799+
const archive = new JSZip();
1800+
archive.file(
1801+
"xl/workbook.xml",
1802+
'<workbook xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets><sheet name="Styles" sheetId="1" r:id="rId1"/></sheets></workbook>',
1803+
);
1804+
archive.file(
1805+
"xl/_rels/workbook.xml.rels",
1806+
'<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/></Relationships>',
1807+
);
1808+
archive.file(
1809+
"xl/styles.xml",
1810+
`<styleSheet><numFmts count="4"><numFmt numFmtId="164" formatCode="0.000"/><numFmt numFmtId="65535" formatCode="0.0000"/><numFmt numFmtId="007" formatCode="&quot;¥&quot;#,##0.00"/><numFmt numFmtId="82" formatCode="[$-010409]dd-mmm-yyyy"/></numFmts><fonts count="2"><font><sz val="11"/></font><font>${underline}<sz val="11"/></font></fonts><cellXfs count="4"><xf numFmtId="0" fontId="0"/><xf numFmtId="7" fontId="0"/><xf numFmtId="82" fontId="0"/><xf numFmtId="0" fontId="1"/></cellXfs></styleSheet>`,
1811+
);
1812+
archive.file(
1813+
"xl/worksheets/sheet1.xml",
1814+
'<worksheet><sheetData><row r="1"><c r="A1" s="1"><v>100</v></c><c r="B1" s="2"><v>44986</v></c><c r="C1" s="3" t="inlineStr"><is><t>Total</t></is></c></row></sheetData></worksheet>',
1815+
);
1816+
const { prepareOffice } = await import("./officeArchive");
1817+
const prepared = await prepareOffice(
1818+
await archive.generateAsync({ type: "arraybuffer" }),
1819+
"xlsx",
1820+
);
1821+
const styles = await (
1822+
await JSZip.loadAsync(prepared.bytes)
1823+
)
1824+
.file("xl/styles.xml")!
1825+
.async("string");
1826+
expect(styles).toContain("<font><u/>");
1827+
const worker = new Worker(new URL("./office/spreadsheet.worker.ts", import.meta.url), {
1828+
type: "module",
1829+
});
1830+
try {
1831+
const { createWorkerClient } = await import("./office/worker-rpc");
1832+
const client = createWorkerClient<import("./office/sheet-protocol").SheetMethods>(worker);
1833+
const bytes = prepared.bytes;
1834+
await client.call("open", { bytes, view: prepared.view! }, { transfer: [bytes] });
1835+
const cells = await client.call("cells", {
1836+
sheet: 0,
1837+
range: { top: 0, bottom: 0, left: 0, right: 1 },
1838+
});
1839+
// Ids under 164 that a workbook defines keep its formats, not Excel's built-in ones.
1840+
expect(cells[0]).toEqual(["¥100.00", "01-Mar-2023"]);
1841+
} finally {
1842+
worker.terminate();
1843+
}
1844+
archive.file("xl/styles.xml", "<styleSheet><numFmts/><numFmts/></styleSheet>");
1845+
await expect(
1846+
prepareOffice(await archive.generateAsync({ type: "arraybuffer" }), "xlsx"),
1847+
).rejects.toThrow("duplicate stylesheet blocks");
1848+
},
1849+
);
1850+
17761851
it("keeps spreadsheet dates on their calendar day west of UTC", async () => {
17771852
const { default: JSZip } = await import("jszip");
17781853
const archive = new JSZip();
@@ -1831,7 +1906,14 @@ it("keeps spreadsheet dates on their calendar day west of UTC", async () => {
18311906
});
18321907
const { createWorkerClient } = await import("./office/worker-rpc");
18331908
const client = createWorkerClient<import("./office/sheet-protocol").SheetMethods>(worker);
1834-
await client.call("open", { bytes }, { transfer: [bytes] });
1909+
await client.call(
1910+
"open",
1911+
{
1912+
bytes,
1913+
view: { hiddenGridlines: [], themeFonts: { major: "Calibri Light", minor: "Calibri" } },
1914+
},
1915+
{ transfer: [bytes] },
1916+
);
18351917
const cells = await client.call("cells", {
18361918
sheet: 0,
18371919
range: { top: 0, bottom: 0, left: 0, right: 0 },

‎apps/web/src/components/files/OpenXmlOfficeViewer.tsx‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ export default function OfficeViewer(props: {
104104
</div>
105105
);
106106
if (!prepared) return <FileSurfaceLoading />;
107-
const Viewer =
108-
props.kind === "docx" ? WordDocument : props.kind === "xlsx" ? Spreadsheet : Presentation;
107+
const Viewer = props.kind === "docx" ? WordDocument : Presentation;
109108
return (
110109
<div className="flex min-h-0 flex-1 flex-col">
111110
{details ? (
@@ -122,7 +121,16 @@ export default function OfficeViewer(props: {
122121
</FileSurfaceNotice>
123122
) : null}
124123
<Suspense fallback={<FileSurfaceLoading />}>
125-
<Viewer bytes={prepared.bytes} onDetails={setDetails} onError={fail} />
124+
{prepared.view ? (
125+
<Spreadsheet
126+
bytes={prepared.bytes}
127+
view={prepared.view}
128+
onDetails={setDetails}
129+
onError={fail}
130+
/>
131+
) : (
132+
<Viewer bytes={prepared.bytes} onDetails={setDetails} onError={fail} />
133+
)}
126134
</Suspense>
127135
</div>
128136
);

‎apps/web/src/components/files/office/Spreadsheet.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
type CellAddr,
1515
} from "@betteroffice/xlsx/headless";
1616
import { FileSurfaceLoading, FileSurfaceNotice } from "../fileSurfaceChrome";
17+
import type { WorkbookView } from "../officePreviewProtocol";
1718
import { createWorkerClient, SupersededError } from "./worker-rpc";
1819
import type { SheetLayout, SheetMethods, SheetFrameRequest, SheetTab } from "./sheet-protocol";
1920
import {
@@ -45,10 +46,11 @@ type Opened = {
4546
};
4647
export default function Spreadsheet(props: {
4748
bytes: ArrayBuffer;
49+
view: WorkbookView;
4850
onDetails: (text: string) => void;
4951
onError: (cause: unknown) => void;
5052
}) {
51-
const { onError, bytes: source, onDetails } = props;
53+
const { onError, bytes: source, onDetails, view } = props;
5254
const [opened, setOpened] = useState<Opened>();
5355
const [error, setError] = useState("");
5456
const picking = useRef<AbortController | null>(null);
@@ -66,7 +68,7 @@ export default function Spreadsheet(props: {
6668
worker.addEventListener("messageerror", fail);
6769
const bytes = source.slice(0);
6870
void engine
69-
.call("open", { bytes }, { transfer: [bytes], signal: controller.signal })
71+
.call("open", { bytes, view }, { transfer: [bytes], signal: controller.signal })
7072
.then((value) => {
7173
if (!value) throw new Error("This workbook has no visible sheets.");
7274
setOpened({ ...value, engine });
@@ -78,7 +80,7 @@ export default function Spreadsheet(props: {
7880
picking.current?.abort();
7981
engine.close();
8082
};
81-
}, [source, onDetails, onError]);
83+
}, [source, view, onDetails, onError]);
8284
const show = (sheet: number, target?: CellAddr) => {
8385
if (!opened) return;
8486
picking.current?.abort();

‎apps/web/src/components/files/office/fonts.browser.test.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BUNDLED_FONTS } from "@betteroffice/fonts";
22
import { describe, expect, it } from "vite-plus/test";
3-
import { wordFonts } from "./fonts";
3+
import { sheetFace, wordFonts } from "./fonts";
44

55
const notoSansJp = BUNDLED_FONTS.find((face) => face.file === "NotoSansJP-Regular.otf")!;
66
const fonts = wordFonts();
@@ -16,3 +16,14 @@ describe("wordFonts", () => {
1616
expect(bytes?.byteLength).toBe(notoSansJp.byteLength);
1717
});
1818
});
19+
20+
it.each([false, true])(
21+
"uses the available Comic Relief face for italic spreadsheet text (bold: %s)",
22+
(bold) => {
23+
expect(sheetFace("Comic Relief", bold, true)).toMatchObject({
24+
family: "Comic Relief",
25+
weight: bold ? 700 : 400,
26+
style: "normal",
27+
});
28+
},
29+
);

‎apps/web/src/components/files/office/fonts.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ export function presentationFace(family: string, bold: boolean, italic: boolean)
3535
);
3636
}
3737

38+
/** The face Excel's cells draw a family with when it is missing: its metric-compatible or bundled face, if any. */
39+
export function sheetFace(family: string, bold: boolean, italic: boolean) {
40+
return (
41+
resolveMetricCompatFace(family, bold, italic) ??
42+
resolveBundledFamilyFace(family, bold, italic) ??
43+
resolveBundledFamilyFace(family, bold, false) ??
44+
resolveBundledFamilyFace(family, false, false)
45+
);
46+
}
47+
3848
/**
3949
* A bundled face's bytes. The font package caches them per worker, so a face loads once per open file; the browser's
4050
* caches, which every worker shares, serve the files to the next.

0 commit comments

Comments
 (0)