CSS Conditional Group Rules Module Level 3

[LONGSTATUS] [DATE]

This version:
http://www.w3.org/TR/[YEAR]/[STATUS]-[SHORTNAME]-[CDATE]
Latest version:
http://www.w3.org/TR/css-conditional
Previous version:
http://lists.w3.org/Archives/Public/www-style/2011Apr/0428.html (@supports)
http://lists.w3.org/Archives/Public/www-style/2004Aug/0135.html (@document)
http://www.w3.org/TR/2011/REC-CSS2-2011MMDD/
Editors:
L. David Baron, Mozilla,

What should this specification be called? CSS Conditional Group(ing) Rules? CSS Group(ing) Rules? Something else?

Abstract

CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, in speech, etc. This module contains the features of CSS for conditional processing of parts of style sheets, conditioned on capabilities of the processor or the document the style sheet is being applied to. It includes and extends the functionality of CSS level 2 [[!CSS21]], which builds on CSS level 1 [[CSS1]]. The main extensions compared to level 2 are allowing nesting of certain at-rules inside @media, the addition of the @supports and @document rules for conditional processing.

Status of this document

The following features are at risk:

Table of contents

Introduction

Background

This section is not normative.

[[!CSS21]] defines one type of conditional group rule, the @media rule, and allows only rulesets (not other @-rules) inside of it. The @media rule provides the ability to have media-specific style sheets, which is also provided by style sheet linking features such as @import and <link>. The restrictions on the contents of @media rules made them less useful; they forced authors using CSS features involving @-rules in media-specific style sheets to use separate style sheets for each medium.

This specification extends the rules for the contents of conditional group rules to allow other @-rules, which enables authors to combine CSS features involving @-rules with media specific style sheets within a single style sheet.

This specification also defines additional types of conditional group rules, @supports and @document, to address author and user requirements.

The @supports rule allows CSS to be conditioned on implementation support for CSS properties and values. This rule makes it much easier for authors to use new CSS features and provide good fallback for implementations that do not support those features. This is particularly important for CSS features that provide new layout mechanisms, and for other cases where a set of related styles needs to be conditioned on property support.

The @document rule allows CSS to be condition on the page to which the style sheet is being applied. This allows users to apply styles to a particular page or group of pages, which greatly increases the power of user style sheets.

Module Interactions

This module replaces and extends the @media rule feature defined in [[!CSS21]] section 7.2.1 and incorporates the modifications previously made non-normatively by [[!MEDIAQ]] section 1.

Its current definition depends on @-rules defined in [[!CSS3FONT]] and [[!CSS3-ANIMATIONS]], but that dependency is only on the assumption that those modules will advance ahead of this one. If this module advances faster, then the dependency will be reversed.

Document Conventions

Conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in the normative parts of this document are to be interpreted as described in RFC 2119.

All of the text of this specification is normative except sections explicitly marked as non-normative, examples, and notes. [[!RFC2119]]

Examples in this specification are introduced with the words “for example” or are set apart from the normative text with class="example", like this:

This is an example of an informative example.

Informative notes begin with the word “Note” and are set apart from the normative text with class="note", like this:

Note, this is an informative note.

Processing of conditional group rules

This specification defines some CSS @-rules, called conditional group rules, that associate a condition with a group of other CSS rules. These different rules allow testing different types of conditions, but share common behavior for how their contents are used when the condition is true and when the condition is false.

For example, this rule:

@media print {
  #navigation { display: none }
}

causes a particular CSS rule (making elements with ID "navigation" be display:none) apply only when the style sheet is used for a print medium. Likewise, this CSS rule:

@document url("http://www.example.com/") {
  #example1 { display: none }
}

does the same type of conditional application, but using a different condition: whether the style sheet is being applied to the page http://www.example.com/.

Each conditional group rule has a condition, which at any time evaluates to true or false. When the condition is true, CSS processors MUST apply the rules inside the group rule as though they were at the group rule's location; when the condition is false, CSS processors MUST not apply any of rules inside the group rule. The current state of the condition does not affect the CSS object model, in which the contents of the group rule always remain within the group rule.

This means that when multiple conditional group rules are nested, a rule inside of both of them applies only when all of the rules' conditions are true.

For example, with this set of nested rules:
@media print { // rule (1)
  #navigation { display: none }
  @media (max-width: 12cm) { // rule (2)
    .note { float: none }
  }
}
the condition of the rule marked (1) is true for print media, and the condition of the rule marked (2) is true when the width of the display area (which for print media is the page box) is less than or equal to 12cm. Thus the rule #navigation { display: none } applies whenever this style sheet is applied to print media, and the rule .note { float: none } is applied only when the style sheet is applied to print media and the width of the page box is less than or equal to 12 centimeters.

