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]: Invalid content doesn't cause error #5500

Closed
1 task done
lepult opened this issue Aug 15, 2024 · 6 comments
Closed
1 task done

[Bug]: Invalid content doesn't cause error #5500

lepult opened this issue Aug 15, 2024 · 6 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

@lepult
Copy link
Contributor

lepult commented Aug 15, 2024

Affected Packages

core, paragraph

Version(s)

2.6.3

Bug Description

Content that doesn't follow the schema can be set with no errors.

Browser Used

Firefox

Code Example URL

https://codesandbox.io/p/sandbox/friendly-leaf-ksgwwk

Expected Behavior

In the sandbox the Tiptap content is set to a Paragraph that contains a HorizontalRule and another Paragraph. This content is invalid, since the schema of Paragraph is 'inline*', while both Paragraph and HorizontalRule are 'block' nodes.

Despite that, the content is set without error and the invalid content is displayed. After you type something and redo that change (CTRL+Z) an error is thrown (when you write in the first line, the error is thrown instantly). Why is that error only thrown after changes and not when the content is set?
grafik

Additional Context (Optional)

This is the result of editor.getJSON() after initialisation of the editor. You can see how the content is invalid, since there is a Paragraph and a HorizontalRule within the Paragraph.

{
  "type": "doc",
  "content": [
    {
      "type": "paragraph",
      "content": [
        {
          "type": "text",
          "text": "Before horizontalRule"
        },
        {
          "type": "horizontalRule"
        },
        {
          "type": "text",
          "text": "After horizontalRule"
        },
        {
          "type": "paragraph",
          "content": [
            {
              "type": "text",
              "text": "Text in Paragraph"
            }
          ]
        }
      ]
    }
  ]
}

Dependency Updates

  • Yes, I've updated all my dependencies.
@lepult lepult 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 15, 2024
@github-project-automation github-project-automation bot moved this to Triage open in Tiptap Aug 15, 2024
@nperez0111
Copy link
Contributor

So what contentCheck is checking for is invalid nodes & marks that are not defined in your schema, not that they follow your schema's nesting rules.

To check for that, it is a pretty easy change:

diff --git a/packages/core/src/helpers/createNodeFromContent.ts b/packages/core/src/helpers/createNodeFromContent.ts
index 7fe83f83c..63a9a2f9d 100644
--- a/packages/core/src/helpers/createNodeFromContent.ts
+++ b/packages/core/src/helpers/createNodeFromContent.ts
@@ -45,7 +45,13 @@ export function createNodeFromContent(
         return Fragment.fromArray(content.map(item => schema.nodeFromJSON(item)))
       }
 
-      return schema.nodeFromJSON(content)
+      const node = schema.nodeFromJSON(content)
+
+      if (options.errorOnInvalidContent) {
+        node.check()
+      }
+
+      return node
     } catch (error) {
       if (options.errorOnInvalidContent) {
         throw new Error('[tiptap error]: Invalid JSON content', { cause: error as Error })

But this will still show the invalid content, ideally prosemirror would just skip over any content that it considers invalid and allows you to display the rest. But it only throws an error right now saying that the node nesting is invalid.

This will require some more thought

@lepult
Copy link
Contributor Author

lepult commented Aug 15, 2024

Thank you for your answer. I didn't know about the node.check() function. My use case is only concerned with detecting invalid content and not with its display, so your answer is a great help to me and already resolves my issues.

@nperez0111
Copy link
Contributor

If that is the case, I would be happy to accept a PR with this change, it requires minimal work on our side and I think matches user expectation with enableContentCheck enabled

@lepult
Copy link
Contributor Author

lepult commented Aug 21, 2024

Do you want me to submit that PR?

@nperez0111
Copy link
Contributor

That would be great @lepult

@nperez0111
Copy link
Contributor

This has been released with 2.7.0-pre.0 and will be released in tiptap 2.7.0 when this beta has been tested by more users

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
No open projects
Archived in project
Development

No branches or pull requests

2 participants