On May 5, 2008, at 2:13 PM, Lachlan Hunt wrote:
*Solution 5*
Define the methods to behave as if an implicit :scope selector
and descendant combinator were prepended to each selector in
the group.
This would work by taking the selector and appending the equivalent
of ":scope " to the beginning of it, and then parsing as
usual. :scope could be implemented in any way the UA wanted here
since it's not actually used by the author.
While this looks promising on the surface, the real problem lies in
the implementation of the selector parsing. This requires signifant
changes to the grammar of selectors, and thus alteration of the
selector engines in browsers.
Consider the following selector:
">strong, >em"
The expectation would be for this to become:
":scope >strong, :scope >em"
The question is how to parse and convert it.
One possibility is to make the scoped API (whether that is
querySelector on elements or some new method) only accept a single
selector, not a group of selectors. This would remove any parsing
difficulties. Also, if commas are only ever used to separate the
selectors in a group in Selector syntax, then prepending ":scope " and
inserting " :scope " after every comma before parsing would work. I
believe this is currently true of Selectors, though it may change in
the future. I would guess the only likely place for commas to appear
would be in parameters to function-like pseudo-classes, in which case
the algorithm above but excluding commas nested inside parentheses
would be more future-proof. Detail of the algorithm:
1. Initialize nesting level to 0
2. Initialize the output string to the empty string
3. While characters in the input string remain:
3.a. read the current character
3.b. if the current character is:
",": if the nesting level is 0, append ", :scope " to the
output string
"(": increase the nesting level by 1, and append "(" to the
output string
")": decrease the nesting level by 1, and append ")" to the
output string
anything else: append the current character to the output string
3.c. advance to the next character
4. return the output string
Regards,
Maciej