top | item 10488059

(no title)

TabAtkins | 10 years ago

CSS selectors always select "down the tree". Or, read in reverse (from right to left), evaluating them only requires information from further up the tree. In HTML, "further up the tree" corresponds exactly to "earlier in the text stream". This means that as soon as an element finishes parsing, we can find all the selectors that apply to it immediately, enabling better incremental display.

Of course, that's no longer always true today. In particular, selectors like :nth-last-child() depend on information from later siblings. Defining a combinator or equivalent mechanism that allows direct selection based on later siblings is thus not out of the realm of possibility. And selecting a parent vs an earlier sibling is almost identical - the parent's start tag is immediately prior to the first sibling (or text), so the distance between "thing you're selecting" and "thing it needs to know about to evaluate the selector" is only incrementally larger. So a way of selecting a parent based on children is probably fine too.

There are a few issues that have prevented us from doing so today. For one, it's slow. Using a single one of the "backwards" sibling pseudo-classes can kick your entire page into slow-selector mode, because it breaks optimizations. The same would be true of parent selectors, probably. For two, we have to prevent nesting - siblings usually aren't too far from each other, but the root can be very far from a descendant. (Also, siblings are usually walkable by traversing an array, while parent/descendant relationships are typically pointer-based and slower to traverse.) This makes it a bit clumsy to specify.

That all said, it's on the roadmap. I plan for it show up in Selectors 5.

(Oh, and note, the "cascade" in "Cascading Style Sheets" isn't about what you're referring to. It's about a "cascade" of values for a given property colliding on a single element, and figuring out which one "wins". Thus the Cascade module <https://drafts.csswg.org/css-cascade/>. It's not the best name, but we're stuck with it. ^_^)

discuss

order

kagamine|10 years ago

Thanks for the comprehensive reply and correction.

As I noted in my comment the area in which a parent selector would be of use was in a framework where I don't always have access to the parent tag where I could add a class or ID to the html. The way around this, because we got it fixed finally, was not to wish for a css solution but was in fact to have the authors of the framework create a fix inside the framework itself; where the limitation actually occurred. It's of little significance to you perhaps, but it is a point worth noting that the solution lay in fixing the right thing and not adding ill-advised functionality to the wrong thing.

Also, thanks to you and your collegues for your work, especially flexbox, I like CSS a lot (I know it gets a lot of criticism) and I believe it is what it should be.