Debian Codegraph @

mixmark-io-domino/lib/ChildNode.js

raw symbols SCIP
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 // The ChildNode interface contains methods that are particular to `Node`
20 // objects that can have a parent. It is implemented by `Element`,
21 // `DocumentType`, and `CharacterData` objects.
22 var ChildNode = {
23
24 // Inserts a set of Node or String objects in the children list of this
25 // ChildNode's parent, just after this ChildNode. String objects are
26 // inserted as the equivalent Text nodes.
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 // Find "viable next sibling"; that is, next one not in argArr
32 while (nextSibling && argArr.some(function(v) { return v===nextSibling; }))
33 nextSibling = nextSibling.nextSibling;
34 // ok, parent and sibling are saved away since this node could itself
35 // appear in argArr and we're about to move argArr to a document fragment.
36 var docFrag = createDocumentFragmentFromArguments(this.doc, argArr);
37
38 parentNode.insertBefore(docFrag, nextSibling);
39 }},
40
41 // Inserts a set of Node or String objects in the children list of this
42 // ChildNode's parent, just before this ChildNode. String objects are
43 // inserted as the equivalent Text nodes.
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 // Find "viable prev sibling"; that is, prev one not in argArr
49 while (prevSibling && argArr.some(function(v) { return v===prevSibling; }))
50 prevSibling = prevSibling.previousSibling;
51 // ok, parent and sibling are saved away since this node could itself
52 // appear in argArr and we're about to move argArr to a document fragment.
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 // Remove this node from its parent
61 remove: { value: function remove() {
62 if (this.parentNode === null) return;
63
64 // Send mutation events if necessary
65 if (this.doc) {
66 this.doc._preremoveNodeIterators(this);
67 if (this.rooted) {
68 this.doc.mutateRemove(this);
69 }
70 }
71
72 // Remove this node from its parents array of children
73 // and update the structure id for all ancestors
74 this._remove();
75
76 // Forget this node's parent
77 this.parentNode = null;
78 }},
79
80 // Remove this node w/o uprooting or sending mutation events
81 // (But do update the structure id for all ancestors)
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 // Replace this node with the nodes or strings provided as arguments.
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 // Find "viable next sibling"; that is, next one not in argArr
104 while (nextSibling && argArr.some(function(v) { return v===nextSibling; }))
105 nextSibling = nextSibling.nextSibling;
106 // ok, parent and sibling are saved away since this node could itself
107 // appear in argArr and we're about to move argArr to a document fragment.
108 var docFrag = createDocumentFragmentFromArguments(this.doc, argArr);
109 if (this.parentNode === parentNode) {
110 parentNode.replaceChild(docFrag, this);
111 } else {
112 // `this` was inserted into docFrag
113 parentNode.insertBefore(docFrag, nextSibling);
114 }
115 }},
116
117 };
118
119 module.exports = ChildNode;