Debian Codegraph @

mixmark-io-domino/lib/Node.js

raw symbols SCIP
1 "use strict";
2 module.exports = Node;
3
4 var EventTarget = require('./EventTarget');
5 var LinkedList = require('./LinkedList');
6 var NodeUtils = require('./NodeUtils');
7 var utils = require('./utils');
8
9 // All nodes have a nodeType and an ownerDocument.
10 // Once inserted, they also have a parentNode.
11 // This is an abstract class; all nodes in a document are instances
12 // of a subtype, so all the properties are defined by more specific
13 // constructors.
14 function Node() {
15 EventTarget.call(this);
16 this.parentNode = null;
17 this._nextSibling = this._previousSibling = this;
18 this._index = undefined;
19 }
20
21 var ELEMENT_NODE = Node.ELEMENT_NODE = 1;
22 var ATTRIBUTE_NODE = Node.ATTRIBUTE_NODE = 2;
23 var TEXT_NODE = Node.TEXT_NODE = 3;
24 var CDATA_SECTION_NODE = Node.CDATA_SECTION_NODE = 4;
25 var ENTITY_REFERENCE_NODE = Node.ENTITY_REFERENCE_NODE = 5;
26 var ENTITY_NODE = Node.ENTITY_NODE = 6;
27 var PROCESSING_INSTRUCTION_NODE = Node.PROCESSING_INSTRUCTION_NODE = 7;
28 var COMMENT_NODE = Node.COMMENT_NODE = 8;
29 var DOCUMENT_NODE = Node.DOCUMENT_NODE = 9;
30 var DOCUMENT_TYPE_NODE = Node.DOCUMENT_TYPE_NODE = 10;
31 var DOCUMENT_FRAGMENT_NODE = Node.DOCUMENT_FRAGMENT_NODE = 11;
32 var NOTATION_NODE = Node.NOTATION_NODE = 12;
33
34 var DOCUMENT_POSITION_DISCONNECTED = Node.DOCUMENT_POSITION_DISCONNECTED = 0x01;
35 var DOCUMENT_POSITION_PRECEDING = Node.DOCUMENT_POSITION_PRECEDING = 0x02;
36 var DOCUMENT_POSITION_FOLLOWING = Node.DOCUMENT_POSITION_FOLLOWING = 0x04;
37 var DOCUMENT_POSITION_CONTAINS = Node.DOCUMENT_POSITION_CONTAINS = 0x08;
38 var DOCUMENT_POSITION_CONTAINED_BY = Node.DOCUMENT_POSITION_CONTAINED_BY = 0x10;
39 var DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 0x20;
40
41 Node.prototype = Object.create(EventTarget.prototype, {
42
43 // Node that are not inserted into the tree inherit a null parent
44
45 // XXX: the baseURI attribute is defined by dom core, but
46 // a correct implementation of it requires HTML features, so
47 // we'll come back to this later.
48 baseURI: { get: utils.nyi },
49
50 parentElement: { get: function() {
51 return (this.parentNode && this.parentNode.nodeType===ELEMENT_NODE) ? this.parentNode : null;
52 }},
53
54 hasChildNodes: { value: utils.shouldOverride },
55
56 firstChild: { get: utils.shouldOverride },
57
58 lastChild: { get: utils.shouldOverride },
59
60 isConnected: {
61 get: function () {
62 let node = this;
63 while (node != null) {
64 if (node.nodeType === Node.DOCUMENT_NODE) {
65 return true;
66 }
67
68 node = node.parentNode;
69 if (node != null && node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
70 node = node.host;
71 }
72 }
73 return false;
74 },
75 },
76
77 previousSibling: { get: function() {
78 var parent = this.parentNode;
79 if (!parent) return null;
80 if (this === parent.firstChild) return null;
81 return this._previousSibling;
82 }},
83
84 nextSibling: { get: function() {
85 var parent = this.parentNode, next = this._nextSibling;
86 if (!parent) return null;
87 if (next === parent.firstChild) return null;
88 return next;
89 }},
90
91 textContent: {
92 // Should override for DocumentFragment/Element/Attr/Text/PI/Comment
93 get: function() { return null; },
94 set: function(v) { /* do nothing */ },
95 },
96
97 innerText: {
98 // Should override for DocumentFragment/Element/Attr/Text/PI/Comment
99 get: function() { return null; },
100 set: function(v) { /* do nothing */ },
101 },
102
103 _countChildrenOfType: { value: function(type) {
104 var sum = 0;
105 for (var kid = this.firstChild; kid !== null; kid = kid.nextSibling) {
106 if (kid.nodeType === type) sum++;
107 }
108 return sum;
109 }},
110
111 _ensureInsertValid: { value: function _ensureInsertValid(node, child, isPreinsert) {
112 var parent = this, i, kid;
113 if (!node.nodeType) throw new TypeError('not a node');
114 // 1. If parent is not a Document, DocumentFragment, or Element
115 // node, throw a HierarchyRequestError.
116 switch (parent.nodeType) {
117 case DOCUMENT_NODE:
118 case DOCUMENT_FRAGMENT_NODE:
119 case ELEMENT_NODE:
120 break;
121 default: utils.HierarchyRequestError();
122 }
123 // 2. If node is a host-including inclusive ancestor of parent,
124 // throw a HierarchyRequestError.
125 if (node.isAncestor(parent)) utils.HierarchyRequestError();
126 // 3. If child is not null and its parent is not parent, then
127 // throw a NotFoundError. (replaceChild omits the 'child is not null'
128 // and throws a TypeError here if child is null.)
129 if (child !== null || !isPreinsert) {
130 if (child.parentNode !== parent) utils.NotFoundError();
131 }
132 // 4. If node is not a DocumentFragment, DocumentType, Element,
133 // Text, ProcessingInstruction, or Comment node, throw a
134 // HierarchyRequestError.
135 switch (node.nodeType) {
136 case DOCUMENT_FRAGMENT_NODE:
137 case DOCUMENT_TYPE_NODE:
138 case ELEMENT_NODE:
139 case TEXT_NODE:
140 case PROCESSING_INSTRUCTION_NODE:
141 case COMMENT_NODE:
142 break;
143 default: utils.HierarchyRequestError();
144 }
145 // 5. If either node is a Text node and parent is a document, or
146 // node is a doctype and parent is not a document, throw a
147 // HierarchyRequestError.
148 // 6. If parent is a document, and any of the statements below, switched
149 // on node, are true, throw a HierarchyRequestError.
150 if (parent.nodeType === DOCUMENT_NODE) {
151 switch (node.nodeType) {
152 case TEXT_NODE:
153 utils.HierarchyRequestError();
154 break;
155 case DOCUMENT_FRAGMENT_NODE:
156 // 6a1. If node has more than one element child or has a Text
157 // node child.
158 if (node._countChildrenOfType(TEXT_NODE) > 0)
159 utils.HierarchyRequestError();
160 switch (node._countChildrenOfType(ELEMENT_NODE)) {
161 case 0:
162 break;
163 case 1:
164 // 6a2. Otherwise, if node has one element child and either
165 // parent has an element child, child is a doctype, or child
166 // is not null and a doctype is following child. [preinsert]
167 // 6a2. Otherwise, if node has one element child and either
168 // parent has an element child that is not child or a
169 // doctype is following child. [replaceWith]
170 if (child !== null /* always true here for replaceWith */) {
171 if (isPreinsert && child.nodeType === DOCUMENT_TYPE_NODE)
172 utils.HierarchyRequestError();
173 for (kid = child.nextSibling; kid !== null; kid = kid.nextSibling) {
174 if (kid.nodeType === DOCUMENT_TYPE_NODE)
175 utils.HierarchyRequestError();
176 }
177 }
178 i = parent._countChildrenOfType(ELEMENT_NODE);
179 if (isPreinsert) {
180 // "parent has an element child"
181 if (i > 0)
182 utils.HierarchyRequestError();
183 } else {
184 // "parent has an element child that is not child"
185 if (i > 1 || (i === 1 && child.nodeType !== ELEMENT_NODE))
186 utils.HierarchyRequestError();
187 }
188 break;
189 default: // 6a1, continued. (more than one Element child)
190 utils.HierarchyRequestError();
191 }
192 break;
193 case ELEMENT_NODE:
194 // 6b. parent has an element child, child is a doctype, or
195 // child is not null and a doctype is following child. [preinsert]
196 // 6b. parent has an element child that is not child or a
197 // doctype is following child. [replaceWith]
198 if (child !== null /* always true here for replaceWith */) {
199 if (isPreinsert && child.nodeType === DOCUMENT_TYPE_NODE)
200 utils.HierarchyRequestError();
201 for (kid = child.nextSibling; kid !== null; kid = kid.nextSibling) {
202 if (kid.nodeType === DOCUMENT_TYPE_NODE)
203 utils.HierarchyRequestError();
204 }
205 }
206 i = parent._countChildrenOfType(ELEMENT_NODE);
207 if (isPreinsert) {
208 // "parent has an element child"
209 if (i > 0)
210 utils.HierarchyRequestError();
211 } else {
212 // "parent has an element child that is not child"
213 if (i > 1 || (i === 1 && child.nodeType !== ELEMENT_NODE))
214 utils.HierarchyRequestError();
215 }
216 break;
217 case DOCUMENT_TYPE_NODE:
218 // 6c. parent has a doctype child, child is non-null and an
219 // element is preceding child, or child is null and parent has
220 // an element child. [preinsert]
221 // 6c. parent has a doctype child that is not child, or an
222 // element is preceding child. [replaceWith]
223 if (child === null) {
224 if (parent._countChildrenOfType(ELEMENT_NODE))
225 utils.HierarchyRequestError();
226 } else {
227 // child is always non-null for [replaceWith] case
228 for (kid = parent.firstChild; kid !== null; kid = kid.nextSibling) {
229 if (kid === child) break;
230 if (kid.nodeType === ELEMENT_NODE)
231 utils.HierarchyRequestError();
232 }
233 }
234 i = parent._countChildrenOfType(DOCUMENT_TYPE_NODE);
235 if (isPreinsert) {
236 // "parent has an doctype child"
237 if (i > 0)
238 utils.HierarchyRequestError();
239 } else {
240 // "parent has an doctype child that is not child"
241 if (i > 1 || (i === 1 && child.nodeType !== DOCUMENT_TYPE_NODE))
242 utils.HierarchyRequestError();
243 }
244 break;
245 }
246 } else {
247 // 5, continued: (parent is not a document)
248 if (node.nodeType === DOCUMENT_TYPE_NODE) utils.HierarchyRequestError();
249 }
250 }},
251
252 insertBefore: { value: function insertBefore(node, child) {
253 var parent = this;
254 // 1. Ensure pre-insertion validity
255 parent._ensureInsertValid(node, child, true);
256 // 2. Let reference child be child.
257 var refChild = child;
258 // 3. If reference child is node, set it to node's next sibling
259 if (refChild === node) { refChild = node.nextSibling; }
260 // 4. Adopt node into parent's node document.
261 parent.doc.adoptNode(node);
262 // 5. Insert node into parent before reference child.
263 node._insertOrReplace(parent, refChild, false);
264 // 6. Return node
265 return node;
266 }},
267
268
269 appendChild: { value: function(child) {
270 // This invokes _appendChild after doing validity checks.
271 return this.insertBefore(child, null);
272 }},
273
274 _appendChild: { value: function(child) {
275 child._insertOrReplace(this, null, false);
276 }},
277
278 removeChild: { value: function removeChild(child) {
279 var parent = this;
280 if (!child.nodeType) throw new TypeError('not a node');
281 if (child.parentNode !== parent) utils.NotFoundError();
282 child.remove();
283 return child;
284 }},
285
286 // To replace a `child` with `node` within a `parent` (this)
287 replaceChild: { value: function replaceChild(node, child) {
288 var parent = this;
289 // Ensure validity (slight differences from pre-insertion check)
290 parent._ensureInsertValid(node, child, false);
291 // Adopt node into parent's node document.
292 if (node.doc !== parent.doc) {
293 // XXX adoptNode has side-effect of removing node from its parent
294 // and generating a mutation event, thus causing the _insertOrReplace
295 // to generate two deletes and an insert instead of a 'move'
296 // event. It looks like the new MutationObserver stuff avoids
297 // this problem, but for now let's only adopt (ie, remove `node`
298 // from its parent) here if we need to.
299 parent.doc.adoptNode(node);
300 }
301 // Do the replace.
302 node._insertOrReplace(parent, child, true);
303 return child;
304 }},
305
306 // See: http://ejohn.org/blog/comparing-document-position/
307 contains: { value: function contains(node) {
308 if (node === null) { return false; }
309 if (this === node) { return true; /* inclusive descendant */ }
310 /* jshint bitwise: false */
311 return (this.compareDocumentPosition(node) &
312 DOCUMENT_POSITION_CONTAINED_BY) !== 0;
313 }},
314
315 compareDocumentPosition: { value: function compareDocumentPosition(that){
316 // Basic algorithm for finding the relative position of two nodes.
317 // Make a list the ancestors of each node, starting with the
318 // document element and proceeding down to the nodes themselves.
319 // Then, loop through the lists, looking for the first element
320 // that differs. The order of those two elements give the
321 // order of their descendant nodes. Or, if one list is a prefix
322 // of the other one, then that node contains the other.
323
324 if (this === that) return 0;
325
326 // If they're not owned by the same document or if one is rooted
327 // and one is not, then they're disconnected.
328 if (this.doc !== that.doc ||
329 this.rooted !== that.rooted)
330 return (DOCUMENT_POSITION_DISCONNECTED +
331 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC);
332
333 // Get arrays of ancestors for this and that
334 var these = [], those = [];
335 for(var n = this; n !== null; n = n.parentNode) these.push(n);
336 for(n = that; n !== null; n = n.parentNode) those.push(n);
337 these.reverse(); // So we start with the outermost
338 those.reverse();
339
340 if (these[0] !== those[0]) // No common ancestor
341 return (DOCUMENT_POSITION_DISCONNECTED +
342 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC);
343
344 n = Math.min(these.length, those.length);
345 for(var i = 1; i < n; i++) {
346 if (these[i] !== those[i]) {
347 // We found two different ancestors, so compare
348 // their positions
349 if (these[i].index < those[i].index)
350 return DOCUMENT_POSITION_FOLLOWING;
351 else
352 return DOCUMENT_POSITION_PRECEDING;
353 }
354 }
355
356 // If we get to here, then one of the nodes (the one with the
357 // shorter list of ancestors) contains the other one.
358 if (these.length < those.length)
359 return (DOCUMENT_POSITION_FOLLOWING +
360 DOCUMENT_POSITION_CONTAINED_BY);
361 else
362 return (DOCUMENT_POSITION_PRECEDING +
363 DOCUMENT_POSITION_CONTAINS);
364 }},
365
366 isSameNode: {value : function isSameNode(node) {
367 return this === node;
368 }},
369
370
371 // This method implements the generic parts of node equality testing
372 // and defers to the (non-recursive) type-specific isEqual() method
373 // defined by subclasses
374 isEqualNode: { value: function isEqualNode(node) {
375 if (!node) return false;
376 if (node.nodeType !== this.nodeType) return false;
377
378 // Check type-specific properties for equality
379 if (!this.isEqual(node)) return false;
380
381 // Now check children for number and equality
382 for (var c1 = this.firstChild, c2 = node.firstChild;
383 c1 && c2;
384 c1 = c1.nextSibling, c2 = c2.nextSibling) {
385 if (!c1.isEqualNode(c2)) return false;
386 }
387 return c1 === null && c2 === null;
388 }},
389
390 // This method delegates shallow cloning to a clone() method
391 // that each concrete subclass must implement
392 cloneNode: { value: function(deep) {
393 // Clone this node
394 var clone = this.clone();
395
396 // Handle the recursive case if necessary
397 if (deep) {
398 for (var kid = this.firstChild; kid !== null; kid = kid.nextSibling) {
399 clone._appendChild(kid.cloneNode(true));
400 }
401 }
402
403 return clone;
404 }},
405
406 lookupPrefix: { value: function lookupPrefix(ns) {
407 var e;
408 if (ns === '' || ns === null || ns === undefined) return null;
409 switch(this.nodeType) {
410 case ELEMENT_NODE:
411 return this._lookupNamespacePrefix(ns, this);
412 case DOCUMENT_NODE:
413 e = this.documentElement;
414 return e ? e.lookupPrefix(ns) : null;
415 case ENTITY_NODE:
416 case NOTATION_NODE:
417 case DOCUMENT_FRAGMENT_NODE:
418 case DOCUMENT_TYPE_NODE:
419 return null;
420 case ATTRIBUTE_NODE:
421 e = this.ownerElement;
422 return e ? e.lookupPrefix(ns) : null;
423 default:
424 e = this.parentElement;
425 return e ? e.lookupPrefix(ns) : null;
426 }
427 }},
428
429
430 lookupNamespaceURI: {value: function lookupNamespaceURI(prefix) {
431 if (prefix === '' || prefix === undefined) { prefix = null; }
432 var e;
433 switch(this.nodeType) {
434 case ELEMENT_NODE:
435 return utils.shouldOverride();
436 case DOCUMENT_NODE:
437 e = this.documentElement;
438 return e ? e.lookupNamespaceURI(prefix) : null;
439 case ENTITY_NODE:
440 case NOTATION_NODE:
441 case DOCUMENT_TYPE_NODE:
442 case DOCUMENT_FRAGMENT_NODE:
443 return null;
444 case ATTRIBUTE_NODE:
445 e = this.ownerElement;
446 return e ? e.lookupNamespaceURI(prefix) : null;
447 default:
448 e = this.parentElement;
449 return e ? e.lookupNamespaceURI(prefix) : null;
450 }
451 }},
452
453 isDefaultNamespace: { value: function isDefaultNamespace(ns) {
454 if (ns === '' || ns === undefined) { ns = null; }
455 var defaultNamespace = this.lookupNamespaceURI(null);
456 return (defaultNamespace === ns);
457 }},
458
459 // Utility methods for nodes. Not part of the DOM
460
461 // Return the index of this node in its parent.
462 // Throw if no parent, or if this node is not a child of its parent
463 index: { get: function() {
464 var parent = this.parentNode;
465 if (this === parent.firstChild) return 0; // fast case
466 var kids = parent.childNodes;
467 if (this._index === undefined || kids[this._index] !== this) {
468 // Ensure that we don't have an O(N^2) blowup if none of the
469 // kids have defined indices yet and we're traversing via
470 // nextSibling or previousSibling
471 for (var i=0; i<kids.length; i++) {
472 kids[i]._index = i;
473 }
474 utils.assert(kids[this._index] === this);
475 }
476 return this._index;
477 }},
478
479 // Return true if this node is equal to or is an ancestor of that node
480 // Note that nodes are considered to be ancestors of themselves
481 isAncestor: { value: function(that) {
482 // If they belong to different documents, then they're unrelated.
483 if (this.doc !== that.doc) return false;
484 // If one is rooted and one isn't then they're not related
485 if (this.rooted !== that.rooted) return false;
486
487 // Otherwise check by traversing the parentNode chain
488 for(var e = that; e; e = e.parentNode) {
489 if (e === this) return true;
490 }
491 return false;
492 }},
493
494 // DOMINO Changed the behavior to conform with the specs. See:
495 // https://groups.google.com/d/topic/mozilla.dev.platform/77sIYcpdDmc/discussion
496 ensureSameDoc: { value: function(that) {
497 if (that.ownerDocument === null) {
498 that.ownerDocument = this.doc;
499 }
500 else if(that.ownerDocument !== this.doc) {
501 utils.WrongDocumentError();
502 }
503 }},
504
505 removeChildren: { value: utils.shouldOverride },
506
507 // Insert this node as a child of parent before the specified child,
508 // or insert as the last child of parent if specified child is null,
509 // or replace the specified child with this node, firing mutation events as
510 // necessary
511 _insertOrReplace: { value: function _insertOrReplace(parent, before, isReplace) {
512 var child = this, before_index, i;
513
514 if (child.nodeType === DOCUMENT_FRAGMENT_NODE && child.rooted) {
515 utils.HierarchyRequestError();
516 }
517
518 /* Ensure index of `before` is cached before we (possibly) remove it. */
519 if (parent._childNodes) {
520 before_index = (before === null) ? parent._childNodes.length :
521 before.index; /* ensure _index is cached */
522
523 // If we are already a child of the specified parent, then
524 // the index may have to be adjusted.
525 if (child.parentNode === parent) {
526 var child_index = child.index;
527 // If the child is before the spot it is to be inserted at,
528 // then when it is removed, the index of that spot will be
529 // reduced.
530 if (child_index < before_index) {
531 before_index--;
532 }
533 }
534 }
535
536 // Delete the old child
537 if (isReplace) {
538 if (before.rooted) before.doc.mutateRemove(before);
539 before.parentNode = null;
540 }
541
542 var n = before;
543 if (n === null) { n = parent.firstChild; }
544
545 // If both the child and the parent are rooted, then we want to
546 // transplant the child without uprooting and rerooting it.
547 var bothRooted = child.rooted && parent.rooted;
548 if (child.nodeType === DOCUMENT_FRAGMENT_NODE) {
549 var spliceArgs = [0, isReplace ? 1 : 0], next;
550 for (var kid = child.firstChild; kid !== null; kid = next) {
551 next = kid.nextSibling;
552 spliceArgs.push(kid);
553 kid.parentNode = parent;
554 }
555 var len = spliceArgs.length;
556 // Add all nodes to the new parent, overwriting the old child
557 if (isReplace) {
558 LinkedList.replace(n, len > 2 ? spliceArgs[2] : null);
559 } else if (len > 2 && n !== null) {
560 LinkedList.insertBefore(spliceArgs[2], n);
561 }
562 if (parent._childNodes) {
563 spliceArgs[0] = (before === null) ?
564 parent._childNodes.length : before._index;
565 parent._childNodes.splice.apply(parent._childNodes, spliceArgs);
566 for (i=2; i<len; i++) {
567 spliceArgs[i]._index = spliceArgs[0] + (i - 2);
568 }
569 } else if (parent._firstChild === before) {
570 if (len > 2) {
571 parent._firstChild = spliceArgs[2];
572 } else if (isReplace) {
573 parent._firstChild = null;
574 }
575 }
576 // Remove all nodes from the document fragment
577 if (child._childNodes) {
578 child._childNodes.length = 0;
579 } else {
580 child._firstChild = null;
581 }
582 // Call the mutation handlers
583 // Use spliceArgs since the original array has been destroyed. The
584 // liveness guarantee requires us to clone the array so that
585 // references to the childNodes of the DocumentFragment will be empty
586 // when the insertion handlers are called.
587 if (parent.rooted) {
588 parent.modify();
589 for (i = 2; i < len; i++) {
590 parent.doc.mutateInsert(spliceArgs[i]);
591 }
592 }
593 } else {
594 if (before === child) { return; }
595 if (bothRooted) {
596 // Remove the child from its current position in the tree
597 // without calling remove(), since we don't want to uproot it.
598 child._remove();
599 } else if (child.parentNode) {
600 child.remove();
601 }
602
603 // Insert it as a child of its new parent
604 child.parentNode = parent;
605 if (isReplace) {
606 LinkedList.replace(n, child);
607 if (parent._childNodes) {
608 child._index = before_index;
609 parent._childNodes[before_index] = child;
610 } else if (parent._firstChild === before) {
611 parent._firstChild = child;
612 }
613 } else {
614 if (n !== null) {
615 LinkedList.insertBefore(child, n);
616 }
617 if (parent._childNodes) {
618 child._index = before_index;
619 parent._childNodes.splice(before_index, 0, child);
620 } else if (parent._firstChild === before) {
621 parent._firstChild = child;
622 }
623 }
624 if (bothRooted) {
625 parent.modify();
626 // Generate a move mutation event
627 parent.doc.mutateMove(child);
628 } else if (parent.rooted) {
629 parent.modify();
630 parent.doc.mutateInsert(child);
631 }
632 }
633 }},
634
635
636 // Return the lastModTime value for this node. (For use as a
637 // cache invalidation mechanism. If the node does not already
638 // have one, initialize it from the owner document's modclock
639 // property. (Note that modclock does not return the actual
640 // time; it is simply a counter incremented on each document
641 // modification)
642 lastModTime: { get: function() {
643 if (!this._lastModTime) {
644 this._lastModTime = this.doc.modclock;
645 }
646 return this._lastModTime;
647 }},
648
649 // Increment the owner document's modclock and use the new
650 // value to update the lastModTime value for this node and
651 // all of its ancestors. Nodes that have never had their
652 // lastModTime value queried do not need to have a
653 // lastModTime property set on them since there is no
654 // previously queried value to ever compare the new value
655 // against, so only update nodes that already have a
656 // _lastModTime property.
657 modify: { value: function() {
658 if (this.doc.modclock) { // Skip while doc.modclock == 0
659 var time = ++this.doc.modclock;
660 for(var n = this; n; n = n.parentElement) {
661 if (n._lastModTime) {
662 n._lastModTime = time;
663 }
664 }
665 }
666 }},
667
668 // This attribute is not part of the DOM but is quite helpful.
669 // It returns the document with which a node is associated. Usually
670 // this is the ownerDocument. But ownerDocument is null for the
671 // document object itself, so this is a handy way to get the document
672 // regardless of the node type
673 doc: { get: function() {
674 return this.ownerDocument || this;
675 }},
676
677
678 // If the node has a nid (node id), then it is rooted in a document
679 rooted: { get: function() {
680 return !!this._nid;
681 }},
682
683 normalize: { value: function() {
684 var next;
685 for (var child=this.firstChild; child !== null; child=next) {
686 next = child.nextSibling;
687
688 if (child.normalize) {
689 child.normalize();
690 }
691
692 if (child.nodeType !== Node.TEXT_NODE) {
693 continue;
694 }
695
696 if (child.nodeValue === "") {
697 this.removeChild(child);
698 continue;
699 }
700
701 var prevChild = child.previousSibling;
702 if (prevChild === null) {
703 continue;
704 } else if (prevChild.nodeType === Node.TEXT_NODE) {
705 // merge this with previous and remove the child
706 prevChild.appendData(child.nodeValue);
707 this.removeChild(child);
708 }
709 }
710 }},
711
712 // Convert the children of a node to an HTML string.
713 // This is used by the innerHTML getter
714 // The serialization spec is at:
715 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html#serializing-html-fragments
716 //
717 // The serialization logic is intentionally implemented in a separate
718 // `NodeUtils` helper instead of the more obvious choice of a private
719 // `_serializeOne()` method on the `Node.prototype` in order to avoid
720 // the megamorphic `this._serializeOne` property access, which reduces
721 // performance unnecessarily. If you need specialized behavior for a
722 // certain subclass, you'll need to implement that in `NodeUtils`.
723 // See https://github.com/fgnass/domino/pull/142 for more information.
724 serialize: { value: function() {
725 if (this._innerHTML) {
726 return this._innerHTML;
727 }
728 var s = '';
729 for (var kid = this.firstChild; kid !== null; kid = kid.nextSibling) {
730 s += NodeUtils.serializeOne(kid, this);
731 }
732 return s;
733 }},
734
735 // Non-standard, but often useful for debugging.
736 outerHTML: {
737 get: function() {
738 return NodeUtils.serializeOne(this, { nodeType: 0 });
739 },
740 set: utils.nyi,
741 },
742
743 // mirror node type properties in the prototype, so they are present
744 // in instances of Node (and subclasses)
745 ELEMENT_NODE: { value: ELEMENT_NODE },
746 ATTRIBUTE_NODE: { value: ATTRIBUTE_NODE },
747 TEXT_NODE: { value: TEXT_NODE },
748 CDATA_SECTION_NODE: { value: CDATA_SECTION_NODE },
749 ENTITY_REFERENCE_NODE: { value: ENTITY_REFERENCE_NODE },
750 ENTITY_NODE: { value: ENTITY_NODE },
751 PROCESSING_INSTRUCTION_NODE: { value: PROCESSING_INSTRUCTION_NODE },
752 COMMENT_NODE: { value: COMMENT_NODE },
753 DOCUMENT_NODE: { value: DOCUMENT_NODE },
754 DOCUMENT_TYPE_NODE: { value: DOCUMENT_TYPE_NODE },
755 DOCUMENT_FRAGMENT_NODE: { value: DOCUMENT_FRAGMENT_NODE },
756 NOTATION_NODE: { value: NOTATION_NODE },
757
758 DOCUMENT_POSITION_DISCONNECTED: { value: DOCUMENT_POSITION_DISCONNECTED },
759 DOCUMENT_POSITION_PRECEDING: { value: DOCUMENT_POSITION_PRECEDING },
760 DOCUMENT_POSITION_FOLLOWING: { value: DOCUMENT_POSITION_FOLLOWING },
761 DOCUMENT_POSITION_CONTAINS: { value: DOCUMENT_POSITION_CONTAINS },
762 DOCUMENT_POSITION_CONTAINED_BY: { value: DOCUMENT_POSITION_CONTAINED_BY },
763 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC: { value: DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC },
764 });