Debian Codegraph @

mixmark-io-domino/lib/ContainerNode.js

raw symbols SCIP
1 "use strict";
2 module.exports = ContainerNode;
3
4 var Node = require('./Node');
5 var NodeList = require('./NodeList');
6
7 // This class defines common functionality for node subtypes that
8 // can have children
9
10 function ContainerNode() {
11 Node.call(this);
12 this._firstChild = this._childNodes = null;
13 }
14
15 // Primary representation is a circular linked list of siblings
16 ContainerNode.prototype = Object.create(Node.prototype, {
17
18 hasChildNodes: { value: function() {
19 if (this._childNodes) {
20 return this._childNodes.length > 0;
21 }
22 return this._firstChild !== null;
23 }},
24
25 childNodes: { get: function() {
26 this._ensureChildNodes();
27 return this._childNodes;
28 }},
29
30 firstChild: { get: function() {
31 if (this._childNodes) {
32 return this._childNodes.length === 0 ? null : this._childNodes[0];
33 }
34 return this._firstChild;
35 }},
36
37 lastChild: { get: function() {
38 var kids = this._childNodes, first;
39 if (kids) {
40 return kids.length === 0 ? null: kids[kids.length-1];
41 }
42 first = this._firstChild;
43 if (first === null) { return null; }
44 return first._previousSibling; // circular linked list
45 }},
46
47 _ensureChildNodes: { value: function() {
48 if (this._childNodes) { return; }
49 var first = this._firstChild,
50 kid = first,
51 childNodes = this._childNodes = new NodeList();
52 if (first) do {
53 childNodes.push(kid);
54 kid = kid._nextSibling;
55 } while (kid !== first); // circular linked list
56 this._firstChild = null; // free memory
57 }},
58
59 // Remove all of this node's children. This is a minor
60 // optimization that only calls modify() once.
61 removeChildren: { value: function removeChildren() {
62 var root = this.rooted ? this.ownerDocument : null,
63 next = this.firstChild,
64 kid;
65 while (next !== null) {
66 kid = next;
67 next = kid.nextSibling;
68
69 if (root) root.mutateRemove(kid);
70 kid.parentNode = null;
71 }
72 if (this._childNodes) {
73 this._childNodes.length = 0;
74 } else {
75 this._firstChild = null;
76 }
77 this.modify(); // Update last modified type once only
78 }},
79
80 });