OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 ChromeVox utilities for the automation extension API. | 6 * @fileoverview ChromeVox utilities for the automation extension API. |
7 */ | 7 */ |
8 | 8 |
9 goog.provide('AutomationUtil'); | 9 goog.provide('AutomationUtil'); |
10 goog.provide('AutomationUtil.Dir'); | 10 goog.provide('AutomationUtil.Dir'); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 * Find the next node in the given direction that is either an immediate sibling | 81 * Find the next node in the given direction that is either an immediate sibling |
82 * or a sibling of an ancestor. | 82 * or a sibling of an ancestor. |
83 * @param {AutomationNode} cur Node to start search from. | 83 * @param {AutomationNode} cur Node to start search from. |
84 * @param {Dir} dir | 84 * @param {Dir} dir |
85 * @return {AutomationNode} | 85 * @return {AutomationNode} |
86 */ | 86 */ |
87 AutomationUtil.findNextSubtree = function(cur, dir) { | 87 AutomationUtil.findNextSubtree = function(cur, dir) { |
88 while (cur) { | 88 while (cur) { |
89 var next = dir == Dir.BACKWARD ? | 89 var next = dir == Dir.BACKWARD ? |
90 cur.previousSibling : cur.nextSibling; | 90 cur.previousSibling : cur.nextSibling; |
| 91 if (!AutomationUtil.isInSameTree(cur, next)) |
| 92 return null; |
91 if (next) | 93 if (next) |
92 return next; | 94 return next; |
| 95 if (!AutomationUtil.isInSameTree(cur, cur.parent)) |
| 96 return null; |
93 cur = cur.parent; | 97 cur = cur.parent; |
94 } | 98 } |
95 }; | 99 }; |
96 | 100 |
97 /** | 101 /** |
98 * Find the next node in the given direction in depth first order. | 102 * Find the next node in the given direction in depth first order. |
99 * @param {AutomationNode} cur Node to begin the search from. | 103 * @param {AutomationNode} cur Node to begin the search from. |
100 * @param {Dir} dir | 104 * @param {Dir} dir |
101 * @param {AutomationPredicate.Unary} pred A predicate to apply | 105 * @param {AutomationPredicate.Unary} pred A predicate to apply |
102 * to a candidate node. | 106 * to a candidate node. |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 /** | 161 /** |
158 * Returns an array containing ancestors of node starting at root down to node. | 162 * Returns an array containing ancestors of node starting at root down to node. |
159 * @param {!AutomationNode} node | 163 * @param {!AutomationNode} node |
160 * @return {!Array.<AutomationNode>} | 164 * @return {!Array.<AutomationNode>} |
161 */ | 165 */ |
162 AutomationUtil.getAncestors = function(node) { | 166 AutomationUtil.getAncestors = function(node) { |
163 var ret = []; | 167 var ret = []; |
164 var candidate = node; | 168 var candidate = node; |
165 while (candidate) { | 169 while (candidate) { |
166 ret.push(candidate); | 170 ret.push(candidate); |
| 171 |
| 172 if (!AutomationUtil.isInSameTree(candidate, candidate.parent)) |
| 173 break; |
| 174 |
167 candidate = candidate.parent; | 175 candidate = candidate.parent; |
168 } | 176 } |
169 return ret.reverse(); | 177 return ret.reverse(); |
170 }; | 178 }; |
171 | 179 |
172 /** | 180 /** |
173 * Gets the first index where the two input arrays differ. Returns -1 if they | 181 * Gets the first index where the two input arrays differ. Returns -1 if they |
174 * do not. | 182 * do not. |
175 * @param {!Array.<AutomationNode>} ancestorsA | 183 * @param {!Array.<AutomationNode>} ancestorsA |
176 * @param {!Array.<AutomationNode>} ancestorsB | 184 * @param {!Array.<AutomationNode>} ancestorsB |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 var divB = ancestorsB[divergence]; | 227 var divB = ancestorsB[divergence]; |
220 | 228 |
221 // One of the nodes is an ancestor of the other. Don't distinguish and just | 229 // One of the nodes is an ancestor of the other. Don't distinguish and just |
222 // consider it Dir.FORWARD. | 230 // consider it Dir.FORWARD. |
223 if (!divA || !divB || divA.parent === nodeB || divB.parent === nodeA) | 231 if (!divA || !divB || divA.parent === nodeB || divB.parent === nodeA) |
224 return Dir.FORWARD; | 232 return Dir.FORWARD; |
225 | 233 |
226 return divA.indexInParent <= divB.indexInParent ? Dir.FORWARD : Dir.BACKWARD; | 234 return divA.indexInParent <= divB.indexInParent ? Dir.FORWARD : Dir.BACKWARD; |
227 }; | 235 }; |
228 | 236 |
| 237 /** |
| 238 * Determines whether the two given nodes come from the same tree source. |
| 239 * @param {AutomationNode} a |
| 240 * @param {AutomationNode} b |
| 241 * @return {boolean} |
| 242 */ |
| 243 AutomationUtil.isInSameTree = function(a, b) { |
| 244 if (!a || !b) |
| 245 return true; |
| 246 |
| 247 return a.root === b.root; |
| 248 }; |
| 249 |
229 }); // goog.scope | 250 }); // goog.scope |
OLD | NEW |