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