MCPcopy Create free account
hub / github.com/editablejs/editable / normalizeDOMPoint

Function normalizeDOMPoint

packages/editor/src/utils/dom.ts:18–43  ·  view source on GitHub ↗
(domPoint: DOMPoint)

Source from the content-addressed store, hash-verified

16 */
17
18export const normalizeDOMPoint = (domPoint: DOMPoint): DOMPoint => {
19 let [node, offset] = domPoint
20
21 // If it's an element node, its offset refers to the index of its children
22 // including comment nodes, so try to find the right text child node.
23 if (isDOMElement(node) && node.childNodes.length) {
24 let isLast = offset === node.childNodes.length
25 let index = isLast ? offset - 1 : offset
26 ;[node, index] = getEditableChildAndIndex(node, index, isLast ? 'backward' : 'forward')
27 // If the editable child found is in front of input offset, we instead seek to its end
28 isLast = index < offset
29
30 // If the node has children, traverse until we have a leaf node. Leaf nodes
31 // can be either text nodes, or other void DOM nodes.
32 while (isDOMElement(node) && node.childNodes.length) {
33 const i = isLast ? node.childNodes.length - 1 : 0
34 node = getEditableChild(node, i, isLast ? 'backward' : 'forward')
35 }
36
37 // Determine the new offset inside the text node.
38 offset = isLast && node.textContent != null ? node.textContent.length : 0
39 }
40
41 // Return the node and offset.
42 return [node, offset]
43}
44
45/**
46 * Determines wether the active element is nested within a shadowRoot

Callers 1

toEditorPointFunction · 0.90

Calls 3

isDOMElementFunction · 0.90
getEditableChildAndIndexFunction · 0.85
getEditableChildFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…