Repository: ianstormtaylor/slate
File: packages/slate-react/src/plugin/react-editor.ts (TypeScript)

Code before:
        } else {
          textNode = leafNode.closest('[data-slate-node="text"]')!
          domNode = leafNode
          offset = domNode.textContent!.length
          domNode.querySelectorAll('[data-slate-zero-width]').forEach(el => {
            offset -= el.textContent!.length
          })
        }
      } else if (nonEditableNode) {
        // Find the edge of the nearest leaf in `searchDirection` from the
        // non-editable node
        const leafNodes = Array.from(
          editorEl.querySelectorAll(
            // This editor's leaves (not those of a nested editor)
            '[data-slate-leaf]:not(:scope [data-slate-editor] [data-slate-leaf])'
          )
        )
        leafNode =
          (searchDirection === 'forward'
            ? leafNodes.find(leaf => isAfter(nonEditableNode, leaf))
            : leafNodes.findLast(leaf => isBefore(nonEditableNode, leaf))) ??
          null

        if (!leafNode) {
          offset = 1
        } else {
          textNode = leafNode.closest('[data-slate-node="text"]')!
          domNode = leafNode
          if (searchDirection === 'forward') {
            offset = 0
          } else {
            offset = domNode.textContent!.length
            domNode.querySelectorAll('[data-slate-zero-width]').forEach(el => {
              offset -= el.textContent!.length
            })
          }
        }
      }

      if (
        domNode &&
        offset === domNode.textContent!.length &&
        // COMPAT: Android IMEs might remove the zero width space while composing,
        // and we don't add it for line-breaks.
        IS_ANDROID &&
        domNode.getAttribute('data-slate-zero-width') === 'z' &&
        domNode.textContent?.startsWith('\uFEFF') &&
        // COMPAT: If the parent node is a Slate zero-width space, editor is
        // because the text node should have no characters. However, during IME
        // composition the ASCII characters will be prepended to the zero-width
        // space, so subtract 1 from the offset to account for the zero-width

Review comment (suggestion):
I copied this from the `else if (voidNode)` block, which prefixes it with `// COMPAT: In read-only editors the leaf is not rendered.` But I'm thinking that may not be applicable here. I'm considering replacing this with

```suggestion
        if (leafNode) {
```

Code after:
        } else {
          textNode = leafNode.closest('[data-slate-node="text"]')!
          domNode = leafNode
          offset = domNode.textContent!.length
          domNode.querySelectorAll('[data-slate-zero-width]').forEach(el => {
            offset -= el.textContent!.length
          })
        }
      } else if (nonEditableNode) {
        // Find the edge of the nearest leaf in `searchDirection`
        const getLeafNodes = (node: DOMElement | null | undefined) =>
          node
            ? node.querySelectorAll(
                // Exclude leaf nodes in nested editors
                '[data-slate-leaf]:not(:scope [data-slate-editor] [data-slate-leaf])'
              )
            : []
        const elementNode = nonEditableNode.closest(
          '[data-slate-node="element"]'
        )

        if (searchDirection === 'forward') {
          const leafNodes = [
            ...getLeafNodes(elementNode),
            ...getLeafNodes(elementNode?.nextElementSibling),
          ]
          leafNode =
            leafNodes.find(leaf => isAfter(nonEditableNode, leaf)) ?? null
        } else {
          const leafNodes = [
            ...getLeafNodes(elementNode?.previousElementSibling),
            ...getLeafNodes(elementNode),
          ]
          leafNode =
            leafNodes.findLast(leaf => isBefore(nonEditableNode, leaf)) ?? null
        }

        if (leafNode) {
          textNode = leafNode.closest('[data-slate-node="text"]')!
          domNode = leafNode
          if (searchDirection === 'forward') {
            offset = 0
          } else {
            offset = domNode.textContent!.length
            domNode.querySelectorAll('[data-slate-zero-width]').forEach(el => {
              offset -= el.textContent!.length
            })
          }
        }
      }