Chromium Code Reviews| 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 12 matching lines...) Expand all Loading... | |
| 23 AutomationUtil.Dir = { | 23 AutomationUtil.Dir = { |
| 24 // Search from left to right. | 24 // Search from left to right. |
| 25 FORWARD: 'forward', | 25 FORWARD: 'forward', |
| 26 | 26 |
| 27 // Search from right to left. | 27 // Search from right to left. |
| 28 BACKWARD: 'backward' | 28 BACKWARD: 'backward' |
| 29 }; | 29 }; |
| 30 | 30 |
| 31 | 31 |
| 32 goog.scope(function() { | 32 goog.scope(function() { |
| 33 var AutomationNode = chrome.automation.AutomationNode; | |
| 33 var Dir = AutomationUtil.Dir; | 34 var Dir = AutomationUtil.Dir; |
| 34 | 35 |
| 35 /** | 36 /** |
| 36 * Find a node in subtree of |cur| satisfying |pred| using pre-order traversal. | 37 * Find a node in subtree of |cur| satisfying |pred| using pre-order traversal. |
| 37 * @param {chrome.automation.AutomationNode} cur Node to begin the search from. | 38 * @param {chrome.automation.AutomationNode} cur Node to begin the search from. |
|
dmazzoni
2014/10/17 16:46:08
Please replace all chrome.automation.AutomationNod
| |
| 38 * @param {Dir} dir | 39 * @param {Dir} dir |
| 39 * @param {AutomationPredicate.Unary} pred A predicate to apply | 40 * @param {AutomationPredicate.Unary} pred A predicate to apply |
| 40 * to a candidate node. | 41 * to a candidate node. |
| 41 * @return {chrome.automation.AutomationNode} | 42 * @return {chrome.automation.AutomationNode} |
| 42 */ | 43 */ |
| 43 AutomationUtil.findNodePre = function(cur, dir, pred) { | 44 AutomationUtil.findNodePre = function(cur, dir, pred) { |
| 44 if (pred(cur)) | 45 if (pred(cur)) |
| 45 return cur; | 46 return cur; |
| 46 | 47 |
| 47 var child = dir == Dir.BACKWARD ? cur.lastChild() : cur.firstChild(); | 48 var child = dir == Dir.BACKWARD ? cur.lastChild() : cur.firstChild(); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 prev = candidate; | 148 prev = candidate; |
| 148 if (!satisfied) | 149 if (!satisfied) |
| 149 before = candidate; | 150 before = candidate; |
| 150 else | 151 else |
| 151 after = candidate; | 152 after = candidate; |
| 152 return satisfied; | 153 return satisfied; |
| 153 }); | 154 }); |
| 154 return opt_options.before ? before : after; | 155 return opt_options.before ? before : after; |
| 155 }; | 156 }; |
| 156 | 157 |
| 158 | |
| 159 /** | |
| 160 * Returns ancestors of the given node (inclusive). | |
|
dmazzoni
2014/10/17 16:46:08
Specify the order in which it returns the ancestor
Peter Lundblad
2014/10/23 13:35:07
If order is guaranteed, specify that.
| |
| 161 * @param {!AutomationNode} node | |
| 162 * @return {Array.<AutomationNode>} | |
| 163 */ | |
| 164 AutomationUtil.getAncestors = function(node) { | |
|
Peter Lundblad
2014/10/23 13:35:07
nit: consider calling getAncestorsAndSelf or somet
| |
| 165 var result = [node]; | |
| 166 var target = node; | |
| 167 while (target = target.parent()) | |
| 168 result.push(target); | |
| 169 return result; | |
| 170 }; | |
| 171 | |
| 172 /** | |
| 173 * Returns whether |first| comes before |second| in Dir.FORWARD traversal. | |
|
Peter Lundblad
2014/10/23 13:35:07
Should specify if that this is not strict.
| |
| 174 * @param {!AutomationNode} first | |
| 175 * @param {!AutomationNode} second | |
| 176 * @return {boolean} | |
| 177 */ | |
| 178 AutomationUtil.isBefore = function(first, second) { | |
|
dmazzoni
2014/10/17 16:46:08
How about a unit test for this?
I think it's wort
| |
| 179 if (first === second) | |
| 180 return true; | |
| 181 | |
| 182 var firstA = AutomationUtil.getAncestors(first); | |
| 183 var secondA = AutomationUtil.getAncestors(second); | |
| 184 var lcaIndex; | |
|
Peter Lundblad
2014/10/23 13:35:07
What does 'lca' mean?
| |
| 185 for (var i = firstA.length - 1, j = secondA.length - 1; | |
| 186 firstA[i] === secondA[j]; | |
| 187 i--, j--) | |
| 188 lcaIndex = [i, j]; | |
| 189 | |
| 190 if (lcaIndex) { | |
| 191 var firstC = firstA[lcaIndex[0] - 1]; | |
| 192 var secondC = secondA[lcaIndex[1] - 1]; | |
| 193 var firstCount = 0, secondCount = 0; | |
| 194 // TODO(dtseng): Expose parent in index (chrome.automation.AutomationNode). | |
|
dmazzoni
2014/10/17 16:46:08
How about writing an indexInParent helper, which c
| |
| 195 while (firstC = firstC.nextSibling()) firstCount++; | |
| 196 firstCount++; | |
|
Peter Lundblad
2014/10/23 13:35:07
Looks like one increment too much here.
| |
| 197 while (secondC = secondC.nextSibling()) | |
| 198 secondCount++; | |
| 199 return firstCount <= secondCount; | |
| 200 } | |
| 201 }; | |
|
Peter Lundblad
2014/10/23 13:35:07
This function is unnecessarily cryptic because of
| |
| 202 | |
| 157 }); // goog.scope | 203 }); // goog.scope |
| OLD | NEW |