I have been looking for a CSS selector function other than Sizzle and I have come across this function.
function SparkEn(xpath,root) {
xpath = xpath
.replace(/((^|\|)\s*)([^/|\s]+)/g,'$2.//$3')
.replace(/\.([\w-]+)(?!([^\]]*]))/g, '[@class="$1" or @class$=" $1" or @class^="$1 " or @class~=" $1 "]')
.replace(/#([\w-]+)/g, '[@id="$1"]')
.replace(/\/\[/g,'/*[');
str = '(@\\w+|"[^"]*"|\'[^\']*\')';
xpath = xpath
.replace(new RegExp(str+'\\s*~=\\s*'+str,'g'), 'contains($1,$2)')
.replace(new RegExp(str+'\\s*\\^=\\s*'+str,'g'), 'starts-with($1,$2)')
.replace(new RegExp(str+'\\s*\\$=\\s*'+str,'g'), 'substring($1,string-length($1)-string-length($2)+1)=$2');
var got = document.evaluate(xpath, root||document, null, 5, null);
var result=[];
while (next = got.iterateNext())
result.push(next);
return result;
}
I just feel like it is too good to be true, is this a firefox only function (xpath?) or is it slow? Basically why would I use Sizzle over this?
I have been looking for a CSS selector function other than Sizzle and I have come across this function.
function SparkEn(xpath,root) {
xpath = xpath
.replace(/((^|\|)\s*)([^/|\s]+)/g,'$2.//$3')
.replace(/\.([\w-]+)(?!([^\]]*]))/g, '[@class="$1" or @class$=" $1" or @class^="$1 " or @class~=" $1 "]')
.replace(/#([\w-]+)/g, '[@id="$1"]')
.replace(/\/\[/g,'/*[');
str = '(@\\w+|"[^"]*"|\'[^\']*\')';
xpath = xpath
.replace(new RegExp(str+'\\s*~=\\s*'+str,'g'), 'contains($1,$2)')
.replace(new RegExp(str+'\\s*\\^=\\s*'+str,'g'), 'starts-with($1,$2)')
.replace(new RegExp(str+'\\s*\\$=\\s*'+str,'g'), 'substring($1,string-length($1)-string-length($2)+1)=$2');
var got = document.evaluate(xpath, root||document, null, 5, null);
var result=[];
while (next = got.iterateNext())
result.push(next);
return result;
}
I just feel like it is too good to be true, is this a firefox only function (xpath?) or is it slow? Basically why would I use Sizzle over this?
Share Improve this question edited Sep 9, 2015 at 13:03 sideshowbarker♦ 88.1k29 gold badges215 silver badges211 bronze badges asked Jan 13, 2011 at 15:47 OlicalOlical 41.4k12 gold badges57 silver badges77 bronze badges 2- I think it is firefox only, how disappointing. Apparently IE can do it on XML documents. – Olical Commented Jan 13, 2011 at 15:58
- o my god at last i found someone think same way as iam :))))))))))))) stackoverflow.com/questions/15310502/… – Marwan Commented Mar 10, 2013 at 11:37
3 Answers
Reset to default 10I believe no stable version of IE supports document.evaluate
, so you're limited to every other browser. It's not slow since it's a native implementation of XPath.
Sizzle is useful because it uses the native support browsers offer when available (such as document.getElementsByClassName), but falls back to doing it itself when unavailable (IE). It's also used by jQuery and Prototype, so it's heavily, heavily tested and is unlikely to give you any trouble. Sizzle is also heavily speed-tested and optimized (they have a whole speed test suite), which is more work you don't have to do.
I'd say go with jQuery, Prototype, or just Sizzle by itself unless you are doing something incredibly performance-sensitive (which, honestly, is probably an indicator that you've structured your application poorly).
I just have found http://sourceforge.net/projects/js-xpath/, which claims to be
an implementation of DOM Level 3 XPath for Internet Explorer 5+
See their implementation at http://nchc.dl.sourceforge.net/project/js-xpath/js-xpath/1.0.0/xpath.js
It is a DOM3 W3C Working Group Note: http://www.w3.org/TR/2004/NOTE-DOM-Level-3-XPath-20040226/xpath.html#XPathEvaluator-evaluate
Implementation status: https://developer.mozilla.org/en-US/docs/Web/API/document.evaluate#Browser_compatibility Today only not in IE 10 on latest stable desktop browsers.