Incorrect results for EditorView.domAtPos() for block replace decorations #1722
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
Thanks for building and maintaining an amazing, open-source editor. I really appreciate it and depend on your library in the application I've written. While using CodeMirror, I believe I've identified the following bug:
The value returned by
EditorView.domAtPos()for positions that contain a block replace decoration return incorrect values in several cases, as shown in the attached example code. The screenshot below shows that lines 0, 2, and 4 are block replace decorations; lines 1 and 3 are text.However, calling
EditorView.domAtPos()only identifies thedivthese block replace decorations contain at position 0, but not at position 2 and 4. The output:To build the attached code:
esbuild src/bug.html src/bug.mts --bundle --outdir=./static/bundled --sourcemap --format=esm --loader:.html=copy.Thanks for your time in looking at this.
Running this code I do not get an error from the
view.coordsAtPos(2)call. Here's the sandbox link.Thanks for taking a look! The code in the sandbox looks fairly different from the code in the attached .zip file. The function under test is
view.domAtPos(), notview.coordsAtPos(). Would you like me to rework this code to run in the sandbox?Here's the code I see:
My bad. I still had the old bug.zip from your earlier issue in my downloads folder and didn't notice my browser added a
(1)to the new download, so I ended up unzipping the old file again.But yes, links to /try are much more convenient for me, since I know exactly how they work and don't have to be paranoid about people trying to slip bad stuff onto my machine.
Now that I'm actually running the code, I'm not seeing anything wrong with the output of
posAtDOM. Yes, it can return the last position in a node, so no, you cannot assume that there will be a node atresult.node.childNodes[result.offset]. It's returning a position in the DOM tree, not a pointer to a specific node.Sounds good -- I'll create links to this in the future.
Perhaps I'm confused -- here's what I expected:
view.domAtPos(0)<div class="CodeChat-doc" contenteditable="false"><p>0-1</p></div>view.domAtPos(2)<div class="CodeChat-doc" contenteditable="false"><p>2-3</p></div><div class="cm-line">3</div>view.domAtPos(4)<div class="CodeChat-doc" contenteditable="false"><p>4-5</p></div><div class="cm-line"><br></div>I'm hoping to use
view.domAtPos()to recover the DOM element for block replace decorations. So, I would expect in this example thatview.domAtPos(2)orview.domAtPos(4)would return a reference to the block decoration DOM, just as the call toview.domAtPos(0)does. Instead, these calls returns a reference to the DOM for following CodeMirror line. Is there some other way to retrieve the DOM for a block decoration given a position?Again, thank you for your time.
Oh, now I get it. The framing of
domAtPosas identifying a node confused me. Yes, this is actually a bug. Attached patch should help.Fantastic. Thanks again for your patience in resolving this. The change works great. I appreciate it!