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

Fixed error, cannot read properties of undefined reading .nodeType #175

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nanov94
Copy link

@nanov94 nanov94 commented Nov 13, 2024

What?

Get error: TypeError: Cannot read properties of undefined (reading 'nodeType')

Why?

In some cases node does not have children and as a result we get property nodeType from undefined

How?

Checked existing of prev

@marijnh
Copy link
Member

marijnh commented Nov 13, 2024

The only way this seems to be possible is if offset is either negative or pointing beyond the length of the node. Both would suggest something is going wrong before this point, and I don't like adding tests like this to paper over other bugs. So I'd be interested in an example of a situation that triggers this crash.

@nanov94
Copy link
Author

nanov94 commented Nov 21, 2024

Hi @marijnh :)
Thank you for your comment 🙏

Summary:

We are encountering an error (below) when selecting table cells, particularly when the table contains empty rows.
Error: TypeError: Cannot read properties of undefined (reading 'nodeType')

More details:

Yes, you are correct about the case where offset is either negative or points beyond the length of the node. In my app, we have another case where offset is 0 and node has no children.

Why is this the case? Empty rows without table cells:
Firstly, a tr element in HTML can have empty content here. This mean that node.childNodes is empty array:

  1. node does not have any children => node.childNodes = []
  2. we got element by index in empty array => node.childNodes[offset - 1] = undefined
  3. after got field from undefined element => node.childNodes[offset - 1]).nodeType

Secondly, this happened after using another ProseMirror library: prosemirror-tables.
How can we create tables with empty rows via ProseMirror? You need to select a few table cells and rows and merge them. We use mergeCells from prosemirror-tables for it (video attached)
Demo merge table.webm

@marijnh
Copy link
Member

marijnh commented Nov 21, 2024

where offset is 0 and node has no children.

The line already contains && offset, which will be false when offset == 0, so the code that could cause this crash will simply not execute in that situation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants