-
-
Save os0x/3242 to your computer and use it in GitHub Desktop.
simple $X
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// simple version of $X | |
// $X(exp); | |
// $X(exp, context); | |
// @source https:/raw.github.com/gist/3242 | |
function $X (exp, context) { | |
context || (context = document); | |
var expr = (context.ownerDocument || context).createExpression(exp, function (prefix) { | |
return document.createNSResolver(context.documentElement || context).lookupNamespaceURI(prefix) || | |
context.namespaceURI || document.documentElement.namespaceURI || ""; | |
}); | |
var result = expr.evaluate(context, XPathResult.ANY_TYPE, null); | |
switch (result.resultType) { | |
case XPathResult.STRING_TYPE : return result.stringValue; | |
case XPathResult.NUMBER_TYPE : return result.numberValue; | |
case XPathResult.BOOLEAN_TYPE: return result.booleanValue; | |
case XPathResult.UNORDERED_NODE_ITERATOR_TYPE: | |
// not ensure the order. | |
var ret = [], i = null; | |
while (i = result.iterateNext()) ret.push(i); | |
return ret; | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment