jkd
12-05-2008, 09:09 PM
I must admit to not understanding the trend in using CSS to do DOM. Every major JS framework includes a selector engine, and browsers are implementing querySelector and querySelectorAll left and right. Now, I understand that utilizing the DOM Core API can be tedious, but using Array extras and either converting collections to arrays or modifying collections' prototypes can reduce the overhead in code-size.
So again I ask, why CSS selectors? XPath is far more powerful and capable of more finely selecting elements both upwards and downwards the tree, and there are JS-based XPath engines as well as browser-native methods (document.evaluate, for example). For the types of traversals CSS is capable of describing, the equivalent XPath is at least as simple. But even if one doesn't buy the at-least-as-simple argument, then why are frameworks using CSS instead of some simpler (and faster!) API? Using functional programming, it's easy to chain together function calls that can do the same as CSS selectors (and faster, since there is no compilation or parsing phase).
The fact that they don't must imply some inherent advantage to using CSS over simpler, faster methods, or about-as-simple, more powerful methods. I can think of two explanations:
1. Selector re-use; it is possible to directly reuse the selectors used to style to apply a behavior to the same elements.
2. Familiarity with CSS in the developer population.
However, I believe both explanations are insufficient; for #1 to be useful, one essentially needs to copy-and-paste selectors from CSS into the JS. But whenever copy-and-paste is used to maintain code-concurrency, inevitably the source diverges and bugs are introduced. It would be possible to scan through the list of CSS rules using the DOM until the desired selector is found, then use the text of the selector in the function call, but we still need to know what selector we are looking for, which doesn't solve the problem (at least, without abusing class attributes or rel attributes).
As for #2, I don't think that makes any sense. Once they have their desired list of DOM nodes, the developers will be attaching event listeners, reasoning out complicated DOM interactions, etc -- if they can't figure out how to query the DOM without CSS selectors, then they probably can't do what they want anyway. Furthermore, if they do know how to do what they want (or they've at least learned the robust API of whatever framework they are using), then they are capable of learning whatever simple query language you throw at them.
So again, I wonder the reasons for reusing the headaches of CSS to do DOM querying. Any thoughts?
So again I ask, why CSS selectors? XPath is far more powerful and capable of more finely selecting elements both upwards and downwards the tree, and there are JS-based XPath engines as well as browser-native methods (document.evaluate, for example). For the types of traversals CSS is capable of describing, the equivalent XPath is at least as simple. But even if one doesn't buy the at-least-as-simple argument, then why are frameworks using CSS instead of some simpler (and faster!) API? Using functional programming, it's easy to chain together function calls that can do the same as CSS selectors (and faster, since there is no compilation or parsing phase).
The fact that they don't must imply some inherent advantage to using CSS over simpler, faster methods, or about-as-simple, more powerful methods. I can think of two explanations:
1. Selector re-use; it is possible to directly reuse the selectors used to style to apply a behavior to the same elements.
2. Familiarity with CSS in the developer population.
However, I believe both explanations are insufficient; for #1 to be useful, one essentially needs to copy-and-paste selectors from CSS into the JS. But whenever copy-and-paste is used to maintain code-concurrency, inevitably the source diverges and bugs are introduced. It would be possible to scan through the list of CSS rules using the DOM until the desired selector is found, then use the text of the selector in the function call, but we still need to know what selector we are looking for, which doesn't solve the problem (at least, without abusing class attributes or rel attributes).
As for #2, I don't think that makes any sense. Once they have their desired list of DOM nodes, the developers will be attaching event listeners, reasoning out complicated DOM interactions, etc -- if they can't figure out how to query the DOM without CSS selectors, then they probably can't do what they want anyway. Furthermore, if they do know how to do what they want (or they've at least learned the robust API of whatever framework they are using), then they are capable of learning whatever simple query language you throw at them.
So again, I wonder the reasons for reusing the headaches of CSS to do DOM querying. Any thoughts?