Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/text editor/tables: prevent creating tables so large, that may hang the app
Adorian Doranc4ec9f7b6f62
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex d79fe2a8cd..3b7e09f0b0 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -4370,7 +4370,8 @@"insert-table": "Insert table…","rows": "Rows","columns": "Columns",- "enter-a-whole-number-between-1-and-0": "Enter a whole number between 1 and %0."+ "enter-a-whole-number-between-1-and-0": "Enter a whole number between 1 and %0.",+ "the-table-is-too-large-max-0-cells": "The table is too large (max %0 cells)."},"task-state-dropped-title": "Invalid task states","task-state-dropped-message_one": "A custom task state definition is invalid and was ignored.",diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Tables.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Tables.htmlindex e92bf75c59..f8f0a5f13c 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Tables.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Tables.html@@ -12,7 +12,8 @@<li>Click the <span class="tn-icon cke cke-table"></span> button and select <em>Insert table…</em>.A small form opens at the cursor.</li><li>Enter the number of <em>Rows</em> (up to 1000) and <em>Columns</em> (up to- 100).</li>+ 100). The table can have at most 5000 cells in total, for example 1000+ rows of 5 columns.</li><li>Press <em>Insert</em> or <kbd>Enter</kbd>.</li></ol><p>To close the form without inserting a table, press <kbd>Esc</kbd> or clickdiff --git a/docs/User Guide/User Guide/Note Types/Text/Tables.md b/docs/User Guide/User Guide/Note Types/Text/Tables.mdindex 93eb77390c..8543992592 100644--- a/docs/User Guide/User Guide/Note Types/Text/Tables.md+++ b/docs/User Guide/User Guide/Note Types/Text/Tables.md@@ -8,7 +8,7 @@ To create a table, simply press the table button and select with the mouse the dSince v0.107.0, the same menu starts with an _Insert table…_ item, for a table larger than the grid or to type its size instead:1. Click the <span class="tn-icon cke cke-table"></span> button and select _Insert table…_. A small form opens at the cursor.-2. Enter the number of _Rows_ (up to 1000) and _Columns_ (up to 100).+2. Enter the number of _Rows_ (up to 1000) and _Columns_ (up to 100). The table can have at most 5000 cells in total, for example 1000 rows of 5 columns.3. Press _Insert_ or <kbd>Enter</kbd>.To close the form without inserting a table, press <kbd>Esc</kbd> or click outside it. From the keyboard, <kbd>Tab</kbd> moves between _Insert table…_ and the grid while the menu is open.diff --git a/packages/ckeditor5/src/plugins/table_insert/table_insert_form.spec.ts b/packages/ckeditor5/src/plugins/table_insert/table_insert_form.spec.tsindex bf48a2e1cb..4e16b8b2d5 100644--- a/packages/ckeditor5/src/plugins/table_insert/table_insert_form.spec.ts+++ b/packages/ckeditor5/src/plugins/table_insert/table_insert_form.spec.ts@@ -4,6 +4,7 @@ import { userEvent } from "vitest/browser";import TableInsertFormView, {DEFAULT_TABLE_SIZE,+ MAX_TABLE_CELLS,MAX_TABLE_COLUMNS,MAX_TABLE_ROWS,parseTableSize,@@ -73,18 +74,54 @@ describe("TableInsertFormView", () => {expect(form.columnsField.errorText).toBeNull();});- it("accepts the largest sizes and rejects one more", () => {- getInput(form.rowsField).value = String(MAX_TABLE_ROWS);- getInput(form.columnsField).value = String(MAX_TABLE_COLUMNS);- expect(form.validate()).toEqual({ rows: MAX_TABLE_ROWS, columns: MAX_TABLE_COLUMNS });+ it("accepts the largest value of each field and rejects one more", () => {+ setSize(form, MAX_TABLE_ROWS, 1);+ expect(form.validate()).toEqual({ rows: MAX_TABLE_ROWS, columns: 1 });+ setSize(form, 1, MAX_TABLE_COLUMNS);+ expect(form.validate()).toEqual({ rows: 1, columns: MAX_TABLE_COLUMNS });- getInput(form.rowsField).value = String(MAX_TABLE_ROWS + 1);- getInput(form.columnsField).value = String(MAX_TABLE_COLUMNS + 1);+ setSize(form, MAX_TABLE_ROWS + 1, MAX_TABLE_COLUMNS + 1);expect(form.validate()).toBeNull();expect(form.rowsField.errorText).toBe(`Enter a whole number between 1 and ${MAX_TABLE_ROWS}.`);expect(form.columnsField.errorText).toBe(`Enter a whole number between 1 and ${MAX_TABLE_COLUMNS}.`);+ expect(form.sizeErrorText).toBeNull();+ });++ it("accepts a table at the cell limit and rejects a larger one", () => {+ const rowsAtLimit = MAX_TABLE_CELLS / MAX_TABLE_COLUMNS;+ setSize(form, rowsAtLimit, MAX_TABLE_COLUMNS);+ expect(form.validate()).toEqual({ rows: rowsAtLimit, columns: MAX_TABLE_COLUMNS });+ expect(getSizeError(form).classList.contains("ck-hidden")).toBe(true);++ setSize(form, rowsAtLimit + 1, MAX_TABLE_COLUMNS);+ expect(form.validate()).toBeNull();++ const error = getSizeError(form);+ expect(form.sizeErrorText)+ .toBe(`The table is too large (max ${MAX_TABLE_CELLS} cells).`);+ expect(error.textContent).toBe(form.sizeErrorText);+ expect(error.classList.contains("ck-hidden")).toBe(false);+ expect(error.getAttribute("role")).toBe("alert");+ expectValid(form.rowsField);+ expectValid(form.columnsField);+ expect(document.activeElement).toBe(getInput(form.rowsField));+ });++ it("clears the size error when either field is edited", async () => {+ setSize(form, MAX_TABLE_ROWS, MAX_TABLE_COLUMNS);+ form.validate();+ expect(form.sizeErrorText).not.toBeNull();++ await userEvent.fill(getInput(form.rowsField), "999");+ expect(form.sizeErrorText).toBeNull();+ expect(getSizeError(form).classList.contains("ck-hidden")).toBe(true);++ form.validate();+ expect(form.sizeErrorText).not.toBeNull();+ await userEvent.fill(getInput(form.columnsField), "99");+ expect(form.sizeErrorText).toBeNull();});it("shows the error of an invalid field and focuses it", () => {@@ -122,8 +159,11 @@ describe("TableInsertFormView", () => {await userEvent.fill(getInput(form.rowsField), "9");getInput(form.columnsField).value = "0";form.validate();+ setSize(form, MAX_TABLE_ROWS, MAX_TABLE_COLUMNS);+ form.validate();form.reset();+ expect(form.sizeErrorText).toBeNull();const defaultText = String(DEFAULT_TABLE_SIZE);expect(getInput(form.rowsField).value).toBe(defaultText);@@ -179,6 +219,19 @@ function getInput(field: TableSizeFieldView): HTMLInputElement {return getElement(field.inputView) as HTMLInputElement;}+function setSize(form: TableInsertFormView, rows: number, columns: number) {+ getInput(form.rowsField).value = String(rows);+ getInput(form.columnsField).value = String(columns);+}++function getSizeError(form: TableInsertFormView): HTMLElement {+ const error = getElement(form).querySelector<HTMLElement>(".ck-table-insert-form__size-error");+ if (!error) {+ throw new Error("The form has no size error element.");+ }+ return error;+}+function getError(field: TableSizeFieldView): HTMLElement {const error = getElement(field).querySelector<HTMLElement>(".ck-table-insert-form__error");if (!error) {diff --git a/packages/ckeditor5/src/plugins/table_insert/table_insert_form.ts b/packages/ckeditor5/src/plugins/table_insert/table_insert_form.tsindex 53c421d047..da59342751 100644--- a/packages/ckeditor5/src/plugins/table_insert/table_insert_form.ts+++ b/packages/ckeditor5/src/plugins/table_insert/table_insert_form.ts@@ -18,6 +18,9 @@ export const MAX_TABLE_ROWS = 1000;/** The largest number of columns the form accepts. */export const MAX_TABLE_COLUMNS = 100;+/** The largest number of cells the form accepts, as rows × columns. */+export const MAX_TABLE_CELLS = 5000;+/** The number of rows and columns the form shows when it opens. */export const DEFAULT_TABLE_SIZE = 2;@@ -28,10 +31,14 @@ export interface TableSize {/*** The balloon form behind "Insert table…": a "Rows" field, a "Columns" field and an Insert button.- * Invalid values are reported under their field when the form is submitted.+ * Invalid values are reported under their field when the form is submitted, and a table with more+ * than {@link MAX_TABLE_CELLS} cells is reported above the button.*/export default class TableInsertFormView extends View {+ /** The error shown when the table has too many cells, or `null`. */+ declare public sizeErrorText: string | null;+public readonly rowsField: TableSizeFieldView;public readonly columnsField: TableSizeFieldView;public readonly insertButtonView: ButtonView;@@ -41,10 +48,15 @@ export default class TableInsertFormView extends View {private readonly focusables = new ViewCollection<FocusableView>();private readonly focusCycler: FocusCycler;+ private readonly tooLargeText: string;constructor(locale: Locale) {super(locale);const t = locale.t;+ const bind = this.bindTemplate;++ this.set("sizeErrorText", null);+ this.tooLargeText = t("The table is too large (max %0 cells).", MAX_TABLE_CELLS);this.rowsField = new TableSizeFieldView(locale, t("Rows"), MAX_TABLE_ROWS);this.columnsField = new TableSizeFieldView(locale, t("Columns"), MAX_TABLE_COLUMNS);@@ -66,9 +78,33 @@ export default class TableInsertFormView extends View {// The fields are checked by `validate()`, which shows translated errors.novalidate: true},- children: [this.rowsField, this.columnsField, this.insertButtonView]+ children: [+ this.rowsField,+ this.columnsField,+ {+ tag: "div",+ attributes: {+ class: [+ "ck",+ "ck-table-insert-form__error",+ "ck-table-insert-form__size-error",+ bind.if("sizeErrorText", "ck-hidden", (text) => !text)+ ],+ role: bind.if("sizeErrorText", "alert")+ },+ children: [{ text: bind.to("sizeErrorText") }]+ },+ this.insertButtonView+ ]});+ // The size error is cleared as soon as either value is edited.+ for (const field of [this.rowsField, this.columnsField]) {+ field.inputView.on("input", () => {+ this.sizeErrorText = null;+ });+ }+this.focusCycler = new FocusCycler({focusables: this.focusables,focusTracker: this.focusTracker,@@ -113,17 +149,19 @@ export default class TableInsertFormView extends View {this.rowsField.inputView.select();}- /** Sets both fields to {@link DEFAULT_TABLE_SIZE} and clears their errors. */+ /** Sets both fields to {@link DEFAULT_TABLE_SIZE} and clears the errors. */public reset(): void {+ this.sizeErrorText = null;this.rowsField.reset(DEFAULT_TABLE_SIZE);this.columnsField.reset(DEFAULT_TABLE_SIZE);}/**- * Returns the entered size, or `null` when a field is invalid. Every invalid field shows its- * error, and the first one is focused.+ * Returns the entered size, or `null` when a field is invalid or the table has more than+ * {@link MAX_TABLE_CELLS} cells. The errors are shown, and the first field to fix is focused.*/public validate(): TableSize | null {+ this.sizeErrorText = null;const rows = this.rowsField.validate();const columns = this.columnsField.validate();@@ -135,6 +173,11 @@ export default class TableInsertFormView extends View {this.columnsField.focus();return null;}+ if (rows * columns > MAX_TABLE_CELLS) {+ this.sizeErrorText = this.tooLargeText;+ this.rowsField.focus();+ return null;+ }return { rows, columns };}}diff --git a/packages/ckeditor5/src/plugins/table_insert/table_insert_ui.spec.ts b/packages/ckeditor5/src/plugins/table_insert/table_insert_ui.spec.tsindex 54e0f67182..ec22ef4b8c 100644--- a/packages/ckeditor5/src/plugins/table_insert/table_insert_ui.spec.ts+++ b/packages/ckeditor5/src/plugins/table_insert/table_insert_ui.spec.ts@@ -18,7 +18,11 @@ import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";import { userEvent } from "vitest/browser";import { createTestEditor } from "../../../test/editor-kit.js";-import TableInsertFormView, { DEFAULT_TABLE_SIZE, MAX_TABLE_COLUMNS } from "./table_insert_form.js";+import TableInsertFormView, {+ DEFAULT_TABLE_SIZE,+ MAX_TABLE_CELLS,+ MAX_TABLE_COLUMNS+} from "./table_insert_form.js";import TableInsertUI from "./table_insert_ui.js";describe("TableInsertUI", () => {@@ -214,6 +218,30 @@ describe("TableInsertUI", () => {expect(getTableSize(editor)).toEqual({ rows: 3, columns: 4 });});+ it("reports a table with too many cells and inserts nothing", async () => {+ const form = await openForm(editor);++ await userEvent.fill(getInput(form, "rows"), "1000");+ await userEvent.fill(getInput(form, "columns"), "6");+ await userEvent.keyboard("{Enter}");++ const message = getElement(form)+ .querySelector<HTMLElement>(".ck-table-insert-form__size-error");+ expect(getTableSize(editor)).toBeNull();+ expect(balloon.visibleView).toBe(form);+ expect(message?.textContent)+ .toBe(`The table is too large (max ${MAX_TABLE_CELLS} cells).`);+ expect(message?.checkVisibility()).toBe(true);+ expect(document.activeElement).toBe(getInput(form, "rows"));++ await userEvent.fill(getInput(form, "rows"), "100");+ expect(message?.checkVisibility()).toBe(false);++ await userEvent.keyboard("{Enter}");+ expect(getTableSize(editor)).toEqual({ rows: 100, columns: 6 });+ expect(balloon.hasView(form)).toBe(false);+ });+it("closes on Esc without inserting", async () => {const form = await openForm(editor);await userEvent.fill(getInput(form, "rows"), "4");diff --git a/packages/ckeditor5/src/theme/table_insert.css b/packages/ckeditor5/src/theme/table_insert.cssindex e2dc012916..909c6d6450 100644--- a/packages/ckeditor5/src/theme/table_insert.css+++ b/packages/ckeditor5/src/theme/table_insert.css@@ -36,6 +36,10 @@white-space: normal;}+ .ck-table-insert-form__size-error {+ grid-column: 1 / -1;+ }+.ck.ck-button-action {grid-column: 2;justify-self: start;