Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(501)

Unified Diff: chrome/browser/resources/access_chromevox/common/xpath_util.js

Issue 6254007: Adding ChromeVox as a component extensions (enabled only for ChromeOS, for no... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/access_chromevox/common/xpath_util.js
===================================================================
--- chrome/browser/resources/access_chromevox/common/xpath_util.js (revision 0)
+++ chrome/browser/resources/access_chromevox/common/xpath_util.js (revision 0)
@@ -0,0 +1,77 @@
+// Copyright (c) 2011 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() {
+ };
+
+
+/**
+ * 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,
+ 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;
+};
+
+/**
+ * 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;
+};
+
Property changes on: chrome/browser/resources/access_chromevox/common/xpath_util.js
___________________________________________________________________
Added: svn:executable
+ *
Added: svn:eol-style
+ LF

Powered by Google App Engine
This is Rietveld 408576698