( child: TestNode, parent: TestElement, ref?: TestNode | null, )
| 138 | } |
| 139 | |
| 140 | function insert( |
| 141 | child: TestNode, |
| 142 | parent: TestElement, |
| 143 | ref?: TestNode | null, |
| 144 | ): void { |
| 145 | let refIndex |
| 146 | if (ref) { |
| 147 | refIndex = parent.children.indexOf(ref) |
| 148 | if (refIndex === -1) { |
| 149 | console.error('ref: ', ref) |
| 150 | console.error('parent: ', parent) |
| 151 | throw new Error('ref is not a child of parent') |
| 152 | } |
| 153 | } |
| 154 | logNodeOp({ |
| 155 | type: NodeOpTypes.INSERT, |
| 156 | targetNode: child, |
| 157 | parentNode: parent, |
| 158 | refNode: ref, |
| 159 | }) |
| 160 | // remove the node first, but don't log it as a REMOVE op |
| 161 | remove(child, false) |
| 162 | // re-calculate the ref index because the child's removal may have affected it |
| 163 | refIndex = ref ? parent.children.indexOf(ref) : -1 |
| 164 | if (refIndex === -1) { |
| 165 | parent.children.push(child) |
| 166 | child.parentNode = parent |
| 167 | } else { |
| 168 | parent.children.splice(refIndex, 0, child) |
| 169 | child.parentNode = parent |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | function remove(child: TestNode, logOp = true): void { |
| 174 | const parent = child.parentNode |
no test coverage detected