W3C home > Mailing lists > Public > www-style@w3.org > April 2013

Re: [css-selectors] Proposal: Logical Combinators / Sets

From: Simon Sapin <simon.sapin@exyr.org>
Date: Fri, 05 Apr 2013 16:48:46 +0200
Message-ID: <515EE44E.7070105@exyr.org>
To: Brian Kardell <bkardell@gmail.com>
CC: "www-style@w3.org" <www-style@w3.org>
Le 08/03/2013 02:39, Brian Kardell a écrit :
> A recent discussion about adding Set operations on another list prompted
> me to discuss something to Tab which I've mulled around (even
> prollyfilled) in the past and think it's worth proposing...
> CSS selectors operate on _sets_ of elements, but until  recently
> (:matches/:not) we've had nothing in the way of logical selectors or any
> language constructs that hinted at set oriented language.   This seems a
> shame to me as sets and logic gates are some of the most powerful
> concepts in CS.  The relationship feels natural to me.
> This proposal would require us to get complex selectors inside, but
> essentially I am proposing some logical combinators like
> :anyof, :allof, :oneof, :noneof to act as predicates which allow some
> very rich selection.  Examples/more details in my blog post:
> http://briankardell.wordpress.com/2013/03/07/logical-psuedo-selectors-a-proposal/

I think that Selectors can already express all logical operations. 
Juxtaposition of simple selectors is AND:


The comma is OR:

    h1, h2, h3

:not() is eponymous, and :matches() is a no-op that just provides grouping.

AND, OR, NOT and grouping are enough to express any boolean expression. 
(So is NAND by itself, but it’s not pretty ;))

As mentioned elsewhere in this thread, it helps to think of :not() and 
:matches() as taking not comma-separated arguments, but a single 
argument that is a selector list where the comma has its usual OR meaning.

Now, let’s get this out of the way: the :anyof() and :noneof() 
pseudo-classes proposed in the blog post are exactly the same as 
:matches() and :not(), respectively. We have a sub-thread on the naming 
so I won’t focus on that again.

That leaves :allof() and :oneof().

:allof() is AND, which is just juxtaposition. In case more than one of 
:allof()’s arguments contain a combinator, :matches() comes to the rescue:

     :allof(.foo, .bar) → .foo.bar
     :allof(.foo, ol li) → ol li.foo
     :allof(.foo .bar, ol li) → .foo .bar:matches(ol li)

Finally, :oneof() is XOR, which can be expressed as a combination of 
AND, OR, and NOT.

     :oneof(.foo, .bar) → .foo:not(.bar), .bar:not(.foo)

It gets messy with more XOR arguments, but it’s still doable.

So, only :oneof() is arguably useful, but is there really an use case 
for XOR in selectors to begin with?

Simon Sapin
Received on Friday, 5 April 2013 14:49:09 UTC

This archive was generated by hypermail 2.4.0 : Friday, 25 March 2022 10:08:28 UTC