Chromium Code Reviews| 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|. | |
| 48 * @param {AutomationNode} cur Node to begin the search from. | |
| 49 * @param {boolean} r False to search forward (left to right), true to search | |
| 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.findNode = 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.findNode(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|. Differs from |findNode| | |
| 70 * in-terms of ordering: checks while unwinding stack. | |
| 71 * @param {AutomationNode} cur Node to begin the search from. | |
| 72 * @param {boolean} r False to search forward (left to right), true to search | |
| 73 * backward (right to left). | |
| 74 * @param {function(AutomationNode) : boolean} pred A predicate to apply to a | |
| 75 * candidate node. | |
| 76 * @return {AutomationNode} | |
| 77 */ | |
| 78 cvox2.AutomationUtil.findDeepestNode = function(cur, r, pred) { | |
|
dmazzoni
2014/09/26 06:08:52
Hmmm, this doesn't really find the "deepest" node
| |
| 79 var child = r ? cur.lastChild() : cur.firstChild(); | |
| 80 while (child) { | |
| 81 var ret = cvox2.AutomationUtil.findDeepestNode(child, r, pred); | |
| 82 if (ret) | |
| 83 return ret; | |
| 84 child = r ? child.previousSibling() : child.nextSibling(); | |
| 85 } | |
| 86 | |
| 87 if (pred(cur)) | |
| 88 return cur; | |
| 89 }; | |
| 90 | |
| 91 /** | |
| 92 * Find the directed next node that is either an immediate sibling or a sibling | |
| 93 * of an ancestor. | |
| 94 * @param {AutomationNode} cur Node to start search from. | |
| 95 * @param {boolean} r False to search forward (left to right), true to search | |
| 96 * backward (right to left). | |
| 97 * @return {AutomationNode} | |
| 98 */ | |
| 99 cvox2.AutomationUtil.findNextSubtree = function(cur, r) { | |
| 100 while (cur) { | |
| 101 var next = r ? cur.previousSibling() : cur.nextSibling(); | |
| 102 if (next) | |
| 103 return next; | |
| 104 | |
| 105 cur = cur.parent(); | |
| 106 } | |
| 107 }; | |
| 108 | |
| 109 /** | |
| 110 * Find the directed next node in depth first order. | |
| 111 * @param {AutomationNode} cur Node to begin the search from. | |
| 112 * @param {boolean} r False to search forward (left to right), true to search | |
| 113 * backward (right to left). | |
| 114 * @param {function(AutomationNode) : boolean} pred A predicate to apply to a | |
| 115 * candidate node. | |
| 116 * @return {AutomationNode} | |
| 117 */ | |
| 118 cvox2.AutomationUtil.findNextNode = function(cur, r, pred) { | |
| 119 var next = cur; | |
| 120 do { | |
| 121 if (!(next = cvox2.AutomationUtil.findNextSubtree(cur, r))) | |
| 122 return null; | |
| 123 cur = next; | |
| 124 next = cvox2.AutomationUtil.findNode(next, r, pred); | |
| 125 } while (!next); | |
| 126 return next; | |
| 127 }; | |
| OLD | NEW |