Notes From A Selector Engine

As previously stated, I’ve recently been working on a selector engine. Unfortunately, as every browser behaves slightly differently, I’ve had to resort to constructing compatibility tables. Seeing as they can be useful in other applications, I thought I’d post it here:

Element.querySelectorAll

A browser-native selector engine that allows you to select element nodes based on a CSS selector (for example: div p a).

var a = document.getElementById('a');

a.querySelectorAll('div.post > p a[class]');
IE6 IE7 IE8 Firefox 3 Firefox 3.5 Opera 9 Opera 10 Safari 3 Safari 4 Chrome 1 Chrome 2
no no no no yes no yes no yes yes yes

Element.firstElementChild / Element.lastElementChild / Element.previousElementSibling / Element.nextElementSibling

Selects the first/last element child or the previous/next element sibling. Saves on extrenuous code like:

var previous = document.getElementById('example');
while((previous = previous.previousSibling) !== null && previous.nodeType !== 1) { }
var a = document.getElementById('a');

a.firstElementChild;
a.lastElementChild;
a.previousElementSibling;
a.nextElementSibling;
IE6 IE7 IE8 Firefox 3 Firefox 3.5 Opera 9 Opera 10 Safari 3 Safari 4 Chrome 1 Chrome 2
no no no no yes yes yes yes yes yes yes

Element.children

Similar to the childNodes method, but without text nodes and comments.

var a = document.getElementById('a');

a.children;
IE6 IE7 IE8 Firefox 3 Firefox 3.5 Opera 9 Opera 10 Safari 3 Safari 4 Chrome 1 Chrome 2
almost (includes comments) almost (includes comments) almost (includes comments) no yes yes yes yes yes yes yes

Element.compareDocumentPosition

Allows you to compare one node’s position to another. Returns a bitmask.

var a = document.getElementById('a');
var b = document.getElementById('b');

a.compareDocumentPosition(b);
IE6 IE7 IE8 Firefox 3 Firefox 3.5 Opera 9 Opera 10 Safari 3 Safari 4 Chrome 1 Chrome 2
no no no yes yes yes yes yes yes no yes

Element.contains

Returns a boolean whether or not an element is contained within another.

var a = document.getElementById('a');
var b = document.getElementById('b');

a.contains(b);
IE6 IE7 IE8 Firefox 3 Firefox 3.5 Opera 9 Opera 10 Safari 3 Safari 4 Chrome 1 Chrome 2
yes yes yes no no yes yes yes yes yes yes

Comments

No responses have been made so far. Add your comments.

    What do you think? Leave a comment...