From 83116b411497ef4f9e27c23bc02427eb37187835 Mon Sep 17 00:00:00 2001 From: Jonas Date: Mon, 9 Nov 2026 10:33:34 +0100 Subject: [PATCH] perf(table): only check changed ranges for nested tables The filter preventing nested tables walked the whole document on every document change and resolved the position of every table, row and cell. Resolving is linear in the number of siblings, so typing in a long table got slower with every row. A nested table can only appear in content changed by the transaction, so only check the changed ranges, skip textblocks and only check table nodes: rows, cells, headers and captions can only exist inside a table. Benchmark on a 500 kB document with one table of 2,262 rows, median per keystroke in a cell in the middle of the table, before -> after: Chromium: - typing: 795 ms -> 672 ms - toggle bold (Ctrl+B): 797 ms -> 599 ms Firefox: - typing: 730 ms -> 487 ms - toggle bold (Ctrl+B): 936 ms -> 319 ms Related to #908 Signed-off-by: Jonas Assisted-by: ClaudeCode:claude-opus-5-5 --- src/nodes/Table/TableCell.ts | 29 ++++++++++++++++++----------- src/tests/nodes/Table.spec.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/src/nodes/Table/TableCell.ts b/src/nodes/Table/TableCell.ts index 66e9f605881..61057a33678 100644 --- a/src/nodes/Table/TableCell.ts +++ b/src/nodes/Table/TableCell.ts @@ -7,7 +7,7 @@ import type { Node as PMNode, Schema } from '@tiptap/pm/model' import { showWarning } from '@nextcloud/dialogs' import { t } from '@nextcloud/l10n' -import { mergeAttributes } from '@tiptap/core' +import { getChangedRanges, mergeAttributes } from '@tiptap/core' import { TableCell } from '@tiptap/extension-table' import { Fragment, Slice } from '@tiptap/pm/model' import { Plugin, PluginKey } from '@tiptap/pm/state' @@ -66,19 +66,26 @@ export default TableCell.extend({ return true } + // Only changed ranges can contain a new nested table + const { doc } = transaction let hasNestedTable = false - transaction.doc.descendants((node, pos) => { - if (node.type.name.startsWith('table')) { - const $pos = transaction.doc.resolve(pos) - for (let depth = $pos.depth; depth >= 0; depth--) { - const ancestor = $pos.node(depth) - if (ancestor.type.name === 'tableCell') { - // eslint-disable-next-line no-console - console.warn('Detected nested table, filtering out transaction') - hasNestedTable = true + getChangedRanges(transaction).forEach(({ newRange }) => { + doc.nodesBetween(newRange.from, newRange.to, (node, pos) => { + if (hasNestedTable || node.isTextblock) { + return false + } + if (node.type.name === 'table') { + const $pos = doc.resolve(pos) + for (let depth = $pos.depth; depth >= 0; depth--) { + if ($pos.node(depth).type.name === 'tableCell') { + // eslint-disable-next-line no-console + console.warn('Detected nested table, filtering out transaction') + hasNestedTable = true + return false + } } } - } + }) }) return !hasNestedTable diff --git a/src/tests/nodes/Table.spec.js b/src/tests/nodes/Table.spec.js index c076dfe091f..ac1ab70ae38 100644 --- a/src/tests/nodes/Table.spec.js +++ b/src/tests/nodes/Table.spec.js @@ -417,6 +417,38 @@ describe('Table extension', () => { expect(serializeMarkdown()).toBe('| H1 | H2 |\n|--------|------|\n| a1 | a2 |\n| b1 | H1b2 | \\\n| | | \\\n| - item | |\n') }) }) + + describe('nested tables', () => { + const html = '
H1

a1

  • item

after

' + + test('transaction inserting a table into a cell is filtered out', ({ editor }) => { + editor.commands.setContent(html) + const doc = editor.state.doc + editor.view.dispatch(editor.state.tr.insert(findText(editor, 'a1') + 3, doc.firstChild)) + expect(editor.state.doc.eq(doc)).toBe(true) + }) + + test('transaction inserting a table into a list in a cell is filtered out', ({ editor }) => { + editor.commands.setContent(html) + const doc = editor.state.doc + editor.view.dispatch(editor.state.tr.insert(findText(editor, 'item') + 5, doc.firstChild)) + expect(editor.state.doc.eq(doc)).toBe(true) + }) + + test('transaction inserting a table outside of tables is applied', ({ editor }) => { + editor.commands.setContent(html) + const doc = editor.state.doc + editor.view.dispatch(editor.state.tr.insert(doc.content.size, doc.firstChild)) + const tables = editor.state.doc.children.filter((node) => node.type.name === 'table') + expect(tables).toHaveLength(2) + }) + + test('typing in a cell is applied', ({ editor, serializeMarkdown }) => { + editor.commands.setContent(html) + editor.view.dispatch(editor.state.tr.insertText('x', findText(editor, 'a1'))) + expect(serializeMarkdown()).toContain('xa1') + }) + }) }) function getHeaderCell(editor, targetIndex = 0) {