Beyond CSS 3 Selectors
By glazou on Tuesday 8 March 2011, 00:09 - CSS and style - Permalink
I want a change in Selectors, my baby, for post-CSS 3. Section 6.6.5.1 of the current provisional REC says:
The
:rootpseudo-class represents an element that is the root of the document. In HTML 4, this is always theHTMLelement.
This is clearly not enough and I have a use case:
- imagine you have two nested tables. You want to count the number of cells in the tbodies of the outermost table. That outermost table has no ID, no class, nothing to differenciate it from another table in the document. Let's call that outermost table
mytable. Thenmytable.querySelectorAll("tbody > tr > th, tbody > tr > td").lengthis not what we are looking for to count the cells because it will count ALL cells, including the cells of the innermost table...:rootdoes not help here because it does not represent the root of the current scope. The:not()functional pseudo does not help either. The count cannot be performed usingquerySelectororquerySelectorAlleven if it could be the easiest, the simplest way. (This is not an imaginary use case, I hit that issue in BlueGriffon...) - imagine you have a scoped stylesheet applying to a subtree with the root of the subtree being a totally arbitrary element. It has no ID, no class you can use, nothing. It's then NOT selectable in the scoped stylesheet at all, you cannot apply styles to that element through the scoped stylesheet...
So I want the following change for the next step of Selectors:
The
:rootpseudo-class represents an element that is the element root of the current scope. In the case of CSS, the current scope is determined by the following rules:
- if
:rootis used inside the parameter of a call toa.querySelectorora.querySelectorAllwhere a is a Document, then the current scope is the whole document- if
:rootis used inside the parameter of a call toa.querySelectorora.querySelectorAllwhere a is an Element, then the current scope is the subtree of the document a is the root of- if
:rootis used inside a non-scoped stylesheet, the current scope is the whole document- if
:rootis used inside a scoped stylesheet, the current scope is the scope of the stylesheet
That way, my count above is mytable.querySelectorAll(":root > tbody > tr > th, :root > tbody > tr > td").length and it is possible to style the root of a scope using the trivial selector :root. In the current CSS 3 case and non-scoped stylesheets, :root still means the root of the document. In the case of calls to Element.querySelector or Element.querySelectorAll, these calls reply an empty list anyway if the argument contains a :root pseudo and if the element is not the root of the document. In other terms, it's not something web authors use because it's totally useless at this time.

Comments
I think this was suggested back when Lachy was writing querySelector, and shot down for some reason (I'd have to dig up emails to find out exactly what the deal was).
This functionality is *super* useful, though. Just in case whatever objections were raised at the time are still valid, perhaps use :this instead?