(node: HTMLElement)
| 8 | |
| 9 | /** Creates a deep clone of an element. */ |
| 10 | export function deepCloneNode(node: HTMLElement): HTMLElement { |
| 11 | const clone = node.cloneNode(true) as HTMLElement; |
| 12 | const descendantsWithId = clone.querySelectorAll('[id]'); |
| 13 | const nodeName = node.nodeName.toLowerCase(); |
| 14 | |
| 15 | // Remove the `id` to avoid having multiple elements with the same id on the page. |
| 16 | clone.removeAttribute('id'); |
| 17 | |
| 18 | for (let i = 0; i < descendantsWithId.length; i++) { |
| 19 | descendantsWithId[i].removeAttribute('id'); |
| 20 | } |
| 21 | |
| 22 | if (nodeName === 'canvas') { |
| 23 | transferCanvasData(node as HTMLCanvasElement, clone as HTMLCanvasElement); |
| 24 | } else if (nodeName === 'input' || nodeName === 'select' || nodeName === 'textarea') { |
| 25 | transferInputData(node as HTMLInputElement, clone as HTMLInputElement); |
| 26 | } |
| 27 | |
| 28 | transferData('canvas', node, clone, transferCanvasData); |
| 29 | transferData('input, textarea, select', node, clone, transferInputData); |
| 30 | return clone; |
| 31 | } |
| 32 | |
| 33 | /** Matches elements between an element and its clone and allows for their data to be cloned. */ |
| 34 | function transferData<T extends Element>( |
no test coverage detected
searching dependent graphs…