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