()
| 24 | * @returns {Document} document |
| 25 | */ |
| 26 | export default function undom() { |
| 27 | |
| 28 | function isElement(node) { |
| 29 | return node.nodeType===1; |
| 30 | } |
| 31 | |
| 32 | class Node { |
| 33 | constructor(nodeType, nodeName) { |
| 34 | this.nodeType = nodeType; |
| 35 | this.nodeName = nodeName; |
| 36 | this.childNodes = []; |
| 37 | } |
| 38 | get nextSibling() { |
| 39 | let p = this.parentNode; |
| 40 | if (p) return p.childNodes[findWhere(p.childNodes, this, true) + 1]; |
| 41 | } |
| 42 | get previousSibling() { |
| 43 | let p = this.parentNode; |
| 44 | if (p) return p.childNodes[findWhere(p.childNodes, this, true) - 1]; |
| 45 | } |
| 46 | get firstChild() { |
| 47 | return this.childNodes[0]; |
| 48 | } |
| 49 | get lastChild() { |
| 50 | return this.childNodes[this.childNodes.length-1]; |
| 51 | } |
| 52 | appendChild(child) { |
| 53 | this.insertBefore(child); |
| 54 | return child; |
| 55 | } |
| 56 | insertBefore(child, ref) { |
| 57 | child.remove(); |
| 58 | child.parentNode = this; |
| 59 | !ref ? this.childNodes.push(child) : splice(this.childNodes, ref, child); |
| 60 | return child; |
| 61 | } |
| 62 | replaceChild(child, ref) { |
| 63 | if (ref.parentNode===this) { |
| 64 | this.insertBefore(child, ref); |
| 65 | ref.remove(); |
| 66 | return ref; |
| 67 | } |
| 68 | } |
| 69 | removeChild(child) { |
| 70 | splice(this.childNodes, child); |
| 71 | return child; |
| 72 | } |
| 73 | remove() { |
| 74 | if (this.parentNode) this.parentNode.removeChild(this); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | |
| 79 | class Text extends Node { |
| 80 | constructor(text) { |
| 81 | super(3, '#text'); // TEXT_NODE |
| 82 | this.nodeValue = text; |
| 83 | } |
no test coverage detected
searching dependent graphs…