|
"use strict";
|
|
|
|
var Node = require('./Node');
|
|
var LinkedList = require('./LinkedList');
|
|
|
|
var createDocumentFragmentFromArguments = function(document, args) {
|
|
var docFrag = document.createDocumentFragment();
|
|
|
|
for (var i=0; i<args.length; i++) {
|
|
var argItem = args[i];
|
|
var isNode = argItem instanceof Node;
|
|
docFrag.appendChild(isNode ? argItem :
|
|
document.createTextNode(String(argItem)));
|
|
}
|
|
|
|
return docFrag;
|
|
};
|
|
|
|
|
|
|
|
|
|
var ChildNode = {
|
|
|
|
|
|
|
|
|
|
after: { value: function after() {
|
|
var argArr = Array.prototype.slice.call(arguments);
|
|
var parentNode = this.parentNode, nextSibling = this.nextSibling;
|
|
if (parentNode === null) { return; }
|
|
|
|
while (nextSibling && argArr.some(function(v) { return v===nextSibling; }))
|
|
nextSibling = nextSibling.nextSibling;
|
|
|
|
|
|
var docFrag = createDocumentFragmentFromArguments(this.doc, argArr);
|
|
|
|
parentNode.insertBefore(docFrag, nextSibling);
|
|
}},
|
|
|
|
|
|
|
|
|
|
before: { value: function before() {
|
|
var argArr = Array.prototype.slice.call(arguments);
|
|
var parentNode = this.parentNode, prevSibling = this.previousSibling;
|
|
if (parentNode === null) { return; }
|
|
|
|
while (prevSibling && argArr.some(function(v) { return v===prevSibling; }))
|
|
prevSibling = prevSibling.previousSibling;
|
|
|
|
|
|
var docFrag = createDocumentFragmentFromArguments(this.doc, argArr);
|
|
|
|
var nextSibling =
|
|
prevSibling ? prevSibling.nextSibling : parentNode.firstChild;
|
|
parentNode.insertBefore(docFrag, nextSibling);
|
|
}},
|
|
|
|
|
|
remove: { value: function remove() {
|
|
if (this.parentNode === null) return;
|
|
|
|
|
|
if (this.doc) {
|
|
this.doc._preremoveNodeIterators(this);
|
|
if (this.rooted) {
|
|
this.doc.mutateRemove(this);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
this._remove();
|
|
|
|
|
|
this.parentNode = null;
|
|
}},
|
|
|
|
|
|
|
|
_remove: { value: function _remove() {
|
|
var parent = this.parentNode;
|
|
if (parent === null) return;
|
|
if (parent._childNodes) {
|
|
parent._childNodes.splice(this.index, 1);
|
|
} else if (parent._firstChild === this) {
|
|
if (this._nextSibling === this) {
|
|
parent._firstChild = null;
|
|
} else {
|
|
parent._firstChild = this._nextSibling;
|
|
}
|
|
}
|
|
LinkedList.remove(this);
|
|
parent.modify();
|
|
}},
|
|
|
|
|
|
replaceWith: { value: function replaceWith() {
|
|
var argArr = Array.prototype.slice.call(arguments);
|
|
var parentNode = this.parentNode, nextSibling = this.nextSibling;
|
|
if (parentNode === null) { return; }
|
|
|
|
while (nextSibling && argArr.some(function(v) { return v===nextSibling; }))
|
|
nextSibling = nextSibling.nextSibling;
|
|
|
|
|
|
var docFrag = createDocumentFragmentFromArguments(this.doc, argArr);
|
|
if (this.parentNode === parentNode) {
|
|
parentNode.replaceChild(docFrag, this);
|
|
} else {
|
|
|
|
parentNode.insertBefore(docFrag, nextSibling);
|
|
}
|
|
}},
|
|
|
|
};
|
|
|
|
module.exports = ChildNode;
|
|
|