Permalink
Browse files

add xpath fn to jquery

  • Loading branch information...
1 parent eb49e3d commit 1a9c1a7e932c6cd21a8bf281ef27e8295b3d14cf @tilgovi committed Jun 24, 2012
Showing with 26 additions and 0 deletions.
  1. +1 −0 config.json
  2. +25 −0 src/xpath.js
View
@@ -49,6 +49,7 @@
, "src/touch-toolbar.js"
, "src/blur-indicator.js"
, "src/ui.js"
+ , "src/xpath.js"
, "src/main.js"
, "src/outro.js"
]
View
@@ -0,0 +1,25 @@
+(function(jQuery) {
+ "use strict";
+
+ var $ = jQuery;
+
+ $.fn.xpath = function(relativeRoot) {
+ var jq = this.map(function() {
+ var path = '';
+ var elem = this;
+
+ // elementNode nodeType == 1
+ while(elem && elem.nodeType == 1 && elem !== relativeRoot) {
+ var idx = $(elem.parentNode).children(elem.tagName).index(elem) + 1;
+
+ idx = "[" + idx + "]";
+ path = "/" + elem.tagName.toLowerCase() + idx + path;
+ elem = elem.parentNode;
+ }
+
+ return path;
+ });
+
+ return jq.get();
+ }
+})(jQuery);

0 comments on commit 1a9c1a7

Please sign in to comment.