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