W3C

CSS Conditional Group Rules Module Level 3

Editor's Draft 4 June 2011

This version:
http://www.w3.org/TR/2011/ED-css-conditional-20110604
Latest version:
http://www.w3.org/TR/css-conditional
Previous version:
http://www.w3.org/TR/2011/REC-CSS2-2011MMDD/ (@media)
http://lists.w3.org/Archives/Public/www-style/2011Apr/0428.html (@supports)
http://lists.w3.org/Archives/Public/www-style/2004Aug/0135.html (@document)
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

This is a public copy of the editors' draft. It is provided for discussion only and may change at any moment. Its publication here does not imply endorsement of its contents by W3C. Don't cite this document other than as work in progress.

The (archived) public mailing list www-style@w3.org (see instructions) is preferred for discussion of this specification. When sending e-mail, please put the text “css-conditional” in the subject, preferably like this: “[css-conditional] …summary of comment…

This document was produced by the CSS Working Group (part of the Style Activity).

This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

The following features are at risk:

Table of contents

1. Introduction

1.1. 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.

1.2. 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.

1.3. 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.

2. 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]).

3. Contents of conditional group rules

The syntax of each conditional group rule consists of some syntax specific to the type of rule followed by a group rule body, which is a block (pair of braces) containing a sequence of rules.

A group rule body is 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 defines the following productions for use in the grammar of conditional group rules:

nested_statement
  : ruleset | media | page | font_face_rule | keyframes-rule |
    supports_rule | document_rule
  ;

group_rule_body
  : '{' S* nested_statement* '}' S*
  ;

in which all the productions are defined in that grammar with the exception of font_face_rule not defined in [CSS3FONT], keyframes-rule shouldn't have dash? defined in [CSS3-ANIMATIONS], and media, 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.

3.1. Error handling

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

Define error handling rules for unknown things.

4. Media-specific style sheets: the @media rule

The @media rule is a conditional group rule whose condition is a media query. It consists of the at-keyword @media followed by a (possibly empty) media query (as defined in [MEDIAQ]), followed by a group rule body. The condition of the rule is the result of the media query.

This @media rule:

@media print, (max-width: 600px) {
  #extra_navigation { display: none }
}

has the condition print, (max-width: 600px), which is true for print media and for devices whose width is at most 600px. When either of this is true, the condition of the rule is true, and the rule #extra_navigation { display: none } is applied.

In terms of the grammar, this specification extends the media production in the Grammar of CSS 2.1 ([CSS21], Appendix G) into:

media
  : MEDIA_SYM S+ media_query_list group_rule_body
  ;

where the group_rule_body production is defined in this specification, the media_query_list production is defined in [MEDIAQ], and the others are defined in the Grammar of CSS 2.1 ([CSS21], Appendix G).

This changes the S* in CSS 2.1 into S+. Is that correct?

5. Feature queries: the @supports rule

The @supports rule is a conditional group rule whose condition tests whether the user agent supports CSS property:value pairs. Authors can use it to write style sheets that use new features when available but degrade gracefully when those features are not supported. CSS has existing mechanisms for graceful degradation, such as ignoring unsupported properties or values, but these are not always sufficient when large groups of styles need to be tied to the support for certain features, as is the case for use of new layout system features.

The syntax of the condition in the @supports rule is slightly more complicated than for the other conditional group rules (though has some similarities to media queries) since:

Therefore, the syntax of the @supports rule allows testing for property:value pairs, and arbitrary conjunctions (and), disjunctions (or), and negations (not) of them.

This extends the lexical scanner in the Grammar of CSS 2.1 ([CSS21], Appendix G) by adding:

@{S}{U}{P}{P}{O}{R}{T}{S}	{return SUPPORTS_SYM;}

and the grammar by adding

supports_rule
  : SUPPORTS_SYM S+ supports_condition group_rule_body
  ;

supports_condition
  : supports_negation | supports_conjunction | supports_disjunction |
    supports_declaration_condition
  ;

supports_negation
  : 'not' S* supports_condition_in_parens
  ;

supports_conjunction
  : supports_condition_in_parens ( 'and' S* supports_condition_in_parens )+
  ;

supports_disjunction
  : supports_condition_in_parens ( 'or' S* supports_condition_in_parens )+
  ;

supports_condition_in_parens
  : ( '(' supports_condition ')' S* ) | supports_declaration_condition
  ;

supports_declaration_condition
  : '(' S* declaration ')' S*
  ;

The rules for evaluating the condition in each of the above grammar terms are:

supports_condition
evaluate the single child term
supports_negation
negate the result of evaluating the single child term
supports_conjunction
true if all of the (non-‘and’) child terms are true, otherwise false
supports_disjunction
true if any of the (non-‘or’) child terms are true, otherwise false
supports_condition_in_parens
evaluate the single (non-‘(’ and non-‘)’) child term
supports_declaration_condition
true if the user agent supports the declaration (which is the same as the rule for whether the user agent parses the declaration), otherwise false

and the condition of the @supports rule is the condition thus described for the supports_rule term.

Describe these examples.

@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 ) {
  ...
}

To avoid confusion between and and or, the syntax requires that both and and or be specified explicitly (rather than, say, using commas or spaces for one of them). Likewise, to avoid confusion caused by precedence rules, the syntax does not allow and, or, and not operators to be mixed without a layer of parentheses. Add examples.

