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; | |
93 if (next) | 91 if (next) |
94 return next; | 92 return next; |
95 if (!AutomationUtil.isInSameTree(cur, cur.parent)) | |
96 return null; | |
97 cur = cur.parent; | 93 cur = cur.parent; |
98 } | 94 } |
99 }; | 95 }; |
100 | 96 |
101 /** | 97 /** |
102 * Find the next node in the given direction in depth first order. | 98 * Find the next node in the given direction in depth first order. |
103 * @param {AutomationNode} cur Node to begin the search from. | 99 * @param {AutomationNode} cur Node to begin the search from. |
104 * @param {Dir} dir | 100 * @param {Dir} dir |
105 * @param {AutomationPredicate.Unary} pred A predicate to apply | 101 * @param {AutomationPredicate.Unary} pred A predicate to apply |
106 * to a candidate node. | 102 * to a candidate node. |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 /** | 157 /** |
162 * Returns an array containing ancestors of node starting at root down to node. | 158 * Returns an array containing ancestors of node starting at root down to node. |
163 * @param {!AutomationNode} node | 159 * @param {!AutomationNode} node |
164 * @return {!Array.<AutomationNode>} | 160 * @return {!Array.<AutomationNode>} |
165 */ | 161 */ |
166 AutomationUtil.getAncestors = function(node) { | 162 AutomationUtil.getAncestors = function(node) { |
167 var ret = []; | 163 var ret = []; |
168 var candidate = node; | 164 var candidate = node; |
169 while (candidate) { | 165 while (candidate) { |
170 ret.push(candidate); | 166 ret.push(candidate); |
171 | |
172 if (!AutomationUtil.isInSameTree(candidate, candidate.parent)) | |
173 break; | |
174 | |
175 candidate = candidate.parent; | 167 candidate = candidate.parent; |
176 } | 168 } |
177 return ret.reverse(); | 169 return ret.reverse(); |
178 }; | 170 }; |
179 | 171 |
180 /** | 172 /** |
181 * Gets the first index where the two input arrays differ. Returns -1 if they | 173 * Gets the first index where the two input arrays differ. Returns -1 if they |
182 * do not. | 174 * do not. |
183 * @param {!Array.<AutomationNode>} ancestorsA | 175 * @param {!Array.<AutomationNode>} ancestorsA |
184 * @param {!Array.<AutomationNode>} ancestorsB | 176 * @param {!Array.<AutomationNode>} ancestorsB |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 var divB = ancestorsB[divergence]; | 219 var divB = ancestorsB[divergence]; |
228 | 220 |
229 // One of the nodes is an ancestor of the other. Don't distinguish and just | 221 // One of the nodes is an ancestor of the other. Don't distinguish and just |
230 // consider it Dir.FORWARD. | 222 // consider it Dir.FORWARD. |
231 if (!divA || !divB || divA.parent === nodeB || divB.parent === nodeA) | 223 if (!divA || !divB || divA.parent === nodeB || divB.parent === nodeA) |
232 return Dir.FORWARD; | 224 return Dir.FORWARD; |
233 | 225 |
234 return divA.indexInParent <= divB.indexInParent ? Dir.FORWARD : Dir.BACKWARD; | 226 return divA.indexInParent <= divB.indexInParent ? Dir.FORWARD : Dir.BACKWARD; |
235 }; | 227 }; |
236 | 228 |
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 | |
250 }); // goog.scope | 229 }); // goog.scope |
OLD | NEW |