| 1 |
"use strict"; |
| 2 |
module.exports = TreeWalker; |
| 3 |
|
| 4 |
var Node = require('./Node'); |
| 5 |
var NodeFilter = require('./NodeFilter'); |
| 6 |
var NodeTraversal = require('./NodeTraversal'); |
| 7 |
var utils = require('./utils'); |
| 8 |
|
| 9 |
var mapChild = { |
| 10 |
first: 'firstChild', |
| 11 |
last: 'lastChild', |
| 12 |
next: 'firstChild', |
| 13 |
previous: 'lastChild' |
| 14 |
}; |
| 15 |
|
| 16 |
var mapSibling = { |
| 17 |
first: 'nextSibling', |
| 18 |
last: 'previousSibling', |
| 19 |
next: 'nextSibling', |
| 20 |
previous: 'previousSibling' |
| 21 |
}; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
function traverseChildren(tw, type) { |
| 34 |
var child, node, parent, result, sibling; |
| 35 |
node = tw._currentNode[mapChild[type]]; |
| 36 |
while (node !== null) { |
| 37 |
result = tw._internalFilter(node); |
| 38 |
if (result === NodeFilter.FILTER_ACCEPT) { |
| 39 |
tw._currentNode = node; |
| 40 |
return node; |
| 41 |
} |
| 42 |
if (result === NodeFilter.FILTER_SKIP) { |
| 43 |
child = node[mapChild[type]]; |
| 44 |
if (child !== null) { |
| 45 |
node = child; |
| 46 |
continue; |
| 47 |
} |
| 48 |
} |
| 49 |
while (node !== null) { |
| 50 |
sibling = node[mapSibling[type]]; |
| 51 |
if (sibling !== null) { |
| 52 |
node = sibling; |
| 53 |
break; |
| 54 |
} |
| 55 |
parent = node.parentNode; |
| 56 |
if (parent === null || parent === tw.root || parent === tw._currentNode) { |
| 57 |
return null; |
| 58 |
} else { |
| 59 |
node = parent; |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
return null; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
function traverseSiblings(tw, type) { |
| 75 |
var node, result, sibling; |
| 76 |
node = tw._currentNode; |
| 77 |
if (node === tw.root) { |
| 78 |
return null; |
| 79 |
} |
| 80 |
while (true) { |
| 81 |
sibling = node[mapSibling[type]]; |
| 82 |
while (sibling !== null) { |
| 83 |
node = sibling; |
| 84 |
result = tw._internalFilter(node); |
| 85 |
if (result === NodeFilter.FILTER_ACCEPT) { |
| 86 |
tw._currentNode = node; |
| 87 |
return node; |
| 88 |
} |
| 89 |
sibling = node[mapChild[type]]; |
| 90 |
if (result === NodeFilter.FILTER_REJECT || sibling === null) { |
| 91 |
sibling = node[mapSibling[type]]; |
| 92 |
} |
| 93 |
} |
| 94 |
node = node.parentNode; |
| 95 |
if (node === null || node === tw.root) { |
| 96 |
return null; |
| 97 |
} |
| 98 |
if (tw._internalFilter(node) === NodeFilter.FILTER_ACCEPT) { |
| 99 |
return null; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
function TreeWalker(root, whatToShow, filter) { |
| 117 |
if (!root || !root.nodeType) { |
| 118 |
utils.NotSupportedError(); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
this._root = root; |
| 123 |
this._whatToShow = Number(whatToShow) || 0; |
| 124 |
this._filter = filter || null; |
| 125 |
this._active = false; |
| 126 |
|
| 127 |
this._currentNode = root; |
| 128 |
} |
| 129 |
|
| 130 |
Object.defineProperties(TreeWalker.prototype, { |
| 131 |
root: { get: function() { return this._root; } }, |
| 132 |
whatToShow: { get: function() { return this._whatToShow; } }, |
| 133 |
filter: { get: function() { return this._filter; } }, |
| 134 |
|
| 135 |
currentNode: { |
| 136 |
get: function currentNode() { |
| 137 |
return this._currentNode; |
| 138 |
}, |
| 139 |
set: function setCurrentNode(v) { |
| 140 |
if (!(v instanceof Node)) { |
| 141 |
throw new TypeError("Not a Node"); |
| 142 |
} |
| 143 |
this._currentNode = v; |
| 144 |
}, |
| 145 |
}, |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
_internalFilter: { value: function _internalFilter(node) { |
| 154 |
|
| 155 |
var result, filter; |
| 156 |
if (this._active) { |
| 157 |
utils.InvalidStateError(); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
if (!(((1 << (node.nodeType - 1)) & this._whatToShow))) { |
| 162 |
return NodeFilter.FILTER_SKIP; |
| 163 |
} |
| 164 |
|
| 165 |
filter = this._filter; |
| 166 |
if (filter === null) { |
| 167 |
result = NodeFilter.FILTER_ACCEPT; |
| 168 |
} else { |
| 169 |
this._active = true; |
| 170 |
try { |
| 171 |
if (typeof filter === 'function') { |
| 172 |
result = filter(node); |
| 173 |
} else { |
| 174 |
result = filter.acceptNode(node); |
| 175 |
} |
| 176 |
} finally { |
| 177 |
this._active = false; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
return (+result); |
| 185 |
}}, |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
parentNode: { value: function parentNode() { |
| 195 |
var node = this._currentNode; |
| 196 |
while (node !== this.root) { |
| 197 |
node = node.parentNode; |
| 198 |
if (node === null) { |
| 199 |
return null; |
| 200 |
} |
| 201 |
if (this._internalFilter(node) === NodeFilter.FILTER_ACCEPT) { |
| 202 |
this._currentNode = node; |
| 203 |
return node; |
| 204 |
} |
| 205 |
} |
| 206 |
return null; |
| 207 |
}}, |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
firstChild: { value: function firstChild() { |
| 215 |
return traverseChildren(this, 'first'); |
| 216 |
}}, |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
lastChild: { value: function lastChild() { |
| 224 |
return traverseChildren(this, 'last'); |
| 225 |
}}, |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
previousSibling: { value: function previousSibling() { |
| 233 |
return traverseSiblings(this, 'previous'); |
| 234 |
}}, |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
nextSibling: { value: function nextSibling() { |
| 242 |
return traverseSiblings(this, 'next'); |
| 243 |
}}, |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
previousNode: { value: function previousNode() { |
| 253 |
var node, result, previousSibling, lastChild; |
| 254 |
node = this._currentNode; |
| 255 |
while (node !== this._root) { |
| 256 |
for (previousSibling = node.previousSibling; |
| 257 |
previousSibling; |
| 258 |
previousSibling = node.previousSibling) { |
| 259 |
node = previousSibling; |
| 260 |
result = this._internalFilter(node); |
| 261 |
if (result === NodeFilter.FILTER_REJECT) { |
| 262 |
continue; |
| 263 |
} |
| 264 |
for (lastChild = node.lastChild; |
| 265 |
lastChild; |
| 266 |
lastChild = node.lastChild) { |
| 267 |
node = lastChild; |
| 268 |
result = this._internalFilter(node); |
| 269 |
if (result === NodeFilter.FILTER_REJECT) { |
| 270 |
break; |
| 271 |
} |
| 272 |
} |
| 273 |
if (result === NodeFilter.FILTER_ACCEPT) { |
| 274 |
this._currentNode = node; |
| 275 |
return node; |
| 276 |
} |
| 277 |
} |
| 278 |
if (node === this.root || node.parentNode === null) { |
| 279 |
return null; |
| 280 |
} |
| 281 |
node = node.parentNode; |
| 282 |
if (this._internalFilter(node) === NodeFilter.FILTER_ACCEPT) { |
| 283 |
this._currentNode = node; |
| 284 |
return node; |
| 285 |
} |
| 286 |
} |
| 287 |
return null; |
| 288 |
}}, |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
nextNode: { value: function nextNode() { |
| 298 |
var node, result, firstChild, nextSibling; |
| 299 |
node = this._currentNode; |
| 300 |
result = NodeFilter.FILTER_ACCEPT; |
| 301 |
|
| 302 |
CHILDREN: |
| 303 |
while (true) { |
| 304 |
for (firstChild = node.firstChild; |
| 305 |
firstChild; |
| 306 |
firstChild = node.firstChild) { |
| 307 |
node = firstChild; |
| 308 |
result = this._internalFilter(node); |
| 309 |
if (result === NodeFilter.FILTER_ACCEPT) { |
| 310 |
this._currentNode = node; |
| 311 |
return node; |
| 312 |
} else if (result === NodeFilter.FILTER_REJECT) { |
| 313 |
break; |
| 314 |
} |
| 315 |
} |
| 316 |
for (nextSibling = NodeTraversal.nextSkippingChildren(node, this.root); |
| 317 |
nextSibling; |
| 318 |
nextSibling = NodeTraversal.nextSkippingChildren(node, this.root)) { |
| 319 |
node = nextSibling; |
| 320 |
result = this._internalFilter(node); |
| 321 |
if (result === NodeFilter.FILTER_ACCEPT) { |
| 322 |
this._currentNode = node; |
| 323 |
return node; |
| 324 |
} else if (result === NodeFilter.FILTER_SKIP) { |
| 325 |
continue CHILDREN; |
| 326 |
} |
| 327 |
} |
| 328 |
return null; |
| 329 |
} |
| 330 |
}}, |
| 331 |
|
| 332 |
|
| 333 |
toString: { value: function toString() { |
| 334 |
return "[object TreeWalker]"; |
| 335 |
}}, |
| 336 |
}); |