4 * Copyright 2009, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://tinymce.moxiecode.com/license
8 * Contributing: http://tinymce.moxiecode.com/contributing
11 tinymce.dom.TreeWalker = function(start_node, root_node) {
12 var node = start_node;
14 function findSibling(node, start_name, sibling_name, shallow) {
18 // Walk into nodes if it has a start
19 if (!shallow && node[start_name])
20 return node[start_name];
22 // Return the sibling if it has one
23 if (node != root_node) {
24 sibling = node[sibling_name];
28 // Walk up the parents to look for siblings
29 for (parent = node.parentNode; parent && parent != root_node; parent = parent.parentNode) {
30 sibling = parent[sibling_name];
39 * Returns the current node.
41 * @return {Node} Current node where the walker is.
43 this.current = function() {
48 * Walks to the next node in tree.
50 * @return {Node} Current node where the walker is after moving to the next node.
52 this.next = function(shallow) {
53 return (node = findSibling(node, 'firstChild', 'nextSibling', shallow));
57 * Walks to the previous node in tree.
59 * @return {Node} Current node where the walker is after moving to the previous node.
61 this.prev = function(shallow) {
62 return (node = findSibling(node, 'lastChild', 'previousSibling', shallow));