OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview A tree walker over the automation tree. | 6 * @fileoverview A tree walker over the automation tree. |
7 */ | 7 */ |
8 | 8 |
9 goog.provide('AutomationTreeWalker'); | 9 goog.provide('AutomationTreeWalker'); |
10 goog.provide('AutomationTreeWalkerPhase'); | 10 goog.provide('AutomationTreeWalkerPhase'); |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 | 171 |
172 if (!this.skipInitialSubtree_ || | 172 if (!this.skipInitialSubtree_ || |
173 this.phase != AutomationTreeWalkerPhase.DESCENDANT) { | 173 this.phase != AutomationTreeWalkerPhase.DESCENDANT) { |
174 this.node_ = node.firstChild; | 174 this.node_ = node.firstChild; |
175 return; | 175 return; |
176 } | 176 } |
177 } | 177 } |
178 | 178 |
179 var searchNode = node; | 179 var searchNode = node; |
180 while (searchNode) { | 180 while (searchNode) { |
181 // We have crossed out of the initial node's subtree. | 181 // We have crossed out of the initial node's subtree for either a |
| 182 // sibling or parent move. |
182 if (searchNode == this.initialNode_) | 183 if (searchNode == this.initialNode_) |
183 this.phase_ = AutomationTreeWalkerPhase.OTHER; | 184 this.phase_ = AutomationTreeWalkerPhase.OTHER; |
184 | 185 |
185 if (searchNode.nextSibling) { | 186 if (searchNode.nextSibling) { |
186 this.node_ = searchNode.nextSibling; | 187 this.node_ = searchNode.nextSibling; |
187 return; | 188 return; |
188 } | 189 } |
189 if (searchNode.parent && this.rootPred_(searchNode.parent)) | 190 |
| 191 // Update the phase based on the parent if needed since we may exit below. |
| 192 if (searchNode.parent == this.initialNode_) |
| 193 this.phase_ = AutomationTreeWalkerPhase.OTHER; |
| 194 |
| 195 // Exit if we encounter a root-like node and are not searching descendants |
| 196 // of the initial node. |
| 197 if (searchNode.parent && |
| 198 this.rootPred_(searchNode.parent) && |
| 199 this.phase_ != AutomationTreeWalkerPhase.DESCENDANT) |
190 break; | 200 break; |
191 | 201 |
192 searchNode = searchNode.parent; | 202 searchNode = searchNode.parent; |
193 } | 203 } |
194 this.node_ = null; | 204 this.node_ = null; |
195 }, | 205 }, |
196 | 206 |
197 /** | 207 /** |
198 * @param {!chrome.automation.AutomationNode} node | 208 * @param {!chrome.automation.AutomationNode} node |
199 * @private | 209 * @private |
200 */ | 210 */ |
201 backward_: function(node) { | 211 backward_: function(node) { |
202 if (node.previousSibling) { | 212 if (node.previousSibling) { |
203 this.phase_ = AutomationTreeWalkerPhase.OTHER; | 213 this.phase_ = AutomationTreeWalkerPhase.OTHER; |
204 node = node.previousSibling; | 214 node = node.previousSibling; |
205 | 215 |
206 while (!this.leafPred_(node) && node.lastChild) | 216 while (!this.leafPred_(node) && node.lastChild) |
207 node = node.lastChild; | 217 node = node.lastChild; |
208 | 218 |
209 this.node_ = node; | 219 this.node_ = node; |
210 return; | 220 return; |
211 } | 221 } |
212 if (node.parent && this.backwardAncestor_ == node.parent) { | 222 if (node.parent && this.backwardAncestor_ == node.parent) { |
213 this.phase_ = AutomationTreeWalkerPhase.ANCESTOR; | 223 this.phase_ = AutomationTreeWalkerPhase.ANCESTOR; |
214 this.backwardAncestor_ = node.parent.parent; | 224 this.backwardAncestor_ = node.parent.parent; |
215 } | 225 } |
216 this.node_ = node.parent; | 226 this.node_ = node.parent; |
217 } | 227 } |
218 }; | 228 }; |
OLD | NEW |