Skip to content

Build your personal knowledge base with Trilium Notes

style(spreadsheet): one line per row, one CSS rule per line

Elian Doran089b391d0365
diff --git a/apps/client/src/services/office_renderer.spec.ts b/apps/client/src/services/office_renderer.spec.ts
index 33cae1fbdc..02098b8b2e 100644
--- a/apps/client/src/services/office_renderer.spec.ts
+++ b/apps/client/src/services/office_renderer.spec.ts
@@ -64,12 +64,14 @@ describe("renderOfficeToHtml", () => {
});
it("splits a leading stylesheet off the fragment, keeping it clear of the sanitizer", async () => {
+ // The renderer writes one rule per line, with the tags on their own.
serverGet.mockResolvedValueOnce(
- '<style>.spreadsheet-table .sst-1{font-weight:bold}</style>\n<table><td class="sst-1">x</td></table>');
+ '<style>\n.spreadsheet-table .sst-1{font-weight:bold}\n.spreadsheet-table .sst-2{color:red}\n</style>\n'
+ + '<table><td class="sst-1">x</td></table>');
const { css, html } = await renderOfficeToHtml("notes", "n1");
- expect(css).toBe(".spreadsheet-table .sst-1{font-weight:bold}");
+ expect(css).toBe(".spreadsheet-table .sst-1{font-weight:bold}\n.spreadsheet-table .sst-2{color:red}");
expect(sanitizeNoteContentHtml).toHaveBeenCalledWith('<table><td class="sst-1">x</td></table>');
expect(html).not.toContain("<style>");
});
diff --git a/apps/client/src/services/office_renderer.ts b/apps/client/src/services/office_renderer.ts
index c157abfa48..1a8db14103 100644
--- a/apps/client/src/services/office_renderer.ts
+++ b/apps/client/src/services/office_renderer.ts
@@ -46,7 +46,10 @@ function splitStylesheet(body: string): OfficePreview {
return { css: "", html: body };
}
- return { css: body.slice("<style>".length, end), html: body.slice(end + "</style>".length).trimStart() };
+ return {
+ css: body.slice("<style>".length, end).trim(),
+ html: body.slice(end + "</style>".length).trimStart()
+ };
}
/**
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
index 419e33211e..7333f96f90 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
@@ -2244,7 +2244,7 @@ describe("style deduplication", () => {
const stylesheet = /^<style>([\s\S]*?)<\/style>/.exec(html)?.[1] ?? "";
expect(stylesheet).toBeTruthy();
- for (const rule of stylesheet.split("}").filter(Boolean)) {
+ for (const rule of stylesheet.split("}").map((r) => r.trim()).filter(Boolean)) {
// Cell rules, and the gridline rule whose context sits in :where(); neither can
// match anything outside a rendered sheet.
expect(rule).toMatch(/^(\.spreadsheet-table \.sst-|:where\(\.spreadsheet-table)/);
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.ts b/packages/commons/src/lib/spreadsheet/render_to_html.ts
index e37ab9d3d6..c81064797c 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts
@@ -83,10 +83,10 @@ export function renderSpreadsheetToHtml(jsonContent: string): string {
* gridlines, then one rule per distinct cell style. Returns "" when there is nothing to write.
*/
function buildStylesheet(sheets: IWorksheetData[], classes: StyleClasses): string {
- const gridlines = sheets.some((sheet) => sheet.showGridlines !== 0) ? GRIDLINE_RULE : "";
- const rules = gridlines + classes.toRules();
+ const rules = sheets.some((sheet) => sheet.showGridlines !== 0) ? [GRIDLINE_RULE] : [];
+ rules.push(...classes.toRules());
- return rules ? `<style>${rules}</style>` : "";
+ return rules.length ? `<style>\n${rules.join("\n")}\n</style>` : "";
}
/**
@@ -130,18 +130,18 @@ class StyleClasses {
}
/**
- * Builds a rule per distinct style collected, or "" when none was.
+ * Builds a rule per distinct style collected.
*
* The declarations cannot contain `<` or `/`: colors are validated against a pattern and every
* other author-supplied value goes through `sanitizeCssValue`, which strips both. That is what
* keeps a cell's styling from closing its rule, or the element these rules are written into.
*/
- toRules(): string {
+ toRules(): string[] {
const rules: string[] = [];
for (const [declarations, name] of this.names) {
rules.push(`.spreadsheet-table .${name}{${declarations}}`);
}
- return rules.join("");
+ return rules;
}
}
@@ -359,11 +359,8 @@ function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData |
const lines: string[] = [];
lines.push(buildTableTag(sheet, totalWidth));
- lines.push("<colgroup>");
- for (const width of colWidths) {
- lines.push(`<col class="${classes.classFor(`width:${px(width)}px`)}">`);
- }
- lines.push("</colgroup>");
+ const cols = colWidths.map((width) => `<col class="${classes.classFor(`width:${px(width)}px`)}">`);
+ lines.push(`<colgroup>${cols.join("")}</colgroup>`);
const defaultHeight = sheet.defaultRowHeight ?? 24;
@@ -375,9 +372,10 @@ function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData |
// Univer leaves a cell at the bottom of its row unless it sets `vt`, while HTML centres it.
// The row carries that default because a `td` inherits `vertical-align`, so a cell that
// sets its own still wins.
- lines.push(`<tr class="${classes.classFor(`height:${height}px;vertical-align:bottom`)}">`);
+ const rowClass = classes.classFor(`height:${height}px;vertical-align:bottom`);
const neighbours: RowNeighbours = { row, minCol, maxCol, cellData, columnData, mergeMap };
+ const cells: string[] = [];
for (let col = minCol; col <= maxCol; col++) {
if (columnData[col]?.hd) continue;
@@ -403,10 +401,13 @@ function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData |
if (mergeInfo.colSpan > 1) attrs.push(`colspan="${mergeInfo.colSpan}"`);
}
- lines.push(`<td${attrs.length ? " " + attrs.join(" ") : ""}>${value}</td>`);
+ cells.push(`<td${attrs.length ? " " + attrs.join(" ") : ""}>${value}</td>`);
}
- lines.push("</tr>");
+ // A row on one line: it reads like the grid it came from, and drops a newline per cell,
+ // which on a large sheet is tens of thousands of them. Whitespace between cells has no
+ // effect on a table's layout, so nothing depends on the newlines being there.
+ lines.push(`<tr class="${rowClass}">${cells.join("")}</tr>`);
}
lines.push("</table>");