6. Document queries: the @document rule

The @document rule is a conditional group rule whose condition depends on the URL of the document being styled. This allows style sheets, particularly user style sheets, to have styles that only apply to a set of pages rather than to all pages using the style sheet.

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?

The @document rule's condition is written as a comma-separated list of URL matching functions, and the condition evaluates to true whenever any one of those functions evaluates to true. The following URL matching functions are permitted:

<url>

The url() function is the exact url matching function. It evaluates to true whenever the URL of the document being styled is exactly the URL given.

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).

For example, this rule:

@document url("http://www.w3.org/Style/CSS/") {
  #summary { background: yellow; color: black}
}

styles the summary element on the page http://www.w3.org/Style/CSS/, but not on any other pages.

url-prefix(<string>)

The url-prefix() function is the url prefix matching function. It evaluates to true whenever the URL of the document being styled has the argument to the function as an initial substring (which is true when the two strings are equal). When the argument is the empty string, it evaluates to true for all documents.

For example, this rule:

@document url-prefix("http://www.w3.org/Style/CSS/") {
  #summary { background: yellow; color: black}
}

styles the summary element on the page http://www.w3.org/Style/CSS/ and on the page http://www.w3.org/Style/CSS/Test, but it does not affect the page http://www.w3.org/ or the page http://www.example.com/Style/CSS/.

domain(<string>)

The domain() function is the domain matching function. It evaluates to true whenever the URL of the page has a host subcomponent (as defined in [URI]) and that host subcomponent is exactly the argument to the domain() function or a final substring of the host component is a period (U+002E) immediately followed by the argument to the domain() function.

For example, this rule:

@document domain("w3.org") {
  body { font-size: 16px ! important }
}

changes the font size of the body element for pages such as http://www.w3.org/Style/CSS/ and http://w3.org/Style/CSS/ and http://lists.w3.org/Archives/Public/www-style/ but it does not affect the page http://www.example.com/Style/CSS/.

regexp(<string>)

Write me

This definition intentionally matches the behavior of the pattern attribute on the input element in [HTML5].

What form of normalization is done on URLs and domains before matching?

Need to define what URL we care about, perhaps in terms of browsing context. It should apply to the URL of an iframe/object/embed/img; it should probably not apply to the URL of an svg:use.

Implementations MUST treat any unknown URL matching functions as a syntax error, and thus ignore the @document rule. Should we instead have more complicated error handling rules?

This extends the lexical scanner in the Grammar of CSS 2.1 ([CSS21], Appendix G) by adding:

@{D}{O}{C}{U}{M}{E}{N}{T}	{return DOCUMENT_SYM;}

and the grammar by adding

document_rule
  : DOCUMENT_SYM S+ url_match_fn ( "," S* url_match_fn )* group_rule_body
  ;

url_match_fn
  : URI | FUNCTION
  ;

7. Conformance

7.1. 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.

7.2. 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.

7.3. 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.

7.4. 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.

Grammar

In order to allow these new @-rules in CSS style sheets, this specification modifies the stylesheet production in the Appendix G grammar of [CSS21] by replacing the media production defined in [CSS21] with the media production defined in this one, and additionally inserting | supports_rule | document_rule alongside ruleset | media | page.

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

[CSS21]
Bert Bos; et al. Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification. 23 April 2009. W3C Candidate Recommendation. (Work in progress.) URL: http://www.w3.org/TR/2009/CR-CSS2-20090423
[CSS3-ANIMATIONS]
Dean Jackson; David Hyatt; Chris Marrin. CSS Animations Module Level 3. 20 March 2009. W3C Working Draft. (Work in progress.) URL: http://www.w3.org/TR/2009/WD-css3-animations-20090320
[CSS3FONT]
John Daggett. CSS Fonts Module Level 3. 24 March 2011. W3C Working Draft. (Work in progress.) URL: http://www.w3.org/TR/2011/WD-css3-fonts-20110324
[MEDIAQ]
Håkon Wium Lie; et al. Media Queries. 27 July 2010. W3C Candidate Recommendation. (Work in progress.) URL: http://www.w3.org/TR/2010/CR-css3-mediaqueries-20100727/
[RFC2119]
S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. Internet RFC 2119. URL: http://www.ietf.org/rfc/rfc2119.txt
[URI]
T. Berners-Lee; R. Fielding; L. Masinter. Uniform Resource Identifiers (URI): generic syntax. January 2005. Internet RFC 3986. URL: http://www.ietf.org/rfc/rfc3986.txt

Other references

[CSS1]
Håkon Wium Lie; Bert Bos. Cascading Style Sheets (CSS1) Level 1 Specification. 11 April 2008. W3C Recommendation. URL: http://www.w3.org/TR/2008/REC-CSS1-20080411
[CSS3-TRANSITIONS]
Dean Jackson; et al. CSS Transitions Module Level 3. 1 December 2009. W3C Working Draft. (Work in progress.) URL: http://www.w3.org/TR/2009/WD-css3-transitions-20091201
[HTML5]
Ian Hickson. HTML5. 13 January 2011. W3C Working Draft. (Work in progress.) URL: http://www.w3.org/TR/2011/WD-html5-20110113

Index