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 A collection of JavaScript utilities used to simplify working | 6 * @fileoverview A collection of JavaScript utilities used to simplify working |
7 * with xpaths. | 7 * with xpaths. |
8 */ | 8 */ |
9 | 9 |
10 | 10 |
11 goog.provide('cvox.XpathUtil'); | 11 goog.provide('cvox.XpathUtil'); |
12 | 12 |
13 | 13 |
14 /** | 14 /** |
15 * Utilities for simplifying working with xpaths | 15 * Utilities for simplifying working with xpaths |
16 * @constructor | 16 * @constructor |
17 */ | 17 */ |
18 cvox.XpathUtil = function() { | 18 cvox.XpathUtil = function() {}; |
19 }; | |
20 | 19 |
21 | 20 |
22 /** | 21 /** |
23 * Mapping for some default namespaces. | 22 * Mapping for some default namespaces. |
24 * @const | 23 * @const |
25 * @private | 24 * @private |
26 */ | 25 */ |
27 cvox.XpathUtil.nameSpaces_ = { | 26 cvox.XpathUtil.nameSpaces_ = { |
28 'xhtml' : 'http://www.w3.org/1999/xhtml', | 27 'xhtml': 'http://www.w3.org/1999/xhtml', |
29 'mathml': 'http://www.w3.org/1998/Math/MathML' | 28 'mathml': 'http://www.w3.org/1998/Math/MathML' |
30 }; | 29 }; |
31 | 30 |
32 | 31 |
33 /** | 32 /** |
34 * Resolve some default name spaces. | 33 * Resolve some default name spaces. |
35 * @param {string} prefix Namespace prefix. | 34 * @param {string} prefix Namespace prefix. |
36 * @return {string} The corresponding namespace URI. | 35 * @return {string} The corresponding namespace URI. |
37 */ | 36 */ |
38 cvox.XpathUtil.resolveNameSpace = function(prefix) { | 37 cvox.XpathUtil.resolveNameSpace = function(prefix) { |
39 return cvox.XpathUtil.nameSpaces_[prefix] || null; | 38 return cvox.XpathUtil.nameSpaces_[prefix] || null; |
40 }; | 39 }; |
41 | 40 |
42 | 41 |
43 /** | 42 /** |
44 * Given an XPath expression and rootNode, it returns an array of children nodes | 43 * Given an XPath expression and rootNode, it returns an array of children nodes |
45 * that match. The code for this function was taken from Mihai Parparita's GMail | 44 * that match. The code for this function was taken from Mihai Parparita's GMail |
46 * Macros Greasemonkey Script. | 45 * Macros Greasemonkey Script. |
47 * http://gmail-greasemonkey.googlecode.com/svn/trunk/scripts/gmail-new-macros.u
ser.js | 46 * http://gmail-greasemonkey.googlecode.com/svn/trunk/scripts/gmail-new-macros.u
ser.js |
48 * @param {string} expression The XPath expression to evaluate. | 47 * @param {string} expression The XPath expression to evaluate. |
49 * @param {Node} rootNode The HTML node to start evaluating the XPath from. | 48 * @param {Node} rootNode The HTML node to start evaluating the XPath from. |
50 * @return {Array} The array of children nodes that match. | 49 * @return {Array} The array of children nodes that match. |
51 */ | 50 */ |
52 cvox.XpathUtil.evalXPath = function(expression, rootNode) { | 51 cvox.XpathUtil.evalXPath = function(expression, rootNode) { |
53 try { | 52 try { |
54 var xpathIterator = rootNode.ownerDocument.evaluate( | 53 var xpathIterator = rootNode.ownerDocument.evaluate( |
55 expression, | 54 expression, rootNode, cvox.XpathUtil.resolveNameSpace, |
56 rootNode, | 55 XPathResult.ORDERED_NODE_ITERATOR_TYPE, |
57 cvox.XpathUtil.resolveNameSpace, | 56 null); // no existing results |
58 XPathResult.ORDERED_NODE_ITERATOR_TYPE, | |
59 null); // no existing results | |
60 } catch (err) { | 57 } catch (err) { |
61 return []; | 58 return []; |
62 } | 59 } |
63 var results = []; | 60 var results = []; |
64 // Convert result to JS array | 61 // Convert result to JS array |
65 for (var xpathNode = xpathIterator.iterateNext(); | 62 for (var xpathNode = xpathIterator.iterateNext(); xpathNode; |
66 xpathNode; | |
67 xpathNode = xpathIterator.iterateNext()) { | 63 xpathNode = xpathIterator.iterateNext()) { |
68 results.push(xpathNode); | 64 results.push(xpathNode); |
69 } | 65 } |
70 return results; | 66 return results; |
71 }; | 67 }; |
72 | 68 |
73 /** | 69 /** |
74 * Given a rootNode, it returns an array of all its leaf nodes. | 70 * Given a rootNode, it returns an array of all its leaf nodes. |
75 * @param {Node} rootNode The node to get the leaf nodes from. | 71 * @param {Node} rootNode The node to get the leaf nodes from. |
76 * @return {Array} The array of leaf nodes for the given rootNode. | 72 * @return {Array} The array of leaf nodes for the given rootNode. |
77 */ | 73 */ |
78 cvox.XpathUtil.getLeafNodes = function(rootNode) { | 74 cvox.XpathUtil.getLeafNodes = function(rootNode) { |
79 try { | 75 try { |
80 var xpathIterator = rootNode.ownerDocument.evaluate( | 76 var xpathIterator = rootNode.ownerDocument.evaluate( |
81 './/*[count(*)=0]', | 77 './/*[count(*)=0]', rootNode, |
82 rootNode, | 78 null, // no namespace resolver |
83 null, // no namespace resolver | 79 XPathResult.ORDERED_NODE_ITERATOR_TYPE, |
84 XPathResult.ORDERED_NODE_ITERATOR_TYPE, | 80 null); // no existing results |
85 null); // no existing results | |
86 } catch (err) { | 81 } catch (err) { |
87 return []; | 82 return []; |
88 } | 83 } |
89 var results = []; | 84 var results = []; |
90 // Convert result to JS array | 85 // Convert result to JS array |
91 for (var xpathNode = xpathIterator.iterateNext(); | 86 for (var xpathNode = xpathIterator.iterateNext(); xpathNode; |
92 xpathNode; | |
93 xpathNode = xpathIterator.iterateNext()) { | 87 xpathNode = xpathIterator.iterateNext()) { |
94 results.push(xpathNode); | 88 results.push(xpathNode); |
95 } | 89 } |
96 return results; | 90 return results; |
97 }; | 91 }; |
98 | 92 |
99 /** | 93 /** |
100 * Returns whether or not xpath is supported. | 94 * Returns whether or not xpath is supported. |
101 * @return {boolean} True if xpath is supported. | 95 * @return {boolean} True if xpath is supported. |
102 */ | 96 */ |
103 cvox.XpathUtil.xpathSupported = function() { | 97 cvox.XpathUtil.xpathSupported = function() { |
104 if (typeof(XPathResult) == 'undefined') { | 98 if (typeof(XPathResult) == 'undefined') { |
105 return false; | 99 return false; |
106 } | 100 } |
107 return true; | 101 return true; |
108 }; | 102 }; |
109 | 103 |
110 | 104 |
111 /** | 105 /** |
112 * Given an XPath expression and rootNode, it evaluates the XPath expression as | 106 * Given an XPath expression and rootNode, it evaluates the XPath expression as |
113 * a boolean type and returns the result. | 107 * a boolean type and returns the result. |
114 * @param {string} expression The XPath expression to evaluate. | 108 * @param {string} expression The XPath expression to evaluate. |
115 * @param {Node} rootNode The HTML node to start evaluating the XPath from. | 109 * @param {Node} rootNode The HTML node to start evaluating the XPath from. |
116 * @return {boolean} The result of evaluating the xpath expression. | 110 * @return {boolean} The result of evaluating the xpath expression. |
117 */ | 111 */ |
118 cvox.XpathUtil.evaluateBoolean = function(expression, rootNode) { | 112 cvox.XpathUtil.evaluateBoolean = function(expression, rootNode) { |
119 try { | 113 try { |
120 var xpathResult = rootNode.ownerDocument.evaluate( | 114 var xpathResult = rootNode.ownerDocument.evaluate( |
121 expression, | 115 expression, rootNode, cvox.XpathUtil.resolveNameSpace, |
122 rootNode, | |
123 cvox.XpathUtil.resolveNameSpace, | |
124 XPathResult.BOOLEAN_TYPE, | 116 XPathResult.BOOLEAN_TYPE, |
125 null); // no existing results | 117 null); // no existing results |
126 } catch (err) { | 118 } catch (err) { |
127 return false; | 119 return false; |
128 } | 120 } |
129 return xpathResult.booleanValue; | 121 return xpathResult.booleanValue; |
130 }; | 122 }; |
131 | 123 |
132 | 124 |
133 /** | 125 /** |
134 * Given an XPath expression and rootNode, it evaluates the XPath expression as | 126 * Given an XPath expression and rootNode, it evaluates the XPath expression as |
135 * a string type and returns the result. | 127 * a string type and returns the result. |
136 * @param {string} expression The XPath expression to evaluate. | 128 * @param {string} expression The XPath expression to evaluate. |
137 * @param {Node} rootNode The HTML node to start evaluating the XPath from. | 129 * @param {Node} rootNode The HTML node to start evaluating the XPath from. |
138 * @return {string} The result of evaluating the Xpath expression. | 130 * @return {string} The result of evaluating the Xpath expression. |
139 */ | 131 */ |
140 cvox.XpathUtil.evaluateString = function(expression, rootNode) { | 132 cvox.XpathUtil.evaluateString = function(expression, rootNode) { |
141 try { | 133 try { |
142 var xpathResult = rootNode.ownerDocument.evaluate( | 134 var xpathResult = rootNode.ownerDocument.evaluate( |
143 expression, | 135 expression, rootNode, cvox.XpathUtil.resolveNameSpace, |
144 rootNode, | |
145 cvox.XpathUtil.resolveNameSpace, | |
146 XPathResult.STRING_TYPE, | 136 XPathResult.STRING_TYPE, |
147 null); // no existing results | 137 null); // no existing results |
148 } catch (err) { | 138 } catch (err) { |
149 return ''; | 139 return ''; |
150 } | 140 } |
151 return xpathResult.stringValue; | 141 return xpathResult.stringValue; |
152 }; | 142 }; |
OLD | NEW |