Pseudo-Class implementation. #1409
-
Reading through the W3C Selectors Level 4 Draft here it states "They can also be dynamic, in the sense that an element can acquire or lose a pseudo-class while a user interacts with the document, without the document itself changing". My question is how, at a high level, would an implementation for this look? The first thing that comes to mind is a list of rules with pseudo-class selectors is maintained and on any appropriate event the DOM nodes are updated to see if they match any such rule. As an example: if the pseudo-class is ":hover" and I have a div that on hover changes background, we would check for a hover any time the user's mouse moves. This approach seems slow to me since you are repeating the process of matching rules to nodes over and over so my next thought is that pseudo-classes become attributes of their own for each node that they match. That is to say, when we parse a stylesheet and apply rules to a node, pseudo-class nodes aren't 'applied' but rather 'stored' and only applied later. This on the surface is more performant but also raises the question of how to undo an applied pseudo-class when it should no longer take affect (eg: user is no longer hovering over the div). I have played around with the idea of implementing some basic pseudo-classes (most notably onhover) so any suggestions are appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
On a related note: how would pseudo-element rules be applied? Taking the |
Beta Was this translation helpful? Give feedback.
-
One approach (which is what Chromium basically does) is to simply recompute style on every mouse move, based on the change of state for which element became hovered or not hovered. This sounds very slow (and it is very slow if implemented without optimizations), but can be highly optimized with the following techniques:
With these optimizations, the work needed is just recomputing style on two elements, plus a single hit test. The hit test may be the most expensive part of these. But in practice (depending on the site), the worst problem may be the issue of forcing rendering in order to be able to hit test, which may cause layout thrashing. (I notice now that layout thrashing is not mentioned in the book; I'll take a note to add it. |
Beta Was this translation helpful? Give feedback.
One approach (which is what Chromium basically does) is to simply recompute style on every mouse move, based on the change of state for which element became hovered or not hovered. This sounds very slow (and it is very slow if implemented without optimizations), but can be highly optimized with the following techniques:
:hover
With these optimization…