(domPoint: DOMPoint)
| 16 | */ |
| 17 | |
| 18 | export 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 |
no test coverage detected
searching dependent graphs…