| 1 |
"use strict"; |
| 2 |
|
| 3 |
var Node = require('./Node'); |
| 4 |
var LinkedList = require('./LinkedList'); |
| 5 |
|
| 6 |
var createDocumentFragmentFromArguments = function(document, args) { |
| 7 |
var docFrag = document.createDocumentFragment(); |
| 8 |
|
| 9 |
for (var i=0; i<args.length; i++) { |
| 10 |
var argItem = args[i]; |
| 11 |
var isNode = argItem instanceof Node; |
| 12 |
docFrag.appendChild(isNode ? argItem : |
| 13 |
document.createTextNode(String(argItem))); |
| 14 |
} |
| 15 |
|
| 16 |
return docFrag; |
| 17 |
}; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
var ChildNode = { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
after: { value: function after() { |
| 28 |
var argArr = Array.prototype.slice.call(arguments); |
| 29 |
var parentNode = this.parentNode, nextSibling = this.nextSibling; |
| 30 |
if (parentNode === null) { return; } |
| 31 |
|
| 32 |
while (nextSibling && argArr.some(function(v) { return v===nextSibling; })) |
| 33 |
nextSibling = nextSibling.nextSibling; |
| 34 |
|
| 35 |
|
| 36 |
var docFrag = createDocumentFragmentFromArguments(this.doc, argArr); |
| 37 |
|
| 38 |
parentNode.insertBefore(docFrag, nextSibling); |
| 39 |
}}, |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
before: { value: function before() { |
| 45 |
var argArr = Array.prototype.slice.call(arguments); |
| 46 |
var parentNode = this.parentNode, prevSibling = this.previousSibling; |
| 47 |
if (parentNode === null) { return; } |
| 48 |
|
| 49 |
while (prevSibling && argArr.some(function(v) { return v===prevSibling; })) |
| 50 |
prevSibling = prevSibling.previousSibling; |
| 51 |
|
| 52 |
|
| 53 |
var docFrag = createDocumentFragmentFromArguments(this.doc, argArr); |
| 54 |
|
| 55 |
var nextSibling = |
| 56 |
prevSibling ? prevSibling.nextSibling : parentNode.firstChild; |
| 57 |
parentNode.insertBefore(docFrag, nextSibling); |
| 58 |
}}, |
| 59 |
|
| 60 |
|
| 61 |
remove: { value: function remove() { |
| 62 |
if (this.parentNode === null) return; |
| 63 |
|
| 64 |
|
| 65 |
if (this.doc) { |
| 66 |
this.doc._preremoveNodeIterators(this); |
| 67 |
if (this.rooted) { |
| 68 |
this.doc.mutateRemove(this); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
this._remove(); |
| 75 |
|
| 76 |
|
| 77 |
this.parentNode = null; |
| 78 |
}}, |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
_remove: { value: function _remove() { |
| 83 |
var parent = this.parentNode; |
| 84 |
if (parent === null) return; |
| 85 |
if (parent._childNodes) { |
| 86 |
parent._childNodes.splice(this.index, 1); |
| 87 |
} else if (parent._firstChild === this) { |
| 88 |
if (this._nextSibling === this) { |
| 89 |
parent._firstChild = null; |
| 90 |
} else { |
| 91 |
parent._firstChild = this._nextSibling; |
| 92 |
} |
| 93 |
} |
| 94 |
LinkedList.remove(this); |
| 95 |
parent.modify(); |
| 96 |
}}, |
| 97 |
|
| 98 |
|
| 99 |
replaceWith: { value: function replaceWith() { |
| 100 |
var argArr = Array.prototype.slice.call(arguments); |
| 101 |
var parentNode = this.parentNode, nextSibling = this.nextSibling; |
| 102 |
if (parentNode === null) { return; } |
| 103 |
|
| 104 |
while (nextSibling && argArr.some(function(v) { return v===nextSibling; })) |
| 105 |
nextSibling = nextSibling.nextSibling; |
| 106 |
|
| 107 |
|
| 108 |
var docFrag = createDocumentFragmentFromArguments(this.doc, argArr); |
| 109 |
if (this.parentNode === parentNode) { |
| 110 |
parentNode.replaceChild(docFrag, this); |
| 111 |
} else { |
| 112 |
|
| 113 |
parentNode.insertBefore(docFrag, nextSibling); |
| 114 |
} |
| 115 |
}}, |
| 116 |
|
| 117 |
}; |
| 118 |
|
| 119 |
module.exports = ChildNode; |