OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @fileoverview ChromeVox utilities for the automation extension API. |
| 7 */ |
| 8 |
| 9 goog.provide('cvox2.AutomationPredicates'); |
| 10 goog.provide('cvox2.AutomationUtil'); |
| 11 goog.provide('cvox2.Dir'); |
| 12 |
| 13 /** |
| 14 * @constructor |
| 15 */ |
| 16 cvox2.AutomationPredicates = function() {}; |
| 17 |
| 18 /** |
| 19 * Constructs a predicate given a role. |
| 20 * @param {string} role |
| 21 * @return {function(AutomationNode) : boolean} |
| 22 */ |
| 23 cvox2.AutomationPredicates.makeRolePredicate = function(role) { |
| 24 return function(node) { |
| 25 return node.role == role; |
| 26 }; |
| 27 }; |
| 28 |
| 29 /** @type {function(AutomationNode) : boolean} */ |
| 30 cvox2.AutomationPredicates.heading = |
| 31 cvox2.AutomationPredicates.makeRolePredicate( |
| 32 chrome.automation.RoleType.heading); |
| 33 /** @type {function(AutomationNode) : boolean} */ |
| 34 cvox2.AutomationPredicates.inlineTextBox = |
| 35 cvox2.AutomationPredicates.makeRolePredicate( |
| 36 chrome.automation.RoleType.inlineTextBox); |
| 37 /** @type {function(AutomationNode) : boolean} */ |
| 38 cvox2.AutomationPredicates.link = |
| 39 cvox2.AutomationPredicates.makeRolePredicate( |
| 40 chrome.automation.RoleType.link); |
| 41 |
| 42 /** |
| 43 * Possible directions to perform tree traversals. |
| 44 * @enum {string} |
| 45 */ |
| 46 cvox2.Dir = { |
| 47 // Search from left to right. |
| 48 FORWARD: 'forward', |
| 49 |
| 50 // Search from right to left. |
| 51 BACKWARD: 'backward' |
| 52 }; |
| 53 |
| 54 /** |
| 55 * @constructor |
| 56 */ |
| 57 cvox2.AutomationUtil = function() {}; |
| 58 |
| 59 /** |
| 60 * Find a node in subtree of |cur| satisfying |pred| using pre-order traversal. |
| 61 * @param {AutomationNode} cur Node to begin the search from. |
| 62 * @param {cvox2.Dir} dir |
| 63 * @param {function(AutomationNode) : boolean} pred A predicate to apply to a |
| 64 * candidate node. |
| 65 * @return {AutomationNode} |
| 66 */ |
| 67 cvox2.AutomationUtil.findNodePre = function(cur, dir, pred) { |
| 68 if (pred(cur)) |
| 69 return cur; |
| 70 |
| 71 var child = dir == cvox2.Dir.BACKWARD ? cur.lastChild() : cur.firstChild(); |
| 72 while (child) { |
| 73 var ret = cvox2.AutomationUtil.findNodePre(child, dir, pred); |
| 74 if (ret) |
| 75 return ret; |
| 76 child = dir == cvox2.Dir.BACKWARD ? |
| 77 child.previousSibling() : child.nextSibling(); |
| 78 } |
| 79 }; |
| 80 |
| 81 /** |
| 82 * Find a node in subtree of |cur| satisfying |pred| using post-order traversal. |
| 83 * @param {AutomationNode} cur Node to begin the search from. |
| 84 * @param {cvox2.Dir} dir |
| 85 * @param {function(AutomationNode) : boolean} pred A predicate to apply to a |
| 86 * candidate node. |
| 87 * @return {AutomationNode} |
| 88 */ |
| 89 cvox2.AutomationUtil.findNodePost = function(cur, dir, pred) { |
| 90 var child = dir == cvox2.Dir.BACKWARD ? cur.lastChild() : cur.firstChild(); |
| 91 while (child) { |
| 92 var ret = cvox2.AutomationUtil.findNodePost(child, dir, pred); |
| 93 if (ret) |
| 94 return ret; |
| 95 child = dir == cvox2.Dir.BACKWARD ? |
| 96 child.previousSibling() : child.nextSibling(); |
| 97 } |
| 98 |
| 99 if (pred(cur)) |
| 100 return cur; |
| 101 }; |
| 102 |
| 103 /** |
| 104 * Find the next node in the given direction that is either an immediate |
| 105 * sibling or a sibling of an ancestor. |
| 106 * @param {AutomationNode} cur Node to start search from. |
| 107 * @param {cvox2.Dir} dir |
| 108 * @return {AutomationNode} |
| 109 */ |
| 110 cvox2.AutomationUtil.findNextSubtree = function(cur, dir) { |
| 111 while (cur) { |
| 112 var next = dir == cvox2.Dir.BACKWARD ? |
| 113 cur.previousSibling() : cur.nextSibling(); |
| 114 if (next) |
| 115 return next; |
| 116 |
| 117 cur = cur.parent(); |
| 118 } |
| 119 }; |
| 120 |
| 121 /** |
| 122 * Find the next node in the given direction in depth first order. |
| 123 * @param {AutomationNode} cur Node to begin the search from. |
| 124 * @param {cvox2.Dir} dir |
| 125 * @param {function(AutomationNode) : boolean} pred A predicate to apply to a |
| 126 * candidate node. |
| 127 * @return {AutomationNode} |
| 128 */ |
| 129 cvox2.AutomationUtil.findNextNode = function(cur, dir, pred) { |
| 130 var next = cur; |
| 131 do { |
| 132 if (!(next = cvox2.AutomationUtil.findNextSubtree(cur, dir))) |
| 133 return null; |
| 134 cur = next; |
| 135 next = cvox2.AutomationUtil.findNodePre(next, dir, pred); |
| 136 } while (!next); |
| 137 return next; |
| 138 }; |
OLD | NEW |