📜 ⬆️ ⬇️

Selectors API - IE8b1 and Webkit

IE8b1 introduces support for a very interesting specification - the Selectors API . So far this is a W3C working draft, but I bet that IE and Webkit have already implemented the specification, Presto and Gecko will not be long in coming.
UPDATE : Firefox developers explained that in release 3.0 it is not necessary to expect support for the Selectors API, however in 3.5 it will most likely be implemented.
So what do we have? The specification contains two new IHTMLElement interface methods, querySelector() and querySelectorAll() , which take a string with any valid CSS selector as a parameter. querySelector returns a link to the first HTMLElement that matches the specified CSS selector; querySelectorAll returns a StaticNodeList with elements matching the specified CSS selector.
Thus, we have a new elegant and convenient way to search for documents in DOM.
It will be possible to forget about inventions like document.getElementsByClassName - you just need to execute document.querySelectorAll(".myClass") and get all the elements with this class.
Currently, these methods exist in almost all popular js frameworks , but in browsers that do not support the Selectors API, they work according to the same principle - they bypass the DOM and search for elements corresponding to a given CSS selector.
In order to compare the performance of libraries and native Selectors API support, Webkit authors created a testcase . The results of both Webkit and IE8b1 are impressive.
However, it must be remembered that the Selectors API is limited by the level of browser support for CSS selectors. IE8b1 does not support the CSS3 selector :last-child , and does not return document.querySelectorAll("body:last-child") anything.
Not without a small fly in the ointment - IE8b1 only partially supports the Selectors API specification, here is a quote from an MSDN article :
Because Internet Explorer 8 doesn’t formally support XHTML documents, it doesn’t support the W3C Selectors API specification, such as the NSResolver parameter.

But for sites where namespaces are not used, this spoon of tar will not spoil the barrel of honey.
Also an interesting fact is that the implementation of the Selectors API opens up the potential threat of stealing information about the visited pages in case of a successful introduction of javascript to the victim page, you can send hrefs of all document.querySelectorAll("a:visited") and thus find out which links to This page has already been visited. The specification leaves the resolution of this problem to the manufacturer implementing the Selectors API.
As a result, IE8b1 ignores the :visited pseudo-classes and :link as querySelector / querySelectorAll parameters.
Here is an example !

')

Source: https://habr.com/ru/post/25561/


All Articles