Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit d373837df85523a93d2914b4c706c8e55de95809 @jfirebaugh committed Sep 13, 2010
Showing with 1,700 additions and 0 deletions.
  1. +32 −0 README.mdown
  2. +38 −0 jquery.xpath.js
  3. +30 −0 test/index.html
  4. +154 −0 test/jquery-1.4.2.min.js
  5. +155 −0 test/qunit.css
  6. +1,261 −0 test/qunit.js
  7. +30 −0 test/test.js
@@ -0,0 +1,32 @@
+# jQuery XPath
+
+jQuery XPath is a jQuery wrapper for the DOM 3 XPath API exposed by [`document.evaluate()`](https://developer.mozilla.org/en/DOM/document.evaluate).
+
+Instead of trying to remember what all the arguments to document.evaluate() are, and using some awkward iteration function, you can use `$.xquery('//path')` and get back a standard jQuery object. That uses the document as the node context; you can also use `.xquery()` on any jQuery object, which evaluates the XPath expression on each element.
+
+Consider the case of finding an input field labeled by "First Name":
+
+ <ul>
+ <li>
+ <label for="first_name">First Name</label>
+ <input id="first_name" type="text">
+ </li>
+ <li>
+ <label for="last_name">Last Name</label>
+ <input id="last_name" type="text">
+ </li>
+ </ul>
+
+jQuery:
+
+ $("input#" + $("label:contains('First Name')").attr("for"));
+
+jQuery XPath:
+
+ $.xpath("//input[@id=//label[contains(., 'First Name')]/@for]");
+
+Given that the jQuery version is shorter, why would you want to use XPath syntax instead? I don't know. I just wanted a simple API to test or debug XPath expressions. I use it to come up with XPaths for use in my capybara scenarios.
+
+# License
+
+Take your pick: MIT or GPL.
@@ -0,0 +1,38 @@
+/*
+ * XPath - jQuery wrapper for the DOM 3 XPath API exposed by document.evaluate()
+ *
+ * Copyright © 2010 John Firebaugh
+ *
+ * Dual licensed under the MIT or GPL licenses:
+ * http://www.opensource.org/licenses/mit-license.php
+ * http://www.gnu.org/licenses/gpl.html
+ *
+ */
+(function ($) {
+ xp = function (xpath, contextNode) {
+ var iterator = document.evaluate(xpath, contextNode, null, XPathResult.ANY_TYPE, null),
+ node = iterator.iterateNext(),
+ nodes = [];
+
+ while (node) {
+ nodes.push(node);
+ node = iterator.iterateNext();
+ }
+
+ return nodes;
+ };
+
+ $.xpath = function (xpath) {
+ return $(xp(xpath, document));
+ }
+
+ $.fn.xpath = function (xpath) {
+ var nodes = [];
+
+ this.each(function () {
+ nodes.push.apply(nodes, xp(xpath, this));
+ });
+
+ return this.pushStack(nodes, "xpath", xpath);
+ }
+})(jQuery);
@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>QUnit Test Suite</title>
+ <link rel="stylesheet" href="qunit.css" type="text/css" media="screen">
+ <script type="text/javascript" src="jquery-1.4.2.min.js"></script>
+ <script type="text/javascript" src="../jquery.xpath.js"></script>
+ <script type="text/javascript" src="qunit.js"></script>
+ <script type="text/javascript" src="test.js"></script>
+</head>
+<body>
+ <h1 id="qunit-header">QUnit Test Suite</h1>
+ <h2 id="qunit-banner"></h2>
+ <div id="qunit-testrunner-toolbar"></div>
+ <h2 id="qunit-userAgent"></h2>
+ <ol id="qunit-tests"></ol>
+ <div id="qunit-fixture">
+ <ul>
+ <li>
+ <label for="first_name">First Name</label>
+ <input id="first_name" type="text">
+ </li>
+ <li>
+ <label for="last_name">Last Name</label>
+ <input id="last_name" type="text">
+ </li>
+ </ul>
+ </div>
+</body>
+</html>

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,155 @@
+/** Font Family and Sizes */
+
+#qunit-tests, #qunit-header, #qunit-banner, #qunit-testrunner-toolbar, #qunit-userAgent, #qunit-testresult {
+ font-family: "Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial;
+}
+
+#qunit-testrunner-toolbar, #qunit-userAgent, #qunit-testresult, #qunit-tests li { font-size: small; }
+#qunit-tests { font-size: smaller; }
+
+
+/** Resets */
+
+#qunit-tests, #qunit-tests ol, #qunit-header, #qunit-banner, #qunit-userAgent, #qunit-testresult {
+ margin: 0;
+ padding: 0;
+}
+
+
+/** Header */
+
+#qunit-header {
+ padding: 0.5em 0 0.5em 1em;
+
+ color: #fff;
+ text-shadow: rgba(0, 0, 0, 0.5) 4px 4px 1px;
+ background-color: #0d3349;
+
+ border-radius: 15px 15px 0 0;
+ -moz-border-radius: 15px 15px 0 0;
+ -webkit-border-top-right-radius: 15px;
+ -webkit-border-top-left-radius: 15px;
+}
+
+#qunit-header a {
+ text-decoration: none;
+ color: white;
+}
+
+#qunit-banner {
+ height: 5px;
+}
+
+#qunit-testrunner-toolbar {
+ padding: 0em 0 0.5em 2em;
+}
+
+#qunit-userAgent {
+ padding: 0.5em 0 0.5em 2.5em;
+ background-color: #2b81af;
+ color: #fff;
+ text-shadow: rgba(0, 0, 0, 0.5) 2px 2px 1px;
+}
+
+
+/** Tests: Pass/Fail */
+
+#qunit-tests {
+ list-style-position: inside;
+}
+
+#qunit-tests li {
+ padding: 0.4em 0.5em 0.4em 2.5em;
+ border-bottom: 1px solid #fff;
+ list-style-position: inside;
+}
+
+#qunit-tests li strong {
+ cursor: pointer;
+}
+
+#qunit-tests ol {
+ margin-top: 0.5em;
+ padding: 0.5em;
+
+ background-color: #fff;
+
+ border-radius: 15px;
+ -moz-border-radius: 15px;
+ -webkit-border-radius: 15px;
+
+ box-shadow: inset 0px 2px 13px #999;
+ -moz-box-shadow: inset 0px 2px 13px #999;
+ -webkit-box-shadow: inset 0px 2px 13px #999;
+}
+
+/*** Test Counts */
+
+#qunit-tests b.counts { color: black; }
+#qunit-tests b.passed { color: #5E740B; }
+#qunit-tests b.failed { color: #710909; }
+
+#qunit-tests li li {
+ margin: 0.5em;
+ padding: 0.4em 0.5em 0.4em 0.5em;
+ background-color: #fff;
+ border-bottom: none;
+ list-style-position: inside;
+}
+
+/*** Passing Styles */
+
+#qunit-tests li li.pass {
+ color: #5E740B;
+ background-color: #fff;
+ border-left: 26px solid #C6E746;
+}
+
+#qunit-tests .pass { color: #528CE0; background-color: #D2E0E6; }
+#qunit-tests .pass .test-name { color: #366097; }
+
+#qunit-tests .pass .test-actual,
+#qunit-tests .pass .test-expected { color: #999999; }
+
+#qunit-banner.qunit-pass { background-color: #C6E746; }
+
+/*** Failing Styles */
+
+#qunit-tests li li.fail {
+ color: #710909;
+ background-color: #fff;
+ border-left: 26px solid #EE5757;
+}
+
+#qunit-tests .fail { color: #000000; background-color: #EE5757; }
+#qunit-tests .fail .test-name,
+#qunit-tests .fail .module-name { color: #000000; }
+
+#qunit-tests .fail .test-actual { color: #EE5757; }
+#qunit-tests .fail .test-expected { color: green; }
+
+#qunit-banner.qunit-fail,
+#qunit-testrunner-toolbar { background-color: #EE5757; }
+
+
+/** Footer */
+
+#qunit-testresult {
+ padding: 0.5em 0.5em 0.5em 2.5em;
+
+ color: #2b81af;
+ background-color: #D2E0E6;
+
+ border-radius: 0 0 15px 15px;
+ -moz-border-radius: 0 0 15px 15px;
+ -webkit-border-bottom-right-radius: 15px;
+ -webkit-border-bottom-left-radius: 15px;
+}
+
+/** Fixture */
+
+#qunit-fixture {
+ position: absolute;
+ top: -10000px;
+ left: -10000px;
+}
Oops, something went wrong.

0 comments on commit d373837

Please sign in to comment.