神刀安全网

CSS When/Else Rules

Writing complex media queries or supports queries is difficult, particularly when you want to do Thing A in one circumstance, Thing B in another circumstance, and Thing C otherwise. This requires carefully crafting your conditional rules to exclude anything matching the other rules, while also making sure you’re not accidentally over-excluding things and leaving some situations unmatched.

This spec proposes two things to fix this problem. First, it proposes anrule, which generalizes the concept of a conditional rule. Anything you can express in an existing conditional rule can be expressed in, it just has to be wrapped in an appropriate function to declare what kind of condition it is. This allow authors to easily combine multiple types of queries, such as media queries and supports queries, in a single boolean expression. Without this, authors must rely on nesting separate conditional rules, which is harder to read and write, presupposes the conditions are to be conjoined with the "and" boolean relation (with no easy way to indicate anything else), and restricts their utility in the proposedconditional rule chains.

Second, it proposes the introduction ofrules, which follow conditional rules and automatically qualify their conditions as you’d expect, such that at most one rule in anconditional rule chainis chosen as active.

2. Generalized Conditional Rules: therule

The @when at-rule is a conditional group rule that generalizes the individual conditional group rules such as @media and @supports . It is defined as:

@when <boolean-condition> {   <stylesheet> }

Define "boolean algebra, with X as leaves" in a generic way in Conditional, so all the conditional rules can reference it directly, rather than having to redefine boolean algebra on their own.

Theandfunctions are defined as:

media() = media( [ <media-query-list> | <mf-plain> | <mf-boolean> | <mf-range> ] ) supports() = supports( [ <supports-condition> | <declaration> ] )

Aorfunction is associated the boolean result that its contained condition is associated with.

3. Chained Conditionals: therule

Usually, conditional group rules are independent; each one has a separate condition, evaluated without direct reference to any other rule, and decides whether or not to apply their contained rules based solely on their condition.

This is fine for simple conditions, but makes it difficult to write a collection of conditionals that are meant to be mutually exclusive; authors have to very carefully craft their conditions to not activate when the other rules are meant to, and make sure the collection of conditionals don’t accidentally all exclude some situation.

The @else rule is a conditional group rule used to formconditional rule chains, which allow multiple conditional rules to be provided and guarantee that at most one of them will evaluate their condition as true. It is defined as:

@else <boolean-condition>? {   <stylesheet> }

is interpreted identically to. If itsis omitted, it’s treated as having a condition that’s always true.

A conditional rule chain is a series of consecutive conditional group rules , starting with a conditional group rule other than, followed by zero or morerules. There cannot be anything between the successive conditional group rules other than whitespace and/or comments; any other token "breaks" the chain.

Should we require that only the lastin a chain can have an omitted condition? It’s not uncommon for me, when debugging code, to short-circuit an if-else chain by setting one of them to "true"; I presume that would be similarly useful in CSS? It’s still pretty easy to see you’ve done something wrong if you omit the condition accidentally.

Within aconditional rule chain, the conditions of each conditional group rule are evaluated in order. If one of them is true, the conditions of all following conditional group rules in the chain must evaluate to false, regardless of what they contain.

Anrule that is not part of aconditional rule chainis invalid and must be ignored.

For example, here’s a (somewhat silly) conditional chain:

@when media((width >= 400px) and (pointer: fine)) and supports(display: flex) {   /* A */ } @else supports((caret-color: pink) or (background: double-rainbow()) {   /* B */ } @else {   /* C */ }

Exactly one of the preceding rules will be chosen, even tho the second rule doesn’t exclude large widths, fine points, or flexbox support, and the last rule doesn’t specify anything at all.

To achieve the same result withoutconditional rule chains, you’d need to write:

@media (width >= 400px) and (pointer: fine) {   @supports (display: flex) {     /* A */   }   @supports not (display: flex) {     @supports (caret-color: pink) and (background: double-rainbow()) {       /* B */     }     @supports not ((caret-color: pink) and (background: double-rainbow())) {       /* C */     }   } } @media not ((width >= 400px) and (pointer: fine)) {   @supports (caret-color: pink) and (background: double-rainbow()) {     /* B */   }   @supports not ((caret-color: pink) and (background: double-rainbow())) {     /* C */   } }

This is simultaneously impossible to read, requires significant duplication of both conditions and contents, and is very difficult to write correctly (I wrote it wrong twice while producing this example). If the conditions got any more complicated (which is not unusual in real-world content), the example would get significantly worse.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » CSS When/Else Rules

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址