( parent: DOMElement, index: number, direction: 'forward' | 'backward', )
| 56 | */ |
| 57 | |
| 58 | export const getEditableChildAndIndex = ( |
| 59 | parent: DOMElement, |
| 60 | index: number, |
| 61 | direction: 'forward' | 'backward', |
| 62 | ): [DOMNode, number] => { |
| 63 | const { childNodes } = parent |
| 64 | let child = childNodes[index] |
| 65 | let i = index |
| 66 | let triedForward = false |
| 67 | let triedBackward = false |
| 68 | |
| 69 | // While the child is a comment node, or an element node with no children, |
| 70 | // keep iterating to find a sibling non-void, non-comment node. |
| 71 | while (isDOMComment(child) || (isDOMElement(child) && child.childNodes.length === 0)) { |
| 72 | if (triedForward && triedBackward) { |
| 73 | break |
| 74 | } |
| 75 | |
| 76 | if (i >= childNodes.length) { |
| 77 | triedForward = true |
| 78 | i = index - 1 |
| 79 | direction = 'backward' |
| 80 | continue |
| 81 | } |
| 82 | |
| 83 | if (i < 0) { |
| 84 | triedBackward = true |
| 85 | i = index + 1 |
| 86 | direction = 'forward' |
| 87 | continue |
| 88 | } |
| 89 | |
| 90 | child = childNodes[i] |
| 91 | index = i |
| 92 | i += direction === 'forward' ? 1 : -1 |
| 93 | } |
| 94 | |
| 95 | return [child, index] |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get the nearest editable child at `index` in a `parent`, preferring |
no test coverage detected
searching dependent graphs…