TreeWalker.js
2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* TreeWalker.js
*
* Released under LGPL License.
* Copyright (c) 1999-2015 Ephox Corp. All rights reserved
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
/**
* TreeWalker class enables you to walk the DOM in a linear manner.
*
* @class tinymce.dom.TreeWalker
* @example
* var walker = new tinymce.dom.TreeWalker(startNode);
*
* do {
* console.log(walker.current());
* } while (walker.next());
*/
define("tinymce/dom/TreeWalker", [], function() {
/**
* Constructs a new TreeWalker instance.
*
* @constructor
* @method TreeWalker
* @param {Node} startNode Node to start walking from.
* @param {node} rootNode Optional root node to never walk out of.
*/
return function(startNode, rootNode) {
var node = startNode;
function findSibling(node, startName, siblingName, shallow) {
var sibling, parent;
if (node) {
// Walk into nodes if it has a start
if (!shallow && node[startName]) {
return node[startName];
}
// Return the sibling if it has one
if (node != rootNode) {
sibling = node[siblingName];
if (sibling) {
return sibling;
}
// Walk up the parents to look for siblings
for (parent = node.parentNode; parent && parent != rootNode; parent = parent.parentNode) {
sibling = parent[siblingName];
if (sibling) {
return sibling;
}
}
}
}
}
function findPreviousNode(node, startName, siblingName, shallow) {
var sibling, parent, child;
if (node) {
sibling = node[siblingName];
if (rootNode && sibling === rootNode) {
return;
}
if (sibling) {
if (!shallow) {
// Walk up the parents to look for siblings
for (child = sibling[startName]; child; child = child[startName]) {
if (!child[startName]) {
return child;
}
}
}
return sibling;
}
parent = node.parentNode;
if (parent && parent !== rootNode) {
return parent;
}
}
}
/**
* Returns the current node.
*
* @method current
* @return {Node} Current node where the walker is.
*/
this.current = function() {
return node;
};
/**
* Walks to the next node in tree.
*
* @method next
* @return {Node} Current node where the walker is after moving to the next node.
*/
this.next = function(shallow) {
node = findSibling(node, 'firstChild', 'nextSibling', shallow);
return node;
};
/**
* Walks to the previous node in tree.
*
* @method prev
* @return {Node} Current node where the walker is after moving to the previous node.
*/
this.prev = function(shallow) {
node = findSibling(node, 'lastChild', 'previousSibling', shallow);
return node;
};
this.prev2 = function(shallow) {
node = findPreviousNode(node, 'lastChild', 'previousSibling', shallow);
return node;
};
};
});