Selectors API support in IE8b1
As I mentioned in the previous post, IE8b1 introduced support for very powerful DOM accessing concept — Selectors API. It is still a W3C working draft, but I bet that as IE and Webkit already support it, Presto and Gecko will soon have it as well.
So what do we have? As per the spec, we have 2 methods:
.querySelectorAll() which can be applied to any HTMLElement and based on he parameter (CSS selectors string) provided return an Element or StaticNodeList populated with elements matching the provided CSS selectors. Bottom line, you give it CSS selector, they return you matching element(s).
It provides you with a new flexible way to select elements in DOM. We can do any weird and wonderful stuff we want with the power of JS combined with the flexibility of CSS selectors:
- Get all paragraphs with the
.noteclassname from one div? Not a problem –
- Get all elements with some classname? Forget about
document.getElementsByClassNameslow kludges — use
- Get a link with
.currentclassname from your UL-based menu?
So generally we don't have to iterate over huge StaticNodeLists anymore — it's done natively and very fast (much faster then by JS libraries). Please see the testcase prepared by Webkit authors to measure their Selectors API support — it works in IE8b1 except for CSS3 Selectors block (IE8b1 doesn't support CSS3
Bottom line, Selectors is a way to find elements in DOM. All browsers know how do it already when they parse CSS rules and find elements to which these rules have to be applied. So it's just an existing browser functionality exposed to the developer. And we have to keep in mind that if browser supports a CSS selector, it will allow you to query for this element using Selectors API. And obviously if there's no support for some CSS selector, you won't be able to get this element using Selectors.
For example, as IE8b1 doesn't support
:last-child CSS3 selector, you can't style such elements in CSS and you can't query them using Selectors.
Because Internet Explorer 8 does not formally support XHTML documents, it does not support the namespace features of the W3C Selectors API specification, such as the NSResolver parameter.
But for websites where namespaces are not used it's not gonna be of any problem.
Another interesting issue that Selectors API spec raises is a potential history theft.
Basically you can get all visited links
hrefs and send them by AJAX somewhere (just a matter of getting a StaticNodeList of elements by doing
Spec leaves it for the vendor to fix. So IE8b1 ignores the
:linkselectors when they appear in the selector query criteria.
Please see the Testcase