Fixed error, cannot read properties of undefined reading .nodeType #175
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "patch-1"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What?
Get error:
TypeError: Cannot read properties of undefined (reading 'nodeType')Why?
In some cases
nodedoes not have children and as a result we get propertynodeTypefromundefinedHow?
Checked existing of
prevThe only way this seems to be possible is if
offsetis 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.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
offsetis either negative or points beyond the length of the node. In my app, we have another case whereoffsetis 0 andnodehas no children.Why is this the case? Empty rows without table cells:
Firstly, a
trelement in HTML can have empty content here. This mean thatnode.childNodesis empty array:nodedoes not have anychildren=>node.childNodes= []node.childNodes[offset - 1]= undefinednode.childNodes[offset - 1]).nodeTypeSecondly, 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-tablesfor it (video attached)Demo merge table.webm
The line already contains
&& offset, which will be false whenoffset == 0, so the code that could cause this crash will simply not execute in that situation.Yep, @marijnh, you are right, sorry for confusing 🙏
We catch error when
offsetmore than 0 andnode.childNodesdoes not have childrenWhat version of prosemirror-view are you using? In anything older than 1.31.0 there might be a situation where this happens, on Chrome when the document contains an
<input>or<textarea>element.we use "prosemirror-view": "1.33.5",
In that case, I'd really want to see instructions on how to reproduce this error, so that I can figure out what the underlying issue is.
We are also seeing this error in Refect Notes (reflect.app). I'm sorry I don't know how to reproduce it though. We're just seeing it in our error logs.
This is what GPT5 is saying:
prevends up undefined and the code still tries to readprev.nodeType.caret*FromPointreturns an out-of-bounds offset (e.g.,offset>node.childNodes.lengthoroffset == 1on an empty element). Thennode.childNodes[offset - 1]is undefined.The problematic spot is here:
Two minimal fixes (either is fine; you can also do both):
Add a null-check before accessing
nodeType:Clamp
offsetto the valid range before indexing:Note there’s a second potential out-of-bounds read a few lines below:
Clamping
offset(as above) will prevent that too.TypeErrorwhen browsers return out-of-range offsets for empty or special elements.Pull request closed