Skip to content

Commit

Permalink
fix(core): resolve isNodeEmpty criteria #5415 (#5419)
Browse files Browse the repository at this point in the history
  • Loading branch information
nperez0111 authored Aug 1, 2024
1 parent 068559d commit 84ebd51
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-pants-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tiptap/core": patch
---

Fix change criteria for isNodeEmpty to resolve #5415
8 changes: 4 additions & 4 deletions packages/core/src/helpers/isNodeEmpty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
29 changes: 27 additions & 2 deletions tests/cypress/integration/core/isNodeEmpty.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand All @@ -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',
Expand Down Expand Up @@ -177,7 +202,7 @@ describe('isNodeEmpty', () => {
],
})

expect(isNodeEmpty(node)).to.eq(true)
expect(isNodeEmpty(node)).to.eq(false)
})
})
})

0 comments on commit 84ebd51

Please sign in to comment.