]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/tiny_mce/classes/dom/TreeWalker.js
Release 6.5.0
[Github/sugarcrm.git] / include / javascript / tiny_mce / classes / dom / TreeWalker.js
1 /**
2  * TreeWalker.js
3  *
4  * Copyright 2009, Moxiecode Systems AB
5  * Released under LGPL License.
6  *
7  * License: http://tinymce.moxiecode.com/license
8  * Contributing: http://tinymce.moxiecode.com/contributing
9  */
10
11 tinymce.dom.TreeWalker = function(start_node, root_node) {
12         var node = start_node;
13
14         function findSibling(node, start_name, sibling_name, shallow) {
15                 var sibling, parent;
16
17                 if (node) {
18                         // Walk into nodes if it has a start
19                         if (!shallow && node[start_name])
20                                 return node[start_name];
21
22                         // Return the sibling if it has one
23                         if (node != root_node) {
24                                 sibling = node[sibling_name];
25                                 if (sibling)
26                                         return sibling;
27
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];
31                                         if (sibling)
32                                                 return sibling;
33                                 }
34                         }
35                 }
36         };
37
38         /**
39          * Returns the current node.
40          *
41          * @return {Node} Current node where the walker is.
42          */
43         this.current = function() {
44                 return node;
45         };
46
47         /**
48          * Walks to the next node in tree.
49          *
50          * @return {Node} Current node where the walker is after moving to the next node.
51          */
52         this.next = function(shallow) {
53                 return (node = findSibling(node, 'firstChild', 'nextSibling', shallow));
54         };
55
56         /**
57          * Walks to the previous node in tree.
58          *
59          * @return {Node} Current node where the walker is after moving to the previous node.
60          */
61         this.prev = function(shallow) {
62                 return (node = findSibling(node, 'lastChild', 'previousSibling', shallow));
63         };
64 };