When the condition for a conditional group rule changes, CSS processors MUST reflect that the rules now apply or no longer apply, except for properties whose definitions define effects of computed values that persist past the lifetime of that value (such as for some properties in [[CSS3-TRANSITIONS]] and [[!CSS3-ANIMATIONS]]).

Contents of conditional group rules

Conditional group rules are allowed to contain rulesets and any @-rules that are allowed at the top level of a style sheet before and after a ruleset. This means that @-rules that must occur at the beginning of the style sheet, such as @charset, @import, and @namespace are not allowed inside of conditional group rules. Conditional group rules can be nested.

In terms of the grammar, this specification extends this production in the Grammar of CSS 2.1:

media
  : MEDIA_SYM S* media_list LBRACE S* ruleset* '}' S*
  ;
into:
media
  : MEDIA_SYM S* media_list LBRACE S* nested_statement* '}' S*
  ;

nested_statement
  : ruleset | media | page | font-face-rule | keyframes-rule |
    supports-rule | document-rule
  ;
in which all the productions are defined in that grammar with the exception of font-face-rule not defined in [[!CSS3FONT]], keyframes-rule defined in [[!CSS3-ANIMATIONS]], and supports-rule and document-rule defined in this specification.

In general, future CSS specifications that add new @-rules that are not forbidden to occur after some other types of rules should modify this nested_statement production to keep the grammar accurate.

Style sheets MUST NOT use rules other than the allowed ones inside conditional group rules.

Error handling

Implementations MUST ignore rules that are not allowed within a group rule.

Define error handling rules for unknown things.

Media-specific style sheets: the @media rule

Put definition of @media rule here, building on [[!CSS21]] and [[!MEDIAQ]].

Feature queries: the @supports rule

Split this into introduction/abstract sections and the normative text here.

supports-rule
  : WRITE ME
  ;
I think having a capability like this in CSS is critical for the
adoption of new layout systems, and also potentially quite useful
for authors to help in the adoption of other new features for which
it is currently difficult to design a sensible fallback.

