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