WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

DOMEditor.toSlatePoint could find a point in a parent editor #5947

@christianhg

Description

@christianhg

Hi there 👋

I was investigating a gnarly issue in an app that unfortunately is closed source and quite difficult to create a reproduction sandbox from. In any case, I thought I would write up what I found about how DOMEditor.toSlatePoint can end up producing a wrong point in a nested editor scenario:

  1. If the editor is read-only and therefore is marked as contenteditable="false" then nonEditableNode could end up being the editor itself:
    const nonEditableNode =
    potentialNonEditableNode && editorEl.contains(potentialNonEditableNode)
    ? potentialNonEditableNode
    : null
  2. And if there's no leafNode because the event origins from a non-leaf then we end up in the } else if (nonEditableNode) { clause:
    let leafNode = parentNode.closest('[data-slate-leaf]')
  3. Inside this clause, if the editor is nested inside another editor then nonEditableNode.closest('[data-slate-node="element"]') can potentially find a node inside the parent editor:
    const elementNode = nonEditableNode.closest(
    '[data-slate-node="element"]'
    )
  4. Further down DOMEditor.toSlatePoint, DOMEditor.toSlateNode is called:
    const slateNode = DOMEditor.toSlateNode(editor, textNode!)
  5. But since this method uses the global ELEMENT_TO_NODE WeakMap, it will probably return a Slate node even though it's from another editor:
    const node = domEl ? ELEMENT_TO_NODE.get(domEl as HTMLElement) : null

I suppose this is a bug? Could it be mitigated it by adding a check to see if nonEditableNode is the editor itself and then return the start or end of the editor depending on the searchDirection?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions