Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/nodes/Table/TableCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions src/tests/nodes/Table.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<table><tr><th>H1</th></tr><tr><td><p>a1</p><ul><li><p>item</p></li></ul></td></tr></table><p>after</p>'

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) {
Expand Down
Loading