Re: Supporting Scoped Selectors in Selectors API 2

On Mon, Sep 28, 2009 at 6:50 AM, Lachlan Hunt <lachlan.hunt@lachy.id.au> wrote:
> The :reference pseudo-class was previously discussed with the CSS WG, though
> it was known as :scope or :context at the time.  The original proposal and
> discussion for which can be found here.
>
> http://lists.w3.org/Archives/Public/www-style/2008Jul/0193.html
> http://lists.w3.org/Archives/Public/www-archive/2008Jul/att-0025/Overview.html

I still sorta like the name :scope, and it has nice symmetry with
<style scoped> in HTML5.

> Scoped selector strings are selectors that begin with either a combinator
> like '>', '+' or '~', or, because the descendant combinator can't work due
> to the need to strip whitespace, an exclamation point.

Very happy to see these in here.  Scoped selectors should have been a
part of querySelector from the beginning.

> Because the descendant combinator can't work in the same way, I've used the
> exclamation point character as an indicator that it is intended to be a
> scoped selector.
>
> e.g.
> elm.querySelector("!div div, div p");
>
> That is interpreted as being equivalent to:
>  ":reference div div, :reference div p".
>
> This character was chosen because it currently can't appear at the beginning
> of a conforming group of selectors.  But it also makes the assumption that
> future selector syntax will never allow a selector to begin with an
> exclamation point, or at least not in a way that would clash with its use
> here.

I'm not as happy with this.  I think it's probably safe to use !, but
it seems very inelegant.  Sizzle can avoid this because scoped
selectors are the rule there rather than the exception, but we're
unfortunately stuck with the current querySelector design.

Just requiring the use of :reference here would probably be fine.
It's a little verbose, but shrug.  Using a shorter word like :scope
would help out slightly here.

If not, then I'd like to make the ! a general requirement, rather than
a weird special case for this one bit.  That is, a scoped selector
string *must* start with a ! to trigger 'scoped mode'.  It's purely a
flag, and is ignored when actually parsing the string.  I don't like
this very much, but I like it better than saying "here's a special
form of one of the combinators that you have to use on the first
selector in a group, but can ignore everywhere else".

As it currently stands, people are going to forget to use it (since
they don't use it when leading with a > or whatever), and *most* of
the time it's going to give good results (it's not too often, I think,
that you'll have a selector match an element when looking at the
document, but not when looking just at the relevant subtree).  Every
once in a while, though, it'll match things it shouldn't, and authors
will have some difficulty understanding why (if they realize it at all
- it may be something relatively rare and only seen through occasional
confusing bug reports).

~TJ

Received on Monday, 28 September 2009 17:26:02 UTC