diff --git a/.changeset/cuddly-pants-destroy.md b/.changeset/cuddly-pants-destroy.md new file mode 100644 index 000000000..f3602beb1 --- /dev/null +++ b/.changeset/cuddly-pants-destroy.md @@ -0,0 +1,5 @@ +--- +"@tiptap/core": patch +--- + +Fix change criteria for isNodeEmpty to resolve #5415 diff --git a/packages/core/src/helpers/isNodeEmpty.ts b/packages/core/src/helpers/isNodeEmpty.ts index 5d7a777d4..90d94f6ee 100644 --- a/packages/core/src/helpers/isNodeEmpty.ts +++ b/packages/core/src/helpers/isNodeEmpty.ts @@ -12,12 +12,12 @@ export function isNodeEmpty( return !node.text } - if (node.content.childCount === 0) { - return true + if (node.isAtom || node.isLeaf) { + return false } - if (node.isLeaf) { - return false + if (node.content.childCount === 0) { + return true } if (checkChildren) { diff --git a/tests/cypress/integration/core/isNodeEmpty.spec.ts b/tests/cypress/integration/core/isNodeEmpty.spec.ts index 7db88996d..7e1a8e6c8 100644 --- a/tests/cypress/integration/core/isNodeEmpty.spec.ts +++ b/tests/cypress/integration/core/isNodeEmpty.spec.ts @@ -3,9 +3,10 @@ import { getSchema, isNodeEmpty } from '@tiptap/core' import Document from '@tiptap/extension-document' import Image from '@tiptap/extension-image' +import Mention from '@tiptap/extension-mention' import StarterKit from '@tiptap/starter-kit' -const schema = getSchema([StarterKit]) +const schema = getSchema([StarterKit, Mention]) const modifiedSchema = getSchema([StarterKit.configure({ document: false }), Document.extend({ content: 'heading block*' })]) const imageSchema = getSchema([StarterKit.configure({ document: false }), Document.extend({ content: 'image block*' }), Image]) @@ -26,6 +27,30 @@ describe('isNodeEmpty', () => { expect(isNodeEmpty(node)).to.eq(false) }) + it('should return false when a paragraph has hardbreaks', () => { + const node = schema.nodeFromJSON({ + type: 'paragraph', + content: [{ type: 'hardBreak' }], + }) + + expect(isNodeEmpty(node)).to.eq(false) + }) + + it('should return false when a paragraph has a mention', () => { + const node = schema.nodeFromJSON({ + type: 'paragraph', + content: [{ + type: 'mention', + attrs: { + id: 'Winona Ryder', + label: null, + }, + }], + }) + + expect(isNodeEmpty(node)).to.eq(false) + }) + it('should return true when a paragraph has no content', () => { const node = schema.nodeFromJSON({ type: 'paragraph', @@ -177,7 +202,7 @@ describe('isNodeEmpty', () => { ], }) - expect(isNodeEmpty(node)).to.eq(true) + expect(isNodeEmpty(node)).to.eq(false) }) }) })