Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: isNodeEmpty Function Incorrectly Flags Tiptap as Empty When Table Lacks Text Content #5475

Closed
1 task done
MohamedElGhandour opened this issue Aug 13, 2024 · 4 comments
Labels
Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug

Comments

@MohamedElGhandour
Copy link

Affected Packages

core

Version(s)

2.5.9

Bug Description

The isNodeEmpty function in Tiptap is incorrectly detecting the editor's content as empty when it contains a table with no text inside the cells. Even though a table structure is present, the function returns true, indicating that the content is empty.

Steps to Reproduce

  1. Initialize a Tiptap editor instance.
  2. Insert a table with multiple rows and columns, but leave all cells empty (no text).
  3. Call the editor.isEmpty - isNodeEmpty - function.

Browser Used

Chrome

Code Example URL

No response

Expected Behavior

The function should recognize that the editor contains a table and return false, indicating that the content is not empty.

Additional Context (Optional)

Actual Behavior

The function returns true, incorrectly signaling that the editor content is empty.

Impact

This behavior can lead to issues in applications relying on isNodeEmpty for content validation or other logic, as valid content (a table) is being dismissed as empty.

Suggested Fix

The function should be updated to account for non-text elements like tables, considering the editor non-empty if such elements are present.

Dependency Updates

  • Yes, I've updated all my dependencies.
@MohamedElGhandour MohamedElGhandour added Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug labels Aug 13, 2024
@nperez0111
Copy link
Contributor

This is difficult, there are many different ways to understand content as being "empty", a table is meant to hold content, if it does not have that content isn't it empty?

I understand that this is different from prior versions but that is only because the prior versions did not really inspect the content.

Could you explain to me a situation where a user would really create an empty table & expect it to be saved?

@MohamedElGhandour
Copy link
Author

This is difficult, there are many different ways to understand content as being "empty", a table is meant to hold content, if it does not have that content isn't it empty?

I understand that this is different from prior versions but that is only because the prior versions did not really inspect the content.

Could you explain to me a situation where a user would really create an empty table & expect it to be saved?

The issue arises because I rely on the isNodeEmpty function to decide when to save or update content. Currently, Tiptap's isNodeEmpty function flags a table with no text as empty, even though the table structure itself holds value. This can be problematic, especially in cases like placeholders for future data or template creation. I understand and agree with your perspective on the matter.

@nperez0111
Copy link
Contributor

In your case, I would recommend using:

import { isNodeEmpty } from '@tiptap/core';

function shouldSave(editor: Editor) {
    let isContentEmpty = true

    editor.state.doc.content.forEach(node => {
      if (isContentEmpty === false) {
        return
      }
      isContentEmpty = isNodeEmpty(node, { checkChildren: false })
    })

    return isContentEmpty
}

This will check all of the top-level children to see if they are empty or not.

Honestly, I think that another option for counting certain node types as empty may be convenient to have as well

@MohamedElGhandour
Copy link
Author

MohamedElGhandour commented Aug 13, 2024

In your case, I would recommend using:

import { isNodeEmpty } from '@tiptap/core';

function shouldSave(editor: Editor) {
    let isContentEmpty = true

    editor.state.doc.content.forEach(node => {
      if (isContentEmpty === false) {
        return
      }
      isContentEmpty = isNodeEmpty(node, { checkChildren: false })
    })

    return isContentEmpty
}

This will check all of the top-level children to see if they are empty or not.

Honestly, I think that another option for counting certain node types as empty may be convenient to have as well

Thank you so much for the suggestion! I really appreciate the code snippet you provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug
Projects
Status: Done
Development

No branches or pull requests

2 participants