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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..57a356591b515067384f1893c7e234f125575c89 |
| --- /dev/null |
| +++ b/chrome/browser/resources/chromeos/chromevox/cvox2/background/automation_util.js |
| @@ -0,0 +1,126 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +/** |
| + * @fileoverview ChromeVox utilities for the automation extension API. |
| + */ |
| + |
| +goog.provide('cvox2.AutomationPredicates'); |
| +goog.provide('cvox2.AutomationUtil'); |
| + |
| +/** |
| + * @constructor |
| + */ |
| +cvox2.AutomationPredicates = function() {}; |
| + |
| +/** |
| + * Constructs a predicate given a role. |
| + * @param {string} role |
| + * @return {function(AutomationNode) : boolean} |
| + */ |
| +cvox2.AutomationPredicates.makeRolePredicate = function(role) { |
| + return function(node) { |
| + return node.role == role; |
| + }; |
| +}; |
| + |
| +/** @type {function(AutomationNode) : boolean} */ |
| +cvox2.AutomationPredicates.heading = |
| + cvox2.AutomationPredicates.makeRolePredicate( |
| + chrome.automation.RoleType.heading); |
| +/** @type {function(AutomationNode) : boolean} */ |
| +cvox2.AutomationPredicates.inlineTextBox = |
| + cvox2.AutomationPredicates.makeRolePredicate( |
| + chrome.automation.RoleType.inlineTextBox); |
| +/** @type {function(AutomationNode) : boolean} */ |
| +cvox2.AutomationPredicates.link = |
| + cvox2.AutomationPredicates.makeRolePredicate( |
| + chrome.automation.RoleType.link); |
| + |
| +/** |
| + * @constructor |
| + */ |
| +cvox2.AutomationUtil = function() {}; |
| + |
| +/** |
| + * Find a node in subtree of |cur| satisfying |pred| using pre-order traversal. |
| + * @param {AutomationNode} cur Node to begin the search from. |
| + * @param {boolean} r False to search forward (left to right), true to search |
|
dmazzoni
2014/09/29 06:06:17
Let's make the type an enum rather than boolean, a
|
| + * backward (right to left). |
| + * @param {function(AutomationNode) : boolean} pred A predicate to apply to a |
| + * candidate node. |
| + * @return {AutomationNode} |
| + */ |
| +cvox2.AutomationUtil.findNodePre = function(cur, r, pred) { |
| + if (pred(cur)) |
| + return cur; |
| + |
| + var child = r ? cur.lastChild() : cur.firstChild(); |
| + while (child) { |
| + var ret = cvox2.AutomationUtil.findNodePre(child, r, pred); |
| + if (ret) |
| + return ret; |
| + child = r ? child.previousSibling() : child.nextSibling(); |
| + } |
| +}; |
| + |
| +/** |
| + * Find a node in subtree of |cur| satisfying |pred| using post-order traversal. |
| + * @param {AutomationNode} cur Node to begin the search from. |
| + * @param {boolean} r False to search forward (left to right), true to search |
| + * backward (right to left). |
| + * @param {function(AutomationNode) : boolean} pred A predicate to apply to a |
| + * candidate node. |
| + * @return {AutomationNode} |
| + */ |
| +cvox2.AutomationUtil.findNodePost = function(cur, r, pred) { |
| + var child = r ? cur.lastChild() : cur.firstChild(); |
| + while (child) { |
| + var ret = cvox2.AutomationUtil.findNodePost(child, r, pred); |
| + if (ret) |
| + return ret; |
| + child = r ? child.previousSibling() : child.nextSibling(); |
| + } |
| + |
| + if (pred(cur)) |
| + return cur; |
| +}; |
| + |
| +/** |
| + * Find the directed next node that is either an immediate sibling or a sibling |
|
dmazzoni
2014/09/29 06:06:17
By "directed next" do you mean, either the next no
David Tseng
2014/09/29 17:05:23
Done.
|
| + * of an ancestor. |
| + * @param {AutomationNode} cur Node to start search from. |
| + * @param {boolean} r False to search forward (left to right), true to search |
| + * backward (right to left). |
|
dmazzoni
2014/09/29 06:06:17
nit: indent 4 spaces - and elsewhere for additiona
David Tseng
2014/09/29 17:05:23
Done.
|
| + * @return {AutomationNode} |
| + */ |
| +cvox2.AutomationUtil.findNextSubtree = function(cur, r) { |
| + while (cur) { |
| + var next = r ? cur.previousSibling() : cur.nextSibling(); |
| + if (next) |
| + return next; |
| + |
| + cur = cur.parent(); |
| + } |
| +}; |
| + |
| +/** |
| + * Find the directed next node in depth first order. |
| + * @param {AutomationNode} cur Node to begin the search from. |
| + * @param {boolean} r False to search forward (left to right), true to search |
| + * backward (right to left). |
| + * @param {function(AutomationNode) : boolean} pred A predicate to apply to a |
| + * candidate node. |
| + * @return {AutomationNode} |
| + */ |
| +cvox2.AutomationUtil.findNextNode = function(cur, r, pred) { |
| + var next = cur; |
| + do { |
| + if (!(next = cvox2.AutomationUtil.findNextSubtree(cur, r))) |
| + return null; |
| + cur = next; |
| + next = cvox2.AutomationUtil.findNodePre(next, r, pred); |
| + } while (!next); |
| + return next; |
| +}; |