css3-conditional/Overview.src.html

Fri, 31 Aug 2012 13:49:19 -0700

author
Tab Atkins Jr. <jackalmage@gmail.com>
date
Fri, 31 Aug 2012 13:49:19 -0700
changeset 6625
66482bcc960b
parent 6622
44bf9c71914a
child 6655
8c75e6e6f34d
permissions
-rwxr-xr-x

[css3-conditional] Fix the one-arg CSS.supports() overload.

     1 <!DOCTYPE html public '-//W3C//DTD HTML 4.01//EN'
     2   'http://www.w3.org/TR/html4/strict.dtd'>
     3 <html lang="en">
     4 <head profile="http://www.w3.org/2006/03/hcard">
     5   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     6   <title>CSS Conditional Rules Module Level 3</title>
     7   <link rel="stylesheet" type="text/css" href="../default.css">
     8   <link rel="stylesheet" type="text/css"
     9         href="http://www.w3.org/StyleSheets/TR/W3C-[STATUS].css">
    10 </head>
    12 <div class="head">
    13 <!--logo-->
    15 <h1>CSS Conditional Rules Module Level 3</h1>
    17 <h2 class="no-num no-toc">[LONGSTATUS] [DATE]</h2>
    18 <dl>
    19   <dt>This version:
    20     <dd><a href="[VERSION]">
    21     http://www.w3.org/TR/[YEAR]/ED-css3-conditional-[CDATE]/</a>
    23   <dt>Latest version:
    24     <dd><a href="http://www.w3.org/TR/[SHORTNAME]/">http://www.w3.org/TR/[SHORTNAME]/</a>
    26   <dt>Editor's draft:
    27     <dd><a href="http://dev.w3.org/csswg/[SHORTNAME]/">http://dev.w3.org/csswg/[SHORTNAME]/</a>
    29   <dt>Previous version:
    30     <dd><a href="http://www.w3.org/TR/2011/WD-css3-conditional-20110901/">http://www.w3.org/TR/2011/WD-css3-conditional-20110901/</a></dd>
    32   <dt>Editors:
    33     <dd class=vcard><a class=fn href="http://dbaron.org/">L. David Baron</a>,
    34       <a class=org href="http://www.mozilla.org/">Mozilla</a>
    36   <dt>Issues list:
    37     <dd>Maintained in document (only editor's draft is current)
    39   <dt>Send feedback to:
    40     <dd>public mailing list <a href="www-style@w3.org">www-style@w3.org</a> (<a href="http://lists.w3.org/Archives/Public/www-style/">archive</a>, <a href="http://www.w3.org/Mail/Request">instructions</a>) with [[SHORTNAME]] at start of the subject
    42   <dt>Test suite:
    43     <dd><a href="https://test.csswg.org/shepherd/search/spec/css3-conditional/">submitted tests</a>; no built test suite yet
    45 </dl>
    47 <!--copyright-->
    49 <hr title="Separator for header">
    50 </div>
    52 <h2 class="no-num no-toc" id="abstract">Abstract</h2>
    54   <p>CSS is a language for describing the rendering of structured documents
    55   (such as HTML and XML) on screen, on paper,  in speech, etc. This module
    56   contains the features of CSS for conditional processing of parts of
    57   style sheets, conditioned on capabilities of the processor or the
    58   document the style sheet is being applied to.
    59   It includes and extends the functionality of CSS level&nbsp;2 [[!CSS21]],
    60   which builds on CSS level&nbsp;1 [[CSS1]].
    61   The main extensions compared to level&nbsp;2 are
    62   allowing nesting of certain at-rules inside '@media',
    63   the addition of the '@supports' and '@document'
    64   rules for conditional processing.
    66 <h2 class="no-num no-toc" id="status">Status of this document</h2>
    68 <!--status-->
    70 <p>The following features are at risk:
    71 <ul>
    72   <li>The inclusion of '@font-face' rules and
    73   '@keyframes' rules as allowed within all of the @-rules in
    74   this specification is at risk, though only because of the relative
    75   rates of advancement of specifications.  If this specification is able
    76   to advance faster than one or both of the specifications defining
    77   those rules, then the inclusion of those rules will move from this
    78   specification to the specification defining those rules.</li>
    80   <li>The addition of support for @-rules inside of conditional grouping
    81   rules is at risk; if interoperable implementations are not found, it
    82   may be removed to advance the other features in this specification to
    83   Proposed Recommendation.</li>
    85   <li>The '@supports' rule is at risk; if interoperable
    86   implementations are not found, it may be removed to advance the other
    87   features in this specification to Proposed Recommendation.</li>
    89   <li>The '@document' rule is at risk; if interoperable
    90   implementations are not found, it may be removed to advance the other
    91   features in this specification to Proposed Recommendation.</li>
    93 </ul>
    95 <h2 class="no-num no-toc" id="contents">Table of contents</h2>
    97 <!--toc-->
    99 <h2 id="introduction">Introduction</h2>
   101 <h3 id="context">Background</h3>
   103   <p><em>This section is not normative.</em>
   105   <p>[[!CSS21]] defines one type of conditional group rule, the
   106   '@media' rule, and allows only rulesets (not other @-rules)
   107   inside of it.  The '@media' rule provides the ability to
   108   have media-specific style sheets, which is also provided by style
   109   sheet linking features such as '@import' and
   110   <code class="html">&lt;link&gt;</code>.  The restrictions on the contents of
   111   '@media' rules made them less useful; they have forced authors
   112   using CSS features involving @-rules in media-specific style sheets to
   113   use separate style sheets for each medium.</p>
   115   <p>This specification extends the rules for the contents of
   116   conditional group rules to allow other @-rules, which enables authors
   117   to combine CSS features involving @-rules with media specific style
   118   sheets within a single style sheet.</p>
   120   <p>This specification also defines additional types of conditional
   121   group rules, '@supports' and '@document', to
   122   address author and user requirements.</p>
   124   <p>The '@supports' rule allows CSS to be conditioned on
   125   implementation support for CSS properties and values.  This rule makes
   126   it much easier for authors to use new CSS features and provide good
   127   fallback for implementations that do not support those features.  This
   128   is particularly important for CSS features that provide new layout
   129   mechanisms, and for other cases where a set of related styles needs to
   130   be conditioned on property support.</p>
   132   <p>The '@document' rule allows CSS to be conditioned on the
   133   page to which the style sheet is being applied.  This allows users to
   134   apply styles to a particular page or group of pages, which greatly
   135   increases the power of user style sheets.</p>
   137 <h3 id="placement">Module Interactions</h3>
   139   <p>This module replaces and extends the '@media' rule
   140   feature defined in [[!CSS21]] section <var>7.2.1</var> and
   141   incorporates the modifications previously made non-normatively by
   142   [[!MEDIAQ]] section <var>1</var>.</p>
   144   <p>Its current definition depends on @-rules defined in [[!CSS3-FONTS]]
   145   and [[!CSS3-ANIMATIONS]], but that dependency is only on the
   146   assumption that those modules will advance ahead of this one.  If this
   147   module advances faster, then the dependency will be reversed.</p>
   149 <h3 id="conventions">Document Conventions</h3>
   151   <p>Conformance requirements are expressed with a combination of
   152   descriptive assertions and RFC 2119 terminology. The key words “MUST”,
   153   “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”,
   154   “RECOMMENDED”, “MAY”, and “OPTIONAL” in the normative parts of this
   155   document are to be interpreted as described in RFC 2119.
   156   However, for readability, these words do not appear in all uppercase
   157   letters in this specification.
   159   <p>All of the text of this specification is normative except sections
   160   explicitly marked as non-normative, examples, and notes. [[!RFC2119]]</p>
   162   <p>Examples in this specification are introduced with the words “for example”
   163   or are set apart from the normative text with
   164   <code class="html">class="example"</code>, like this:
   166   <div class="example">
   167     <p>This is an example of an informative example.</p>
   168   </div>
   170   <p>Informative notes begin with the word “Note” and are set apart from the
   171   normative text with <code class="html">class="note"</code>, like this:
   173   <p class="note">Note, this is an informative note.</p>
   175 <h2 id="processing">Processing of conditional group rules</h2>
   177 <p>This specification defines some CSS @-rules, called <dfn>conditional
   178 group rules</dfn>, that associate a condition with a group of other
   179 CSS rules.  These different rules allow testing different types of
   180 conditions, but share common behavior for how their contents are used
   181 when the condition is true and when the condition is false.</p>
   183 <div class="example">
   184 <p>For example, this rule:</p>
   185 <pre>@media print {
   186   #navigation { display: none }
   187 }</pre>
   188 <p>causes a particular CSS rule (making elements with ID "navigation" be
   189 display:none) apply only when the style sheet is used for a print
   190 medium.  Likewise, this CSS rule:</p>
   191 <pre>@document url("http://www.example.com/") {
   192   #example1 { display: none }
   193 }</pre>
   194 <p>does the same type of conditional application, but using a different
   195 condition:  whether the style sheet is being applied to the page
   196 <code>http://www.example.com/</code>.</p>
   197 </div>
   199 <p>Each conditional group rule has a condition, which at any time
   200 evaluates to true or false.  When the condition is true, CSS processors
   201 <strong>must</strong> apply the rules inside the group rule as though
   202 they were at the group rule's location; when the condition is false, CSS
   203 processors <strong>must not</strong> apply any of rules inside the group
   204 rule.  The current state of the condition does not affect the CSS object
   205 model, in which the contents of the group rule always remain within the
   206 group rule.</p>
   208 <p>This means that when multiple conditional group rules are nested,
   209 a rule inside of both of them applies only when all of the rules'
   210 conditions are true.</p>
   212 <div class="example">For example, with this set of nested rules:
   213 <pre>@media print { // rule (1)
   214   #navigation { display: none }
   215   @media (max-width: 12cm) { // rule (2)
   216     .note { float: none }
   217   }
   218 }</pre>
   219 the condition of the rule marked (1) is true for print media, and the
   220 condition of the rule marked (2) is true when the width of the display
   221 area (which for print media is the page box) is less than or equal to
   222 12cm.  Thus the rule ''#navigation { display: none }'' applies
   223 whenever this style sheet is applied to print media, and the rule
   224 ''.note { float: none }'' is applied only when the style sheet
   225 is applied to print media <em>and</em> the width of the page box is less
   226 than or equal to 12 centimeters.</div>
   228 <p>When the condition for a conditional group rule changes, CSS
   229 processors <strong>must</strong> reflect that the rules now apply or no
   230 longer apply, except for properties whose definitions define effects of
   231 computed values that persist past the lifetime of that value (such as
   232 for some properties in [[CSS3-TRANSITIONS]] and
   233 [[!CSS3-ANIMATIONS]]).</p>
   235 <h2 id="contents-of">Contents of conditional group rules</h2>
   237 <p class="issue">There is also likely demand for using these
   238 conditions with '@import'.  We should see if we can
   239 come up with sensible syntax for that, perhaps functional notation
   240 at the end of the '@import' rule.</p>
   242 <p>The syntax of each conditional group rule consists of some syntax
   243 specific to the type of rule followed by a <dfn>group rule body</dfn>,
   244 which is a block (pair of braces) containing a sequence of rules.</p>
   246 <p>A group rule body is allowed to contain rulesets and any @-rules that
   247 are allowed at the top level of a style sheet before and after a
   248 ruleset.  This means that @-rules that must occur at the beginning of
   249 the style sheet (such as '@charset', '@import',
   250 and '@namespace' rules) are not allowed inside of conditional group
   251 rules.  Conditional group rules can be nested.</p>
   253 <p>In terms of the grammar, this specification defines the following
   254 productions for use in the grammar of conditional group rules:</p>
   256 <pre>nested_statement
   257   : ruleset | media | page | font_face_rule | keyframes_rule |
   258     supports_rule | document_rule
   259   ;
   261 group_rule_body
   262   : '{' S* nested_statement* '}' S*
   263   ;</pre>
   264 <p>
   265 in which all the productions are defined in that grammar with the
   266 exception of <code>font_face_rule</code> <span class="issue">not</span>
   267 defined in [[!CSS3-FONTS]], <code>keyframes_rule</code> defined in
   268 [[!CSS3-ANIMATIONS]], and <code>media</code>, <code>supports_rule</code>
   269 and <code>document_rule</code> defined in this specification.</p>
   271 <p>In general, future CSS specifications that add new @-rules that are
   272 not forbidden to occur after some other types of rules should modify
   273 this <code>nested_statement</code> production to keep the grammar
   274 accurate.</p>
   276 <p>Style sheets <strong>must not</strong> use rules other than the allowed ones inside
   277 conditional group rules.</p>
   279 <p>CSS processors <strong>must</strong> ignore rules that are not
   280 allowed within a group rule, and <strong>must</strong> handle invalid
   281 rules inside of group rules as described in <a
   282 href="http://www.w3.org/TR/CSS21/syndata.html#parsing-errors">section
   283 4.2 (Rules for handling parsing errors)</a>, <a
   284 href="http://www.w3.org/TR/CSS21/syndata.html#at-rules">section 4.1.5
   285 (At-rules)</a>, and <a
   286 href="http://www.w3.org/TR/CSS21/syndata.html#rule-sets">section 4.1.7
   287 (Rule sets, declaration blocks, and selectors)</a> of [[!CSS21]].</p>
   289 <h2 id="use">Placement of conditional group rules</h2>
   291 <p>Conditional group rules are allowed at the top-level of a style
   292 sheet, and inside other conditional group rules.  CSS processors
   293 <strong>must</strong> process such rules as <a
   294 href="#processing">described above</a>.</p>
   296 <p>Any rules that are not allowed after a ruleset (e.g., ''@charset'',
   297 ''@import'', or ''@namespace'' rules) are also not allowed after a
   298 conditional group rule.  Therefore, style sheets <strong>must
   299 not</strong> place such rules after a conditional group rules, and CSS
   300 processors <strong>must</strong> ignore such rules.</p>
   302 <h2 id="at-media">Media-specific style sheets:  the '@media' rule</h2>
   304 <p>The <dfn>'@media' rule</dfn> is a conditional group rule whose
   305 condition is a media query.  It consists of the at-keyword
   306 '@media' followed by a (possibly empty) media query (as
   307 defined in [[!MEDIAQ]]), followed by a group rule body.  The condition
   308 of the rule is the result of the media query.</p>
   310 <div class="example">
   311 <p>This '@media' rule:</p>
   312 <pre>@media print, (max-width: 600px) {
   313   #extra_navigation { display: none }
   314 }</pre>
   315 <p>has the condition ''print, (max-width: 600px)'', which is
   316 true for print media and for devices whose width is at most 600px.  When
   317 either of these is true, the condition of the rule is true, and the rule
   318 ''#extra_navigation { display: none }'' is applied.
   319 </div>
   321 <p>In terms of the grammar, this specification extends the
   322 <code>media</code> production in the
   323 <a href="http://www.w3.org/TR/CSS21/grammar.html">Grammar of CSS 2.1</a>
   324 ([[!CSS21]], Appendix G) into:
   325 <pre>media
   326   : MEDIA_SYM S* media_query_list group_rule_body
   327   ;</pre>
   328 <p>where the <code>group_rule_body</code> production is defined in this
   329 specification, the <code>media_query_list</code> production is defined
   330 in [[!MEDIAQ]], and the others are defined in the <a
   331 href="http://www.w3.org/TR/CSS21/grammar.html">Grammar of CSS 2.1</a>
   332 ([[!CSS21]], Appendix G).
   334 <h2 id="at-supports">Feature queries: the '@supports' rule</h2>
   336 <p>The <dfn>'@supports' rule</dfn> is a conditional group
   337 rule whose condition tests whether the user agent supports CSS
   338 property:value pairs.  Authors can use it to write style sheets that use
   339 new features when available but degrade gracefully when those features
   340 are not supported.  CSS has existing mechanisms for graceful
   341 degradation, such as ignoring unsupported properties or values, but
   342 these are not always sufficient when large groups of styles need to be
   343 tied to the support for certain features, as is the case for use of new
   344 layout system features.</p>
   346 <p>The syntax of the condition in the '@supports' rule is
   347 slightly more complicated than for the other conditional group rules
   348 (though has some similarities to media queries) since:</p>
   349 <ul>
   350   <li>negation is needed so that the new-feature styles and the fallback
   351   styles can be separated (within the forward-compatible grammar's rules
   352   for the syntax of @-rules), and not required to override each other</li>
   353   <li>conjunction (and) is needed so that multiple required features can
   354   be tested</li>
   355   <li>disjunction (or) is needed when there are multiple alternative
   356   features for a set of styles, particularly when some of those
   357   alternatives are vendor-prefixed properties or values</li>
   358 </ul>
   360 <p>Therefore, the syntax of the '@supports' rule allows
   361 testing for property:value pairs, and arbitrary conjunctions (and),
   362 disjunctions (or), and negations (not) of them.</p>
   364 <p>This extends the lexical scanner in the
   365 <a href="http://www.w3.org/TR/CSS21/grammar.html">Grammar of CSS 2.1</a>
   366 ([[!CSS21]], Appendix G) by adding:
   367 <pre>@{S}{U}{P}{P}{O}{R}{T}{S}	{return SUPPORTS_SYM;}</pre>
   368 <p>and the grammar by adding</p>
   369 <pre><dfn>supports_rule</dfn>
   370   : SUPPORTS_SYM S+ supports_condition group_rule_body
   371   ;
   373 <dfn>supports_condition</dfn>
   374   : supports_negation | supports_conjunction | supports_disjunction |
   375     supports_condition_in_parens
   376   ;
   378 <dfn>supports_condition_in_parens</dfn>
   379   : ( '(' S* supports_condition ')' S* ) | supports_declaration_condition
   380   ;
   382 <dfn>supports_negation</dfn>
   383   : 'not' S* supports_condition_in_parens
   384   ;
   386 <dfn>supports_conjunction</dfn>
   387   : supports_condition_in_parens ( 'and' S* supports_condition_in_parens )+
   388   ;
   390 <dfn>supports_disjunction</dfn>
   391   : supports_condition_in_parens ( 'or' S* supports_condition_in_parens )+
   392   ;
   394 <dfn>supports_declaration_condition</dfn>
   395   : '(' S* core_declaration ')' S*
   396   ;</pre>
   397 <p>in which <code>core_declaration</code> is the production
   398 <code>declaration</code> in the core syntax of CSS defined in <a
   399 href="http://www.w3.org/TR/CSS21/syndata.html#tokenization">section
   400 4.1.1 (Tokenization)</a> of [[!CSS21]].</p>
   402 <p>Any ''@supports'' rule that does not parse according to the grammar
   403 above is invalid.  Style sheets <strong>must not</strong> use such a
   404 rule and processors <strong>must</strong> ignore such a rule.</p>
   406 <p class="note">Note that this means that declarations that meet the
   407 forward-compatible syntax for declarations are permitted (and support
   408 for them is then tested by the ''@supports'' rule), but declarations
   409 that do not meet the forward-compatible syntax for declarations cause
   410 the entire ''@supports'' rule to be ignored.</p>
   412 <p class="issue">Is any further allowance for forward-compatible parsing
   413 needed, for example, to allow additional features (such as, say,
   414 selector tests) to be added to the ''@supports'' rule?  Or are these
   415 forward-compatible parsing rules the best solution for such future
   416 expansion anyway?</p>
   418 <p>Each of these grammar terms is associated with a boolean result, as
   419 follows:</p>
   420 <dl>
   421 <dt>supports_condition</dt>
   422 <dd>
   423   The result is the result of the single child term.
   424 </dd>
   426 <dt>supports_condition_in_parens</dt>
   427 <dd>
   428   The result is the result of the single <code>supports_condition</code>
   429   or <code>supports_declaration_condition</code> child term.
   430 </dd>
   432 <dt>supports_negation</dt>
   433 <dd>
   434   The result is the <em>negation</em> of the result of the
   435   <code>supports_condition_in_parens</code> child term.
   436 </dd>
   438 <dt>supports_conjunction</dt>
   439 <dd>
   440   The result is true if the result of <em>all</em> of the
   441   <code>supports_condition_in_parens</code> child terms is true;
   442   otherwise it is false.
   443 </dd>
   445 <dt>supports_disjunction</dt>
   446 <dd>
   447   The result is true if the result of <em>any</em> of the
   448   <code>supports_condition_in_parens</code> child terms is true;
   449   otherwise it is false.
   450 </dd>
   452 <dt>supports_declaration_condition</dt>
   453 <dd>
   454   The result is whether the CSS processor <a
   455   href="#support-definition">supports</a> the declaration.
   456 </dd>
   457 </dl>
   459 <p>The condition of the '@supports' rule is the result of the
   460 <code>supports_condition</code> term that is a child of the
   461 <code>supports_rule</code> term.</p>
   463 <div class="example">
   464 <p>For example, the following rule</p>
   465 <pre>@supports ( display: flexbox ) {
   466   body, #navigation, #content { display: flexbox; }
   467   #navigation { background: blue; color: white; }
   468   #article { background: white; color: black; }
   469 }</pre>
   470 <p>applies the rules inside the '@supports' rule only when
   471 ''display: flexbox'' is supported.</p>
   472 </div>
   474 <div class="example">
   475 <p>The following example shows an additional '@supports' rule that can
   476 be used to provide an alternative for when ''display: flexbox'' is not
   477 supported:</p>
   478 <pre>@supports not ( display: flexbox ) {
   479   body { width: 100%; height: 100%; background: white; color: black; }
   480   #navigation { width: 25%; }
   481   #article { width: 75%; }
   482 }</pre>
   483 <p>Note that the 'width' declarations may be harmful to the
   484 flexbox-based layout, so it is important that they be present only in
   485 the non-flexbox styles.</p>
   486 </div>
   488 <div class="example">
   489 <p>The following example checks for support for the 'box-shadow'
   490 property, including checking for support for vendor-prefixed versions of
   491 it.  When the support is present, it specifies both 'box-shadow' (with
   492 the prefixed versions) and 'color' in a way what would cause the text to
   493 become invisible were 'box-shadow' not supported.</p>
   494 <pre>@supports ( box-shadow: 2px 2px 2px black ) or
   495           ( -moz-box-shadow: 2px 2px 2px black ) or
   496           ( -webkit-box-shadow: 2px 2px 2px black ) or
   497           ( -o-box-shadow: 2px 2px 2px black ) {
   498   .outline {
   499     color: white;
   500     -moz-box-shadow: 2px 2px 2px black;
   501     -webkit-box-shadow: 2px 2px 2px black;
   502     -o-box-shadow: 2px 2px 2px black;
   503     box-shadow: 2px 2px 2px black; /* unprefixed last */
   504   }
   505 }</pre></div>
   507 <p>To avoid confusion between ''and'' and ''or'', the syntax requires
   508 that both ''and'' and ''or'' be specified explicitly (rather than, say,
   509 using commas or spaces for one of them).  Likewise, to avoid confusion
   510 caused by precedence rules, the syntax does not allow ''and'', ''or'',
   511 and ''not'' operators to be mixed without a layer of parentheses.</p>
   513 <div class="example">
   514 <p>For example, the following rule is not valid:
   515 <pre class="illegal">@supports (transition-property: color) or
   516           (animation-name: foo) and
   517           (transform: rotate(10deg)) {
   518   // ...
   519 }</pre>
   520 <p>Instead, authors must write one of the following:</p>
   521 <pre>@supports ((transition-property: color) or
   522            (animation-name: foo)) and
   523           (transform: rotate(10deg)) {
   524   // ...
   525 }</pre>
   526 <pre>@supports (transition-property: color) or
   527           ((animation-name: foo) and
   528            (transform: rotate(10deg))) {
   529   // ...
   530 }</pre>
   531 </div>
   533 <p>The declaration being tested must always occur within parentheses,
   534 when it is the only thing in the expression.<p>
   536 <div class="example">
   537 <p>For example, the following rule is not valid:
   538 <pre class="illegal">@supports display: flexbox {
   539   // ...
   540 }</pre>
   541 <p>Instead, authors must write:</p>
   542 <pre>@supports (display: flexbox) {
   543   // ...
   544 }</pre>
   545 </div>
   547 <p>The syntax allows extra parentheses when they are not needed.  This
   548 flexibility is sometimes useful for authors (for example, when
   549 commenting out parts of an expression) and may also be useful for
   550 authoring tools.</p>
   552 <div class="example">
   553 <p>For example, authors may write:</p>
   554 <pre>@supports ((display: flexbox)) {
   555   // ...
   556 }</pre>
   557 </div>
   559 <p class="issue">Add an example involving <code>!important</code>.</p>
   561 <h3 id="support-definition">Definition of support</h3>
   563 <p>A CSS processor is considered to <dfn id="dfn-support">support</dfn>
   564 a declaration (consisting of a property and value) if it implements the
   565 given value of the given property.</p>
   567 <h3 id="partial-implementations">Partial implementations</h3>
   569 <p>For forward-compatibility, <a
   570 href="http://www.w3.org/TR/CSS21/syndata.html#declaration">section 4.1.8
   571 (Declarations and properties)</a> of [[!CSS21]] defines rules for
   572 handling invalid properties and values.  CSS processors that partially
   573 implement a specification <strong>must</strong> treat any part of a
   574 value that they:</p>
   575 <ul>
   576   <li>do not implement, or</li>
   577   <li>do not have a usable level of support for,</li>
   578 </ul>
   579 <p>as invalid according to this rule
   580 for handling invalid properties and values,
   581 and <strong>must not</strong>
   582 accept or support a declaration containing such a value.  This allows
   583 authors to use fallback (either in the [[CSS1]] sense of declarations
   584 that are overridden by later declarations or with the new capabilities
   585 provided by the ''@supports'' rule in this specification) that works
   586 correctly for the features implemented.  This applies especially to
   587 compound values; implementations must implement all parts of the value
   588 in order to consider the declaration supported, either inside a ruleset
   589 or in the declaration condition of an ''@supports'' rule.</p>
   591 <h2 id="at-document">Document queries: the '@document' rule</h2>
   593 <p>The <dfn>'@document' rule</dfn> is a conditional group
   594 rule whose condition depends on the
   595 <a href="#url-of-doc">URL of the document being styled</a>.
   596 This allows style sheets, particularly user style sheets, to have styles
   597 that only apply to a set of pages rather than to all pages using the
   598 style sheet.</p>
   600 <p class="issue">Given that this @-rule is intended primarily for user
   601 style sheets, what should this specification say about its use in author
   602 style sheets?  Should it be forbidden?  Should use instead be
   603 discouraged?  Or should this specification remain neutral on the
   604 topic, since there are valid uses in author style sheets?</p>
   606 <p id="url-of-doc">The <dfn>URL of the document being styled</dfn> is
   607 the URI at which the document is located, excluding any fragment
   608 identifiers.  (This means, for example, that HTTP redirects have been
   609 followed.)  If the styles are being applied inside a complete document
   610 embedded into the presentation of another (e.g., [[HTML5]]&#39;s <code
   611 class="html">iframe</code>, <code class="html">object</code>, or <code
   612 class="html">img</code> elements), the relevant URI is that of the
   613 frame, not of its container.  However, if content from other documents
   614 is mixed in via mechanisms that mix content from one document into
   615 another (e.g., [[SVG11]]&#39;s <code>use</code> element), then the
   616 address of the container document is used.</p>
   618 <p class="note">Note:  In [[HTML5]], this is the
   619 <a href="http://dev.w3.org/html5/spec/dom.html#the-document-s-address">document's address</a>
   620 of a document in a
   621 <a href="http://dev.w3.org/html5/spec/browsers.html#browsing-context">browsing context</a>.</p>
   623 <div class="issue">What form of normalization is done on URLs and domains
   624 before matching?  In particular, this specification needs to describe:
   625 <ul>
   626 <li>what form is used for the <a href="#url-of-doc">URL of the document
   627 being styled</a> (and what has been normalized in that form)</li>
   628 <li>what normalization (if any) happens to the argument of each of the match
   629 functions before the comparison that they describe and</li>
   630 <li>whether the
   631 comparison algorithm used is string comparison or some other URL
   632 comparison algorithm.</li></ul></div>
   634 <p>The '@document' rule's condition is written as a
   635 comma-separated list of <dfn>URL matching functions</dfn>, and the
   636 condition evaluates to true whenever any one of those functions
   637 evaluates to true.  The following URL matching functions are
   638 permitted:</p>
   640 <dl>
   641   <dt><dfn id="url-exact" title="url()|URL matching functions::exact">&lt;url&gt;</dfn></dt>
   643   <dd>
   644     <p>The 'url()' function is the <dfn>exact url matching
   645     function</dfn>.  It evaluates to true whenever the <a
   646     href="#url-of-doc">URL of the document being styled</a> is exactly
   647     the URL given.</p>
   649     <p class="Note">The 'url()' function, since it is a core syntax
   650     element in CSS, is allowed (subject to different character
   651     limitations and thus escaping requirements) to contain an unquoted
   652     value (in addition to the string values that are allowed as
   653     arguments for all four functions).</p>
   655     <div class="example">
   656       <p>For example, this rule:</p>
   657 <pre>@document url("http://www.w3.org/Style/CSS/") {
   658   #summary { background: yellow; color: black}
   659 }</pre>
   660       <p>styles the <code class="html">summary</code> element on the page
   661       <code>http://www.w3.org/Style/CSS/</code>, but not on any other
   662       pages.</p>
   663     </div>
   664   </dd>
   666   <dt><dfn id="url-prefix" title="url-prefix()|URL matching functions::prefix">url-prefix(&lt;string&gt;)</dfn></dt>
   668   <dd>
   669     <p>The 'url-prefix()' function is the <dfn>url prefix
   670     matching function</dfn>.  It evaluates to true whenever the
   671     <a href="#url-of-doc">URL of the document being styled</a>
   672     has the argument to the function as an
   673     initial substring (which is true when the two strings are equal).
   674     When the argument is the empty string, it evaluates to true for all
   675     documents.</p>
   676     <div class="example">
   677       <p>For example, this rule:</p>
   678 <pre>@document url-prefix("http://www.w3.org/Style/CSS/") {
   679   #summary { background: yellow; color: black}
   680 }</pre>
   681       <p>styles the <code class="html">summary</code> element on the page
   682       <code>http://www.w3.org/Style/CSS/</code> and on the page
   683       <code>http://www.w3.org/Style/CSS/Test</code>, but it does not
   684       affect the page <code>http://www.w3.org/</code> or the page
   685       <code>http://www.example.com/Style/CSS/</code>.</p>
   686     </div>
   687   </dd>
   689   <dt><dfn id="url-domain" title="domain()|URL matching functions::domain">domain(&lt;string&gt;)</dfn></dt>
   691   <dd>
   692     <p>The 'domain()' function is the <dfn>domain
   693     matching function</dfn>.  It evaluates to true whenever
   694     the <a href="#url-of-doc">URL of the document being styled</a>
   695     has a host subcomponent (as defined in [[!URI]])
   696     and that host subcomponent is exactly the argument to the
   697     'domain()' function or a final substring of the host
   698     component is a period (U+002E) immediately followed by the argument
   699     to the 'domain()' function.</p>
   700     <div class="example">
   701       <p>For example, this rule:</p>
   702 <pre>@document domain("w3.org") {
   703   body { font-size: 16px ! important }
   704 }</pre>
   705       <p>changes the font size of the body element for pages such as
   706       <code>http://www.w3.org/Style/CSS/</code> and
   707       <code>http://w3.org/Style/CSS/</code> and
   708       <code>http://lists.w3.org/Archives/Public/www-style/</code>
   709       but it does not affect the page
   710       <code>http://www.example.com/Style/CSS/</code>.</p>
   711     </div>
   712   </dd>
   714   <dt><dfn id="url-regexp" title="regexp()|URL matching functions::regular expression">regexp(&lt;string&gt;)</dfn></dt>
   716   <dd>
   717     <p>The contents of the &lt;string&gt; argument <strong>must</strong>
   718     match the JavaScript <code>Pattern</code> production
   719     ([[!ECMA-262-5.1]], section 15.10.1).  However,
   720     failing to do so is not a CSS syntax error and does not trigger any
   721     error handling for CSS syntax errors.</p>
   723     <p>The ''regexp()'' function evaluates to true whenever the string
   724     argument compiled as a JavaScript regular expression with the
   725     <code>global</code>, <code>ignoreCase</code> and
   726     <code>multiline</code> flags <em>disabled</em>
   727     (see [[!ECMA-262-5.1]], sections 15.10.7.2 through 15.10.7.4)
   728     compiles successfully and the resulting regular expression matches
   729     the entirety of the
   730     <a href="#url-of-doc">URL of the document being styled</a>.</p>
   732     <p class="note">Note that regular expression must match the entire
   733     URL, not just a part of it.</p>
   735     <p class="note">Note that this definition intentionally matches the
   736     behavior of the <a
   737     href="http://dev.w3.org/html5/spec/common-input-element-attributes.html#attr-input-pattern"><code class="html">pattern</code>
   738     attribute</a> on the <code class="html">input</code> element
   739     in [[HTML5]].</p>
   741     <div class="example">
   742       <p>For example, this rule:</p>
   743 <pre>@document regexp("http://www.w3.org/TR/\\d{4}/[^/]*-CSS2-\\d{8}/") {
   744   body { font-size: 20px ! important }
   745 }</pre>
   746       <p>changes the font size of the body element for pages such as
   747       <code>http://www.w3.org/TR/2011/PR-CSS2-20110412/</code>.</p>
   748       <p class="note">Note that the backslashes in the regular
   749       expression require CSS escaping as ''\\''.</p>
   750     </div>
   751   </dd>
   753 </dl>
   755 <p>Implementations <strong>must</strong> treat any unknown URL matching
   756 functions as a syntax error, and thus ignore the '@document' rule.
   757 <span class="issue">Should we instead have more complicated error
   758 handling rules to make forward-compatibility work differently, or is
   759 this rule the best solution for such future expansion anyway?</span></p>
   761 <div class="issue">This syntax doesn't offer any ability to do negations,
   762 which has been requested in <a
   763 href="https://bugzilla.mozilla.org/show_bug.cgi?id=349813">Mozilla bug
   764 349813</a>.  Use cases that people have wanted negations for
   765 include:
   766 <ul>
   767   <li>User style sheets that want a particular rule in general, but know
   768   that that rule does more harm than good on specific sites.</li>
   769   <li>Authors who have a rule that they want to apply to most of their
   770   pages, but wish to make a few exceptions for.</li>
   771 </ul>
   772 </div>
   774 <p>This extends the lexical scanner in the
   775 <a href="http://www.w3.org/TR/CSS21/grammar.html">Grammar of CSS 2.1</a>
   776 ([[!CSS21]], Appendix G) by adding:
   777 <pre>@{D}{O}{C}{U}{M}{E}{N}{T}	{return DOCUMENT_SYM;}</pre>
   778 <p>and the grammar by adding</p>
   779 <pre>document_rule
   780   : DOCUMENT_SYM S+ url_match_fn ( "," S* url_match_fn )* group_rule_body
   781   ;
   783 url_match_fn
   784   : (URI | FUNCTION) S*
   785   ;</pre>
   787 <h2 id="apis">APIs</h2>
   789 <h3 id='extentions-to-cssrule-interface'>
   790 Extensions to the <code>CSSRule</code> interface</h3>
   792 <p>The <code>CSSRule</code> interface is extended as follows:
   794 <pre class='idl'>partial interface CSSRule {
   795     const unsigned short SUPPORTS_RULE = 12;
   796     const unsigned short DOCUMENT_RULE = 13;
   797 }</pre>
   800 <h3 id="the-cssconditionrule-interface">
   801 The <code>CSSConditionRule</code> interface</h3>
   803 <p>The <dfn><code>CSSConditionRule</code></dfn> interface represents all the "conditional" at-rules,
   804   which consist of a condition and a statement block.
   806 <pre class='idl'>interface CSSConditionRule : CSSRule {
   807     attribute DOMString conditionText;
   808     readonly attribute CSSRuleList cssRules;
   809     unsigned long insertRule (DOMString rule, unsigned long index);
   810     void deleteRule (unsigned long index);
   811 }</pre>
   813 <dl class='idl-attributes'>
   814   <dt><code>conditionText</code> of type <code>DOMString</code>
   815   <dd>The <code>conditionText</code> attribute, on getting, must return
   816     the result of serializing the associated condition.
   818     <p>On setting the <code>conditionText</code> attribute these steps
   819       must be run:
   821     <ol>
   822       <li>Trim the given value of white space.
   823       <li>If the given value matches the grammar of the 
   824         appropriate condition production for the given rule, 
   825         replace the associated CSS condition with the given value.
   826       <li>Otherwise, do nothing.
   827     </ol>
   829   <dt><code>cssRules</code> of type <code>CSSRuleList</code>, readonly
   830   <dd>The <code>cssRules</code> attribute must return a <code>CSSRuleList</code>
   831     object for the list of CSS rules nested inside the condition rule.
   832 </dl>
   834 <dl class='idl-methods'>
   835   <dt><code>insertRule(DOMString rule, unsigned long index)</code>, returns
   836     <code>unsigned long</code>
   837   <dd>The <code>insertRule</code> operation must insert a CSS rule <var>rule</var>
   838     into the CSS rule list returned by <code>cssRules</code> at <var>index</var>.
   840   <dt><code>deleteRule (unsigned long index)</code>, return <code>void</code>
   841   <dd>The <code>deleteRule</code> operation must remove a CSS rule from the 
   842     CSS rule list returned by <code>cssRules</code> at <var>index</var>.
   843 </dl>
   846 <h3 id="the-cssmediarule-interface">
   847 The <code>CSSMediaRule</code> interface</h3>
   849 <p>The <dfn><code>CSSMediaRule</code></dfn> interface represents a ''@media'' rule:
   851 <pre class='idl'>interface CSSMediaRule : CSSConditionRule {
   852     readonly attribute MediaList media;
   853 }</pre>
   855 <dl class='idl-attributes'>
   856   <dt><code>media</code> of type <code>MediaList</code>, readonly
   857   <dd>The <code>media</code> attribute must return a <code>MediaList</code> object
   858     for the list of media queries specified with the ''@media'' rule.
   860   <dt><code>conditionText</code> of type <code>DOMString</code>
   861   <dd>The <code>conditionText</code> attribute (defined on the <code>CSSConditionRule</code> parent rule),
   862     on getting, must return the value of <code>media.mediaText</code> on the rule.
   864     <p>Setting the <code>conditionText</code> attribute
   865       must set the <code>media.mediaText</code> attribute on the rule.
   866 </dl>
   869 <h3 id="the-csssupportsrule-interface">
   870 The <code>CSSSupportsRule</code> interface</h3>
   872 <p>The <dfn><code>CSSSupportsRule</code></dfn> interface represents a ''@supports'' rule.</p>
   874 <pre class='idl'>interface CSSSupportsRule : CSSConditionRule {
   875 }</pre>
   878 <h3 id="the-cssdocumentrule-interface">
   879 The <code>CSSDocumentRule</code> interface</h3>
   881 <p>The <dfn><code>CSSDocumentRule</code></dfn> interface represents a ''@document'' rule.</p>
   883 <pre class='idl'>interface CSSDocumentRule : CSSConditionRule {
   884 }</pre>
   887 <h3 id='the-css-interface'>
   888 The <code>CSS</code> interface, and the <code title=''>supports()</code> function</h3>
   890 <p>The <dfn id='CSS-interface'><code>CSS</code></dfn> interface holds useful CSS-related functions that do not belong elsewhere.
   892 <pre class='idl'>interface CSS {
   893   boolean supports(DOMString property, DOMString value);
   894   boolean supports(DOMString declaration);
   895 }</pre>
   897 <dl class='idl-methods'>
   898   <dt><code>supports(DOMString property, DOMString value)</code>,
   899     returns <code>boolean</code>
   900   <dt><code>supports(DOMString conditionText)</code>,
   901     returns <code>boolean</code>
   902   <dd>
   903     When the <code title=''>supports()</code> method is invoked with two arguments <var>property</var> and <var>value</var>,
   904     it must return <code>true</code> if <var>property</var> is a literal match for the name of a CSS property that the UA supports,
   905     and <var>value</var> would be successfully parsed as a supported value for that property.
   906     Otherwise, it must return <code>false</code>.
   908     <p>
   909       When invoked with a single <var>conditionText</var> argument,
   910       it must return <code>true</code> if <var>conditionText</var>,
   911       when parsed and evaluated as a <code>supports_condition</code>,
   912       would return true.
   913       Otherwise, it must return <code>false</code>.
   914   </dl>
   917 <h2 id="conformance">Conformance</h2>
   919 <h3 id="base-modules">Base Modules</h3>
   921 <p>This specification defines conformance in terms of base modules,
   922 which are modules that this specification builds on top of.  The base
   923 modules of this module are:</p>
   925 <ul>
   926 <li>[[!CSS21]]</li>
   927 </ul>
   929 <p>All of the conformance requirements of all base modules are
   930 incorporated as conformance requirements of this module, except where
   931 overridden by this module.</p>
   933 <p>Additionally, all conformance requirements related to validity of
   934 syntax in this module and all of its base modules are to be interpreted
   935 as though all syntax in all of those modules is valid.</p>
   937 <div class="example"><p>For example, this means that grammar presented
   938 in modules other than [[!CSS21]] must obey the requirements that
   939 [[!CSS21]] defines for the parsing of properties, and that requirements
   940 for handling invalid syntax in [[!CSS21]] do not treat syntax added by
   941 other modules as invalid.</p></div>
   943 <p>Additionally, the set of valid syntax can be increased by the
   944 conformance of a style sheet or processor to additional modules; use of
   945 such syntax does not make a style sheet nonconformant and failure to
   946 treat such syntax as invalid does not make a processor
   947 nonconformant.</p>
   949 <h3 id="conformance-classes">Conformance Classes</h3>
   951   <p>Conformance to the CSS Conditional Rules Module is defined for three
   952   conformance classes:
   953   <dl>
   954     <dt><dfn title="conformance::style sheet" id="conform-style-sheet">style sheet</dfn>
   955       <dd>A <a href="http://www.w3.org/TR/CSS21/conform.html#style-sheet">CSS
   956       style sheet</a>.</dd>
   957     <dt><dfn title="conformance::processor" id="conform-processor">processor</dfn></dt>
   958       <dd>A tool that reads CSS style sheets:  it may be a renderer or
   959       <a
   960       href="http://www.w3.org/TR/CSS21/conform.html#user-agent">user-agent</a>
   961       that interprets the semantics of a style sheet and renders
   962       documents that use style sheets, or it may be a validator that
   963       checks style sheets.</dd>
   964     <dt><dfn title="conformance::authoring tool" id="conform-authoring-tool">authoring tool</dfn></dt>
   965       <dd>A tool that writes a style sheet.</dd>
   966   </dl>
   968   <p>A style sheet is conformant to the CSS Conditional Rules Module
   969   if it meets all of the conformance requirements in the module that are
   970   described as requirements of style sheets.</p>
   972   <p>A processor is conformant to the CSS Conditional Rules Module if it
   973   meets all applicable conformance requirements in the module that are
   974   described as requirements of processors.  In general, all requirements
   975   are applicable to renderers.  Requirements concerning a part of CSS
   976   not performed by a processor are not applicable, e.g., requirements
   977   related to rendering are not applicable to a validator.  The inability
   978   of a processor to correctly render a document due to limitations of
   979   the device does not make it non-conformant. (For example, a renderer
   980   is not required to render color on a monochrome monitor.)</p>
   982   <p>An authoring tool is conformant to the CSS Conditional Rules Module
   983   if it writes style sheets that conform to the module and (if it reads
   984   CSS) it is a conformant processor.</p>
   986 <h3 id="partial">
   987 Partial Implementations</h3>
   989   <p>So that authors can exploit the forward-compatible parsing rules to
   990   assign fallback values, CSS renderers <strong>must</strong>
   991   treat as invalid (and <a href="http://www.w3.org/TR/CSS21/conform.html#ignore">ignore
   992   as appropriate</a>) any at-rules, properties, property values, keywords,
   993   and other syntactic constructs for which they have no usable level of
   994   support. In particular, user agents <strong>must not</strong> selectively
   995   ignore unsupported component values and honor supported values in a single
   996   multi-value property declaration: if any value is considered invalid
   997   (as unsupported values must be), CSS requires that the entire declaration
   998   be ignored.</p>
  1000 <h3 id="experimental">Experimental Implementations</h3>
  1002   <p>To avoid clashes with future CSS features, the CSS specifications
  1003   reserve a <a href="http://www.w3.org/TR/CSS21/syndata.html#vendor-keywords">prefixed
  1004   syntax</a> for proprietary property and value extensions to CSS. The CSS
  1005   Working Group recommends that experimental implementations of features in
  1006   CSS Working Drafts also use vendor-prefixed property or value names. This
  1007   avoids any incompatibilities with future changes in the draft. Once a
  1008   specification reaches the Candidate Recommendation stage, implementors
  1009   should implement the non-prefixed syntax for any feature they consider to
  1010   be correctly implemented according to spec.</p>
  1012 <h3 id="cr-exit-criteria">CR Exit Criteria</h3>
  1014   <p>For this specification to be advanced to Proposed Recommendation,
  1015   there must be at least two independent, interoperable implementations
  1016   of each feature. Each feature may be implemented by a different set of
  1017   products, there is no requirement that all features be implemented by
  1018   a single product. For the purposes of this criterion, we define the
  1019   following terms:
  1021   <dl>
  1022     <dt>independent <dd>each implementation must be developed by a
  1023     different party and cannot share, reuse, or derive from code
  1024     used by another qualifying implementation. Sections of code that
  1025     have no bearing on the implementation of this specification are
  1026     exempt from this requirement.
  1028     <dt>interoperable <dd>passing the respective test case(s) in the
  1029     official CSS test suite, or, if the implementation is not a Web
  1030     browser, an equivalent test. Every relevant test in the test
  1031     suite should have an equivalent test created if such a user
  1032     agent (UA) is to be used to claim interoperability. In addition
  1033     if such a UA is to be used to claim interoperability, then there
  1034     must one or more additional UAs which can also pass those
  1035     equivalent tests in the same way for the purpose of
  1036     interoperability. The equivalent tests must be made publicly
  1037     available for the purposes of peer review.
  1039     <dt>implementation <dd>a user agent which:
  1041     <ol class=inline>
  1042       <li>implements the specification.
  1044       <li>is available to the general public. The implementation may
  1045       be a shipping product or other publicly available version
  1046       (i.e., beta version, preview release, or “nightly build”). 
  1047       Non-shipping product releases must have implemented the
  1048       feature(s) for a period of at least one month in order to
  1049       demonstrate stability.
  1051       <li>is not experimental (i.e., a version specifically designed
  1052       to pass the test suite and is not intended for normal usage
  1053       going forward).
  1054     </ol>
  1055   </dl>
  1057   <p>The specification will remain Candidate Recommendation for at least
  1058   six months.
  1060 <h2 class=no-num id="grammar">Grammar</h2>
  1062 <p>In order to allow these new @-rules in CSS style sheets, this
  1063 specification modifies the <code>stylesheet</code> production in the <a
  1064 href="http://www.w3.org/TR/CSS21/grammar.html">Appendix G</a> grammar of
  1065 [[!CSS21]] by replacing the <code>media</code> production defined in
  1066 [[!CSS21]] with the <code>media</code> production defined in this one,
  1067 and additionally inserting <code>| supports_rule | document_rule</code>
  1068 alongside <code>ruleset | media | page</code>.</p>
  1070 <h2 class=no-num id="acknowledgments">Acknowledgments</h2>
  1072 <p>
  1073 Thanks to the ideas and feedback from
  1074 Tab Atkins,
  1075 <span lang="tr">Tantek Çelik</span>,
  1076 Alex Danilo,
  1077 Elika Etemad,
  1078 Pascal Germroth,
  1079 <span lang="de">Björn Höhrmann</span>,
  1080 Paul Irish,
  1081 Vitor Menezes,
  1082 Alex Mogilevsky,
  1083 Chris Moschini,
  1084 Ben Ward,
  1085 Zack Weinberg,
  1086 Estelle Weyl,
  1087 Boris Zbarsky,
  1088 and all the rest of the <a href="http://lists.w3.org/Archives/Public/www-style/">www-style</a> community.
  1090 </p>
  1092 <h2 class=no-num id="references">References</h2>
  1095 <h3 class="no-num" id="normative-references">Normative references</h3>
  1096 <!--normative-->
  1098 <h3 class="no-num" id="other-references">Other references</h3>
  1099 <!--informative-->
  1101 <h2 class="no-num" id="index">Index</h2>
  1102 <!--index-->
  1104 </body>
  1105 </html>
  1106 <!-- Keep this comment at the end of the file
  1107 Local variables:
  1108 mode: sgml
  1109 sgml-declaration:"~/SGML/HTML4.decl"
  1110 sgml-default-doctype-name:"html"
  1111 sgml-minimize-attributes:t
  1112 sgml-nofill-elements:("pre" "style" "br")
  1113 sgml-live-element-indicator:t
  1114 sgml-omittag:nil
  1115 sgml-shorttag:nil
  1116 sgml-namecase-general:t
  1117 sgml-general-insert-case:lower
  1118 sgml-always-quote-attributes:t
  1119 sgml-indent-step:nil
  1120 sgml-indent-data:t
  1121 sgml-parent-document:nil
  1122 sgml-exposed-tags:nil
  1123 sgml-local-catalogs:nil
  1124 sgml-local-ecat-files:nil
  1125 End:
  1126 -->

mercurial