4 * Copyright, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://www.tinymce.com/license
8 * Contributing: http://www.tinymce.com/contributing
12 * TreeWalker class enables you to walk the DOM in a linear manner.
14 * @class tinymce.dom.TreeWalker
16 define("tinymce/dom/TreeWalker", [], function() {
17 return function(start_node, root_node) {
18 var node = start_node;
20 function findSibling(node, start_name, sibling_name, shallow) {
24 // Walk into nodes if it has a start
25 if (!shallow && node[start_name]) {
26 return node[start_name];
29 // Return the sibling if it has one
30 if (node != root_node) {
31 sibling = node[sibling_name];
36 // Walk up the parents to look for siblings
37 for (parent = node.parentNode; parent && parent != root_node; parent = parent.parentNode) {
38 sibling = parent[sibling_name];
48 * Returns the current node.
51 * @return {Node} Current node where the walker is.
53 this.current = function() {
58 * Walks to the next node in tree.
61 * @return {Node} Current node where the walker is after moving to the next node.
63 this.next = function(shallow) {
64 node = findSibling(node, 'firstChild', 'nextSibling', shallow);
69 * Walks to the previous node in tree.
72 * @return {Node} Current node where the walker is after moving to the previous node.
74 this.prev = function(shallow) {
75 node = findSibling(node, 'lastChild', 'previousSibling', shallow);