Chromium Code Reviews| Index: chrome/browser/resources/chromeos/chromevox/cvox2/background/automation_util.js |
| diff --git a/chrome/browser/resources/chromeos/chromevox/cvox2/background/automation_util.js b/chrome/browser/resources/chromeos/chromevox/cvox2/background/automation_util.js |
| index 6ce94eae686945a6f3481b7396a5018a045bc123..ec8fe08fa0a7ab679d2bf33a4b7dd20e5034a2d6 100644 |
| --- a/chrome/browser/resources/chromeos/chromevox/cvox2/background/automation_util.js |
| +++ b/chrome/browser/resources/chromeos/chromevox/cvox2/background/automation_util.js |
| @@ -30,6 +30,7 @@ AutomationUtil.Dir = { |
| goog.scope(function() { |
| +var AutomationNode = chrome.automation.AutomationNode; |
| var Dir = AutomationUtil.Dir; |
| /** |
| @@ -154,4 +155,49 @@ AutomationUtil.findNodeUntil = function(cur, dir, pred, opt_options) { |
| return opt_options.before ? before : after; |
| }; |
| + |
| +/** |
| + * 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.
|
| + * @param {!AutomationNode} node |
| + * @return {Array.<AutomationNode>} |
| + */ |
| +AutomationUtil.getAncestors = function(node) { |
|
Peter Lundblad
2014/10/23 13:35:07
nit: consider calling getAncestorsAndSelf or somet
|
| + var result = [node]; |
| + var target = node; |
| + while (target = target.parent()) |
| + result.push(target); |
| + return result; |
| +}; |
| + |
| +/** |
| + * 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.
|
| + * @param {!AutomationNode} first |
| + * @param {!AutomationNode} second |
| + * @return {boolean} |
| + */ |
| +AutomationUtil.isBefore = function(first, second) { |
|
dmazzoni
2014/10/17 16:46:08
How about a unit test for this?
I think it's wort
|
| + if (first === second) |
| + return true; |
| + |
| + var firstA = AutomationUtil.getAncestors(first); |
| + var secondA = AutomationUtil.getAncestors(second); |
| + var lcaIndex; |
|
Peter Lundblad
2014/10/23 13:35:07
What does 'lca' mean?
|
| + for (var i = firstA.length - 1, j = secondA.length - 1; |
| + firstA[i] === secondA[j]; |
| + i--, j--) |
| + lcaIndex = [i, j]; |
| + |
| + if (lcaIndex) { |
| + var firstC = firstA[lcaIndex[0] - 1]; |
| + var secondC = secondA[lcaIndex[1] - 1]; |
| + var firstCount = 0, secondCount = 0; |
| + // TODO(dtseng): Expose parent in index (chrome.automation.AutomationNode). |
|
dmazzoni
2014/10/17 16:46:08
How about writing an indexInParent helper, which c
|
| + while (firstC = firstC.nextSibling()) firstCount++; |
| + firstCount++; |
|
Peter Lundblad
2014/10/23 13:35:07
Looks like one increment too much here.
|
| + while (secondC = secondC.nextSibling()) |
| + secondCount++; |
| + return firstCount <= secondCount; |
| + } |
| +}; |
|
Peter Lundblad
2014/10/23 13:35:07
This function is unnecessarily cryptic because of
|
| + |
| }); // goog.scope |