I'm particuarly interested in building something based on the
@supports proposal in the second message above.  But I think it
needs a few additions (some of which were mentioned in the messages
following that message).  In particular:

 * Support for negation is needed (i.e., for an "else" clause).

 * At the very least, support is needed for "or" between non-negated
   declaration blocks and either negation of the conjunction or
   support for "and" between negated ones.  Without this, queries
   for prefixed properties would be nearly impossible.

 * It needs to fit within the CSS @-rule syntax, which doesn't
   allow an "else" construct with two blocks within the
   forward-compatible syntax.  (Or, if the @else was its own
   at-rule, it doesn't make sense logically.)

I also wonder whether the idea that listing multiple declarations is
implicitly "and" is clear; I think it could be confused with or.


Thus I'm inclined towards something that allows:

@supports ( display: flex ) {
  ...
}

@supports ( -moz-box-shadow: 2px 2px 2px black ) or
          ( -webkit-box-shadow: 2px 2px 2px black ) or
          ( -o-box-shadow: 2px 2px 2px black ) {
  ...
}

@supports not ( display: flex ) {
  ...
}

I want to avoid allowing more than one of 'not', 'and', and 'or' at
the same nesting level (i.e., parentheses are required to prevent
things like "a and b or c" or "not a and b")

I do, however, want to allow authors to mix 'and', 'or', and 'not'
terms as needed.  I think the inability to do that in media queries
has been painful for authors.


So in other words, I'd propose the grammar for a new rule
('supports-rule' in the grammar below) which would be allowed
wherever @media rules are allowed:

supports-rule: '@supports' condition '{' S* ruleset* '}' S*
condition: negation | conjunction | disjunction | declaration-condition
negation: 'not' condition-in-parens
conjunction: condition-in-parens ( 'and' condition-in-parens )+
disjunction: condition-in-parens ( 'or' condition-in-parens )+
condition-in-parens: ( '(' condition ')' ) | declaration-condition
declaration-condition: '(' declaration ')'

The rules for evaluating the condition in each grammar term is:
  condition: evaluate the single child term
  negation: negate the result of evaluating the single child term
  conjunction: true if all of the (non-'and') child terms are true,
    otherwise false
  disjunction: true if any of the (non-'or') child terms are true,
    otherwise false
  condition-in-parens: evaluate the single (non-'(' and non-')')
    child term
  declaration-condition: true if the user agent supports the
    declaration, otherwise false
The user-agent MUST use the rulesets in an @supports if and only if
that @supports rule's condition evalutes to true.

Document queries: the @document rule

Given that this @-rule is intended primarily for user style sheets, what should this specification say about its use in author style sheets? Should it be forbidden? Should use instead be discouraged? Or should this specification remain neutral on the topic?

document-rule
  : WRITE ME
  ;
docrule ::= "@-moz-document" S+ url-list "{" S* ruleset* "}"

url-list ::= url-item ( "," S* url-item )*

url-item ::= ( "url(" | "url-prefix(" | "domain(" | "regexp(" ) URL ")" S*

where S is from the CSS2 tokenization, ruleset is from the CSS2 grammar,
and URL is like the production URI in the CSS2 tokenization except
without the 'url(' and ')'.
<url>

Write me

The url() function, since it is a core syntax element in CSS, is allowed (subject to different character limitations and thus escaping requirements) to contain an unquoted value (in addition to the string values that are allowed as arguments for all four functions).

url-prefix(<string>)

Write me

domain(<string>)

Write me

regexp(<string>)

Write me

Conformance

Conformance Classes

Conformance to the CSS Conditional Group Rules Module is defined for three conformance classes:

style sheet
A CSS style sheet.
renderer
A UA that interprets the semantics of a style sheet and renders documents that use them.
authoring tool
A UA that writes a style sheet.

A style sheet is conformant to CSS TEMPLATE Module if all of its declarations that use properties defined in this module have values that are valid according to the generic CSS grammar and the individual grammars of each property as given in this module.

A renderer is conformant to CSS TEMPLATE Module if, in addition to interpreting the style sheet as defined by the appropriate specifications, it supports all the properties defined by CSS TEMPLATE Module by parsing them correctly and rendering the document accordingly. However, the inability of a UA to correctly render a document due to limitations of the device does not make the UA non-conformant. (For example, a UA is not required to render color on a monochrome monitor.)

An authoring tool is conformant to CSS TEMPLATE Module if it writes syntactically correct style sheets, according to the generic CSS grammar and the individual grammars of each property in this module.

Partial Implementations

So that authors can exploit the forward-compatible parsing rules to assign fallback values, CSS renderers must treat as invalid (and ignore as appropriate) any at-rules, properties, property values, keywords, and other syntactic constructs for which they have no usable level of support. In particular, user agents must not selectively ignore unsupported component values and honor supported values in a single multi-value property declaration: if any value is considered invalid (as unsupported values must be), CSS requires that the entire declaration be ignored.

Experimental Implementations

To avoid clashes with future CSS features, the CSS specifications reserve a prefixed syntax for proprietary property and value extensions to CSS. The CSS Working Group recommends that experimental implementations of features in CSS Working Drafts also use vendor-prefixed property or value names. This avoids any incompatibilities with future changes in the draft. Once a specification reaches the Candidate Recommendation stage, implementors should implement the non-prefixed syntax for any feature they consider to be correctly implemented according to spec.

CR Exit Criteria

For this specification to be advanced to Proposed Recommendation, there must be at least two independent, interoperable implementations of each feature. Each feature may be implemented by a different set of products, there is no requirement that all features be implemented by a single product. For the purposes of this criterion, we define the following terms:

independent
each implementation must be developed by a different party and cannot share, reuse, or derive from code used by another qualifying implementation. Sections of code that have no bearing on the implementation of this specification are exempt from this requirement.
interoperable
passing the respective test case(s) in the official CSS test suite, or, if the implementation is not a Web browser, an equivalent test. Every relevant test in the test suite should have an equivalent test created if such a user agent (UA) is to be used to claim interoperability. In addition if such a UA is to be used to claim interoperability, then there must one or more additional UAs which can also pass those equivalent tests in the same way for the purpose of interoperability. The equivalent tests must be made publicly available for the purposes of peer review.
implementation
a user agent which:
  1. implements the specification.
  2. is available to the general public. The implementation may be a shipping product or other publicly available version (i.e., beta version, preview release, or “nightly build”). Non-shipping product releases must have implemented the feature(s) for a period of at least one month in order to demonstrate stability.
  3. is not experimental (i.e., a version specifically designed to pass the test suite and is not intended for normal usage going forward).

The specification will remain Candidate Recommendation for at least six months.

Acknowledgments

Thanks to the ideas and feedback from Tantek Çelik, Elika Etemad, Pascal Germroth, Björn Höhrmann, Chris Moschini, Ben Ward, Zack Weinberg, Boris Zbarsky, and all the rest of the www-style community.

References

Normative references

Other references

Index