| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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 A collection of JavaScript utilities used to simplify working |
| 7 * with xpaths. |
| 8 */ |
| 9 |
| 10 |
| 11 goog.provide('cvox.XpathUtil'); |
| 12 |
| 13 |
| 14 /** |
| 15 * Utilities for simplifying working with xpaths |
| 16 * @constructor |
| 17 */ |
| 18 cvox.XpathUtil = function() { |
| 19 }; |
| 20 |
| 21 |
| 22 /** |
| 23 * Given an XPath expression and rootNode, it returns an array of children nodes |
| 24 * that match. The code for this function was taken from Mihai Parparita's GMail |
| 25 * Macros Greasemonkey Script. |
| 26 * http://gmail-greasemonkey.googlecode.com/svn/trunk/scripts/gmail-new-macros.u
ser.js |
| 27 * @param {string} expression The XPath expression to evaluate. |
| 28 * @param {Node} rootNode The HTML node to start evaluating the XPath from. |
| 29 * @return {Array} The array of children nodes that match. |
| 30 */ |
| 31 cvox.XpathUtil.evalXPath = function(expression, rootNode) { |
| 32 try { |
| 33 var xpathIterator = rootNode.ownerDocument.evaluate( |
| 34 expression, |
| 35 rootNode, |
| 36 null, // no namespace resolver |
| 37 XPathResult.ORDERED_NODE_ITERATOR_TYPE, |
| 38 null); // no existing results |
| 39 } catch (err) { |
| 40 return []; |
| 41 } |
| 42 var results = []; |
| 43 // Convert result to JS array |
| 44 for (var xpathNode = xpathIterator.iterateNext(); |
| 45 xpathNode; |
| 46 xpathNode = xpathIterator.iterateNext()) { |
| 47 results.push(xpathNode); |
| 48 } |
| 49 return results; |
| 50 }; |
| 51 |
| 52 /** |
| 53 * Given a rootNode, it returns an array of all its leaf nodes. |
| 54 * @param {Node} rootNode The node to get the leaf nodes from. |
| 55 * @return {Array} The array of leaf nodes for the given rootNode. |
| 56 */ |
| 57 cvox.XpathUtil.getLeafNodes = function(rootNode) { |
| 58 try { |
| 59 var xpathIterator = rootNode.ownerDocument.evaluate( |
| 60 './/*[count(*)=0]', |
| 61 rootNode, |
| 62 null, // no namespace resolver |
| 63 XPathResult.ORDERED_NODE_ITERATOR_TYPE, |
| 64 null); // no existing results |
| 65 } catch (err) { |
| 66 return []; |
| 67 } |
| 68 var results = []; |
| 69 // Convert result to JS array |
| 70 for (var xpathNode = xpathIterator.iterateNext(); |
| 71 xpathNode; |
| 72 xpathNode = xpathIterator.iterateNext()) { |
| 73 results.push(xpathNode); |
| 74 } |
| 75 return results; |
| 76 }; |
| 77 |
| OLD | NEW |