Tue, 09 Oct 2012 17:30:03 -0700
[css3-conditional] Added the ability to put functions into @supports, for future extension purposes.
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/2012/WD-css3-conditional-20120911/">http://www.w3.org/TR/2012/WD-css3-conditional-20120911/</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>Feedback:
41 <dd><a
42 href="http://lists.w3.org/Archives/Public/www-style/">www-style@w3.org</a>
43 with subject line “<kbd>[[SHORTNAME]] <var>… message topic
44 …</var></kbd>”
46 <dt>Test suite:
47 <dd><a href="https://test.csswg.org/shepherd/search/spec/css3-conditional/">submitted tests</a>; no built test suite yet
49 </dl>
51 <!--copyright-->
53 <hr title="Separator for header">
54 </div>
56 <h2 class="no-num no-toc" id="abstract">Abstract</h2>
58 <p>CSS is a language for describing the rendering of structured documents
59 (such as HTML and XML) on screen, on paper, in speech, etc. This module
60 contains the features of CSS for conditional processing of parts of
61 style sheets, conditioned on capabilities of the processor or the
62 document the style sheet is being applied to.
63 It includes and extends the functionality of CSS level 2 [[!CSS21]],
64 which builds on CSS level 1 [[CSS1]].
65 The main extensions compared to level 2 are
66 allowing nesting of certain at-rules inside '@media',
67 the addition of the '@supports' and '@document'
68 rules for conditional processing.
70 <h2 class="no-num no-toc" id="status">Status of this document</h2>
72 <!--status-->
74 <p>The following features are at risk:
75 <ul>
76 <li>The inclusion of '@font-face' rules and
77 '@keyframes' rules as allowed within all of the @-rules in
78 this specification is at risk, though only because of the relative
79 rates of advancement of specifications. If this specification is able
80 to advance faster than one or both of the specifications defining
81 those rules, then the inclusion of those rules will move from this
82 specification to the specification defining those rules.</li>
84 <li>The addition of support for @-rules inside of conditional grouping
85 rules is at risk; if interoperable implementations are not found, it
86 may be removed to advance the other features in this specification to
87 Proposed Recommendation.</li>
89 <li>The '@supports' 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 <li>The support for functions inside of ''@supports'' is at risk.</li>
95 <li>The '@document' rule is at risk; if interoperable
96 implementations are not found, it may be removed to advance the other
97 features in this specification to Proposed Recommendation.</li>
99 </ul>
101 <h2 class="no-num no-toc" id="contents">Table of contents</h2>
103 <!--toc-->
105 <h2 id="introduction">Introduction</h2>
107 <h3 id="context">Background</h3>
109 <p><em>This section is not normative.</em>
111 <p>[[!CSS21]] defines one type of conditional group rule, the
112 '@media' rule, and allows only rulesets (not other @-rules)
113 inside of it. The '@media' rule provides the ability to
114 have media-specific style sheets, which is also provided by style
115 sheet linking features such as '@import' and
116 <code class="html"><link></code>. The restrictions on the contents of
117 '@media' rules made them less useful; they have forced authors
118 using CSS features involving @-rules in media-specific style sheets to
119 use separate style sheets for each medium.</p>
121 <p>This specification extends the rules for the contents of
122 conditional group rules to allow other @-rules, which enables authors
123 to combine CSS features involving @-rules with media specific style
124 sheets within a single style sheet.</p>
126 <p>This specification also defines additional types of conditional
127 group rules, '@supports' and '@document', to
128 address author and user requirements.</p>
130 <p>The '@supports' rule allows CSS to be conditioned on
131 implementation support for CSS properties and values. This rule makes
132 it much easier for authors to use new CSS features and provide good
133 fallback for implementations that do not support those features. This
134 is particularly important for CSS features that provide new layout
135 mechanisms, and for other cases where a set of related styles needs to
136 be conditioned on property support.</p>
138 <p>The '@document' rule allows CSS to be conditioned on the
139 page to which the style sheet is being applied. This allows users to
140 apply styles to a particular page or group of pages, which greatly
141 increases the power of user style sheets.</p>
143 <h3 id="placement">Module Interactions</h3>
145 <p>This module replaces and extends the '@media' rule
146 feature defined in [[!CSS21]] section <var>7.2.1</var> and
147 incorporates the modifications previously made non-normatively by
148 [[!MEDIAQ]] section <var>1</var>.</p>
150 <p>Its current definition depends on @-rules defined in [[!CSS3-FONTS]]
151 and [[!CSS3-ANIMATIONS]], but that dependency is only on the
152 assumption that those modules will advance ahead of this one. If this
153 module advances faster, then the dependency will be reversed.</p>
155 <h3 id="conventions">Document Conventions</h3>
157 <p>Conformance requirements are expressed with a combination of
158 descriptive assertions and RFC 2119 terminology. The key words “MUST”,
159 “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”,
160 “RECOMMENDED”, “MAY”, and “OPTIONAL” in the normative parts of this
161 document are to be interpreted as described in RFC 2119.
162 However, for readability, these words do not appear in all uppercase
163 letters in this specification.
165 <p>All of the text of this specification is normative except sections
166 explicitly marked as non-normative, examples, and notes. [[!RFC2119]]</p>
168 <p>Examples in this specification are introduced with the words “for example”
169 or are set apart from the normative text with
170 <code class="html">class="example"</code>, like this:
172 <div class="example">
173 <p>This is an example of an informative example.</p>
174 </div>
176 <p>Informative notes begin with the word “Note” and are set apart from the
177 normative text with <code class="html">class="note"</code>, like this:
179 <p class="note">Note, this is an informative note.</p>
181 <h2 id="processing">Processing of conditional group rules</h2>
183 <p>This specification defines some CSS @-rules, called <dfn>conditional
184 group rules</dfn>, that associate a condition with a group of other
185 CSS rules. These different rules allow testing different types of
186 conditions, but share common behavior for how their contents are used
187 when the condition is true and when the condition is false.</p>
189 <div class="example">
190 <p>For example, this rule:</p>
191 <pre>@media print {
192 #navigation { display: none }
193 }</pre>
194 <p>causes a particular CSS rule (making elements with ID "navigation" be
195 display:none) apply only when the style sheet is used for a print
196 medium. Likewise, this CSS rule:</p>
197 <pre>@document url("http://www.example.com/") {
198 #example1 { display: none }
199 }</pre>
200 <p>does the same type of conditional application, but using a different
201 condition: whether the style sheet is being applied to the page
202 <code>http://www.example.com/</code>.</p>
203 </div>
205 <p>Each conditional group rule has a condition, which at any time
206 evaluates to true or false. When the condition is true, CSS processors
207 <strong>must</strong> apply the rules inside the group rule as though
208 they were at the group rule's location; when the condition is false, CSS
209 processors <strong>must not</strong> apply any of rules inside the group
210 rule. The current state of the condition does not affect the CSS object
211 model, in which the contents of the group rule always remain within the
212 group rule.</p>
214 <p>This means that when multiple conditional group rules are nested,
215 a rule inside of both of them applies only when all of the rules'
216 conditions are true.</p>
218 <div class="example">For example, with this set of nested rules:
219 <pre>@media print { // rule (1)
220 #navigation { display: none }
221 @media (max-width: 12cm) { // rule (2)
222 .note { float: none }
223 }
224 }</pre>
225 the condition of the rule marked (1) is true for print media, and the
226 condition of the rule marked (2) is true when the width of the display
227 area (which for print media is the page box) is less than or equal to
228 12cm. Thus the rule ''#navigation { display: none }'' applies
229 whenever this style sheet is applied to print media, and the rule
230 ''.note { float: none }'' is applied only when the style sheet
231 is applied to print media <em>and</em> the width of the page box is less
232 than or equal to 12 centimeters.</div>
234 <p>When the condition for a conditional group rule changes, CSS
235 processors <strong>must</strong> reflect that the rules now apply or no
236 longer apply, except for properties whose definitions define effects of
237 computed values that persist past the lifetime of that value (such as
238 for some properties in [[CSS3-TRANSITIONS]] and
239 [[!CSS3-ANIMATIONS]]).</p>
241 <h2 id="contents-of">Contents of conditional group rules</h2>
243 <p class="issue">There is also likely demand for using these
244 conditions with '@import'. We should see if we can
245 come up with sensible syntax for that, perhaps functional notation
246 at the end of the '@import' rule.</p>
248 <p>The syntax of each conditional group rule consists of some syntax
249 specific to the type of rule followed by a <dfn>group rule body</dfn>,
250 which is a block (pair of braces) containing a sequence of rules.</p>
252 <p>A group rule body is allowed to contain rulesets and any @-rules that
253 are allowed at the top level of a style sheet before and after a
254 ruleset. This means that @-rules that must occur at the beginning of
255 the style sheet (such as '@charset', '@import',
256 and '@namespace' rules) are not allowed inside of conditional group
257 rules. Conditional group rules can be nested.</p>
259 <p>In terms of the grammar, this specification defines the following
260 productions for use in the grammar of conditional group rules:</p>
262 <pre>nested_statement
263 : ruleset | media | page | font_face_rule | keyframes_rule |
264 supports_rule | document_rule
265 ;
267 group_rule_body
268 : '{' S* nested_statement* '}' S*
269 ;</pre>
270 <p>
271 in which all the productions are defined in that grammar with the
272 exception of <code>font_face_rule</code> <span class="issue">not</span>
273 defined in [[!CSS3-FONTS]], <code>keyframes_rule</code> defined in
274 [[!CSS3-ANIMATIONS]], and <code>media</code>, <code>supports_rule</code>
275 and <code>document_rule</code> defined in this specification.</p>
277 <p>In general, future CSS specifications that add new @-rules that are
278 not forbidden to occur after some other types of rules should modify
279 this <code>nested_statement</code> production to keep the grammar
280 accurate.</p>
282 <p>Style sheets <strong>must not</strong> use rules other than the allowed ones inside
283 conditional group rules.</p>
285 <p>CSS processors <strong>must</strong> ignore rules that are not
286 allowed within a group rule, and <strong>must</strong> handle invalid
287 rules inside of group rules as described in <a
288 href="http://www.w3.org/TR/CSS21/syndata.html#parsing-errors">section
289 4.2 (Rules for handling parsing errors)</a>, <a
290 href="http://www.w3.org/TR/CSS21/syndata.html#at-rules">section 4.1.5
291 (At-rules)</a>, and <a
292 href="http://www.w3.org/TR/CSS21/syndata.html#rule-sets">section 4.1.7
293 (Rule sets, declaration blocks, and selectors)</a> of [[!CSS21]].</p>
295 <h2 id="use">Placement of conditional group rules</h2>
297 <p>Conditional group rules are allowed at the top-level of a style
298 sheet, and inside other conditional group rules. CSS processors
299 <strong>must</strong> process such rules as <a
300 href="#processing">described above</a>.</p>
302 <p>Any rules that are not allowed after a ruleset (e.g., ''@charset'',
303 ''@import'', or ''@namespace'' rules) are also not allowed after a
304 conditional group rule. Therefore, style sheets <strong>must
305 not</strong> place such rules after a conditional group rules, and CSS
306 processors <strong>must</strong> ignore such rules.</p>
308 <h2 id="at-media">Media-specific style sheets: the '@media' rule</h2>
310 <p>The <dfn>'@media' rule</dfn> is a conditional group rule whose
311 condition is a media query. It consists of the at-keyword
312 '@media' followed by a (possibly empty) media query list (as
313 defined in [[!MEDIAQ]]), followed by a group rule body. The condition
314 of the rule is the result of the media query.</p>
316 <div class="example">
317 <p>This '@media' rule:</p>
318 <pre>@media print, (max-width: 600px) {
319 #extra_navigation { display: none }
320 }</pre>
321 <p>has the condition ''print, (max-width: 600px)'', which is
322 true for print media and for devices whose width is at most 600px. When
323 either of these is true, the condition of the rule is true, and the rule
324 ''#extra_navigation { display: none }'' is applied.
325 </div>
327 <p>In terms of the grammar, this specification extends the
328 <code>media</code> production in the
329 <a href="http://www.w3.org/TR/CSS21/grammar.html">Grammar of CSS 2.1</a>
330 ([[!CSS21]], Appendix G) into:
331 <pre>media
332 : MEDIA_SYM S* media_query_list group_rule_body
333 ;</pre>
334 <p>where the <code>group_rule_body</code> production is defined in this
335 specification, the <code>media_query_list</code> production is defined
336 in [[!MEDIAQ]], and the others are defined in the <a
337 href="http://www.w3.org/TR/CSS21/grammar.html">Grammar of CSS 2.1</a>
338 ([[!CSS21]], Appendix G).
340 <h2 id="at-supports">Feature queries: the '@supports' rule</h2>
342 <p>The <dfn>'@supports' rule</dfn> is a conditional group
343 rule whose condition tests whether the user agent supports CSS
344 property:value pairs. Authors can use it to write style sheets that use
345 new features when available but degrade gracefully when those features
346 are not supported. CSS has existing mechanisms for graceful
347 degradation, such as ignoring unsupported properties or values, but
348 these are not always sufficient when large groups of styles need to be
349 tied to the support for certain features, as is the case for use of new
350 layout system features.</p>
352 <p>The syntax of the condition in the '@supports' rule is
353 slightly more complicated than for the other conditional group rules
354 (though has some similarities to media queries) since:</p>
355 <ul>
356 <li>negation is needed so that the new-feature styles and the fallback
357 styles can be separated (within the forward-compatible grammar's rules
358 for the syntax of @-rules), and not required to override each other</li>
359 <li>conjunction (and) is needed so that multiple required features can
360 be tested</li>
361 <li>disjunction (or) is needed when there are multiple alternative
362 features for a set of styles, particularly when some of those
363 alternatives are vendor-prefixed properties or values</li>
364 </ul>
366 <p>Therefore, the syntax of the '@supports' rule allows
367 testing for property:value pairs, and arbitrary conjunctions (and),
368 disjunctions (or), and negations (not) of them.</p>
370 <p>This extends the lexical scanner in the
371 <a href="http://www.w3.org/TR/CSS21/grammar.html">Grammar of CSS 2.1</a>
372 ([[!CSS21]], Appendix G) by adding:
373 <pre>
374 @{S}{U}{P}{P}{O}{R}{T}{S} {return SUPPORTS_SYM;}
375 {O}{R} {return OR;}
376 </pre>
377 <p>and the grammar by adding</p>
378 <pre><dfn>supports_rule</dfn>
379 : SUPPORTS_SYM S* supports_condition group_rule_body
380 ;
382 <dfn>supports_condition</dfn>
383 : supports_negation | supports_conjunction | supports_disjunction |
384 supports_condition_in_parens
385 ;
387 <dfn>supports_condition_in_parens</dfn>
388 : ( '(' S* supports_condition ')' S* ) | supports_declaration_condition
389 ;
391 <dfn>supports_negation</dfn>
392 : NOT S* supports_condition_in_parens
393 ;
395 <dfn>supports_conjunction</dfn>
396 : supports_condition_in_parens ( AND S* supports_condition_in_parens )+
397 ;
399 <dfn>supports_disjunction</dfn>
400 : supports_condition_in_parens ( OR S* supports_condition_in_parens )+
401 ;
403 <dfn>supports_declaration_condition</dfn>
404 : '(' S* core_declaration ')' S* | FUNCTION S* [any|unused]* ')'
405 ;</pre>
406 <p>in which <code>core_declaration</code> is the production
407 <code>declaration</code> in the core syntax of CSS defined in <a
408 href="http://www.w3.org/TR/CSS21/syndata.html#tokenization">section
409 4.1.1 (Tokenization)</a> of [[!CSS21]],
410 and the <code>AND</code> and <code>NOT</code> tokens are defined in
411 the Media Queries specification [[!MEDIAQ]].</p>
413 <p>Any ''@supports'' rule that does not parse according to the grammar
414 above is invalid. Style sheets <strong>must not</strong> use such a
415 rule and processors <strong>must</strong> ignore such a rule.</p>
417 <p class="note">Note that this means that declarations that meet the
418 forward-compatible syntax for declarations are permitted (and support
419 for them is then tested by the ''@supports'' rule), but declarations
420 that do not meet the forward-compatible syntax for declarations cause
421 the entire ''@supports'' rule to be ignored.</p>
423 <p>Each of these grammar terms is associated with a boolean result, as
424 follows:</p>
425 <dl>
426 <dt>supports_condition</dt>
427 <dd>
428 The result is the result of the single child term.
429 </dd>
431 <dt>supports_condition_in_parens</dt>
432 <dd>
433 The result is the result of the single <code>supports_condition</code>
434 or <code>supports_declaration_condition</code> child term.
435 </dd>
437 <dt>supports_negation</dt>
438 <dd>
439 The result is the <em>negation</em> of the result of the
440 <code>supports_condition_in_parens</code> child term.
441 </dd>
443 <dt>supports_conjunction</dt>
444 <dd>
445 The result is true if the result of <em>all</em> of the
446 <code>supports_condition_in_parens</code> child terms is true;
447 otherwise it is false.
448 </dd>
450 <dt>supports_disjunction</dt>
451 <dd>
452 The result is true if the result of <em>any</em> of the
453 <code>supports_condition_in_parens</code> child terms is true;
454 otherwise it is false.
455 </dd>
457 <dt>supports_declaration_condition</dt>
458 <dd>
459 The result is whether the CSS processor <a
460 href="#support-definition">supports</a> the declaration or function.
461 </dd>
462 </dl>
464 <p>The condition of the '@supports' rule is the result of the
465 <code>supports_condition</code> term that is a child of the
466 <code>supports_rule</code> term.</p>
468 <div class="example">
469 <p>For example, the following rule</p>
470 <pre>@supports ( display: flexbox ) {
471 body, #navigation, #content { display: flexbox; }
472 #navigation { background: blue; color: white; }
473 #article { background: white; color: black; }
474 }</pre>
475 <p>applies the rules inside the '@supports' rule only when
476 ''display: flexbox'' is supported.</p>
477 </div>
479 <div class="example">
480 <p>The following example shows an additional '@supports' rule that can
481 be used to provide an alternative for when ''display: flexbox'' is not
482 supported:</p>
483 <pre>@supports not ( display: flexbox ) {
484 body { width: 100%; height: 100%; background: white; color: black; }
485 #navigation { width: 25%; }
486 #article { width: 75%; }
487 }</pre>
488 <p>Note that the 'width' declarations may be harmful to the
489 flexbox-based layout, so it is important that they be present only in
490 the non-flexbox styles.</p>
491 </div>
493 <div class="example">
494 <p>The following example checks for support for the 'box-shadow'
495 property, including checking for support for vendor-prefixed versions of
496 it. When the support is present, it specifies both 'box-shadow' (with
497 the prefixed versions) and 'color' in a way what would cause the text to
498 become invisible were 'box-shadow' not supported.</p>
499 <pre>@supports ( box-shadow: 2px 2px 2px black ) or
500 ( -moz-box-shadow: 2px 2px 2px black ) or
501 ( -webkit-box-shadow: 2px 2px 2px black ) or
502 ( -o-box-shadow: 2px 2px 2px black ) {
503 .outline {
504 color: white;
505 -moz-box-shadow: 2px 2px 2px black;
506 -webkit-box-shadow: 2px 2px 2px black;
507 -o-box-shadow: 2px 2px 2px black;
508 box-shadow: 2px 2px 2px black; /* unprefixed last */
509 }
510 }</pre></div>
512 <p>To avoid confusion between ''and'' and ''or'', the syntax requires
513 that both ''and'' and ''or'' be specified explicitly (rather than, say,
514 using commas or spaces for one of them). Likewise, to avoid confusion
515 caused by precedence rules, the syntax does not allow ''and'', ''or'',
516 and ''not'' operators to be mixed without a layer of parentheses.</p>
518 <div class="example">
519 <p>For example, the following rule is not valid:
520 <pre class="illegal">@supports (transition-property: color) or
521 (animation-name: foo) and
522 (transform: rotate(10deg)) {
523 // ...
524 }</pre>
525 <p>Instead, authors must write one of the following:</p>
526 <pre>@supports ((transition-property: color) or
527 (animation-name: foo)) and
528 (transform: rotate(10deg)) {
529 // ...
530 }</pre>
531 <pre>@supports (transition-property: color) or
532 ((animation-name: foo) and
533 (transform: rotate(10deg))) {
534 // ...
535 }</pre>
536 </div>
538 <p>The declaration being tested must always occur within parentheses,
539 when it is the only thing in the expression.<p>
541 <div class="example">
542 <p>For example, the following rule is not valid:
543 <pre class="illegal">@supports display: flexbox {
544 // ...
545 }</pre>
546 <p>Instead, authors must write:</p>
547 <pre>@supports (display: flexbox) {
548 // ...
549 }</pre>
550 </div>
552 <p>The syntax allows extra parentheses when they are not needed. This
553 flexibility is sometimes useful for authors (for example, when
554 commenting out parts of an expression) and may also be useful for
555 authoring tools.</p>
557 <div class="example">
558 <p>For example, authors may write:</p>
559 <pre>@supports ((display: flexbox)) {
560 // ...
561 }</pre>
562 </div>
564 <p>A trailing ''!important'' on a declaration being tested is allowed,
565 though it won't change the validity of the declaration.
567 <div class="example">
568 <p>For example, the following rule is valid:
569 <pre>@supports (display: flexbox !important) {
570 // ...
571 }</pre>
572 </div>
574 <h3 id="support-definition">Definition of support</h3>
576 <p>For forward-compatibility,
577 <a href="http://www.w3.org/TR/CSS21/syndata.html#declaration">section 4.1.8
578 (Declarations and properties)</a> of [[!CSS21]]
579 defines rules for handling invalid properties and values.
580 CSS processors that
581 do not implement or partially implement a specification
582 <strong>must</strong> treat any part of a value that they
583 do not implement, or
584 do not have a usable level of support for,
585 as invalid according to this rule
586 for handling invalid properties and values,
587 and therefore <strong>must</strong> discard the declaration as a parse error.</p>
589 <p>A CSS processor is considered to <dfn id="dfn-support">support</dfn>
590 a declaration (consisting of a property and value) if it accepts that
591 declaration (rather than discarding it as a parse error).
592 If a processor does not implement, with a usable level of support,
593 the value given,
594 then it <strong>must not</strong>
595 accept the declaration or claim support for it.</p>
597 <p>A CSS processor is considered to <i>support</i> a function
598 (consisting of a function name and arguments)
599 if it accepts that function
600 (rather than discarding it as a parse error).
601 If a processor does not implement, with a usable level of support,
602 the value given,
603 then it <strong>must not</strong>
604 accept the function or claim support for it.</p>
606 <p>These rules (and the equivalence between them) allow
607 authors to use fallback (either in the [[CSS1]] sense of declarations
608 that are overridden by later declarations or with the new capabilities
609 provided by the ''@supports'' rule in this specification) that works
610 correctly for the features implemented. This applies especially to
611 compound values; implementations must implement all parts of the value
612 in order to consider the declaration supported, either inside a ruleset
613 or in the declaration condition of an ''@supports'' rule.</p>
615 <h2 id="at-document">Document queries: the '@document' rule</h2>
617 <p>The <dfn>'@document' rule</dfn> is a conditional group
618 rule whose condition depends on the
619 <a href="#url-of-doc">URL of the document being styled</a>.
620 This allows style sheets, particularly user style sheets, to have styles
621 that only apply to a set of pages rather than to all pages using the
622 style sheet.</p>
624 <p class="issue">Given that this @-rule is intended primarily for user
625 style sheets, what should this specification say about its use in author
626 style sheets? Should it be forbidden? Should use instead be
627 discouraged? Or should this specification remain neutral on the
628 topic, since there are valid uses in author style sheets?</p>
630 <p id="url-of-doc">The <dfn>URL of the document being styled</dfn> is
631 the URI at which the document is located, excluding any fragment
632 identifiers. (This means, for example, that HTTP redirects have been
633 followed.) If the styles are being applied inside a complete document
634 embedded into the presentation of another (e.g., [[HTML5]]'s <code
635 class="html">iframe</code>, <code class="html">object</code>, or <code
636 class="html">img</code> elements), the relevant URI is that of the
637 frame, not of its container. However, if content from other documents
638 is mixed in via mechanisms that mix content from one document into
639 another (e.g., [[SVG11]]'s <code>use</code> element), then the
640 address of the container document is used.</p>
642 <p class="note">Note: In [[HTML5]], this is the
643 <a href="http://dev.w3.org/html5/spec/dom.html#documents">document's address</a>
644 of a document in a
645 <a href="http://dev.w3.org/html5/spec/browsers.html#browsing-context">browsing context</a>.</p>
647 <div class="issue">What form of normalization is done on URLs and domains
648 before matching? In particular, this specification needs to describe:
649 <ul>
650 <li>what form is used for the <a href="#url-of-doc">URL of the document
651 being styled</a> (and what has been normalized in that form)</li>
652 <li>what normalization (if any) happens to the argument of each of the match
653 functions before the comparison that they describe and</li>
654 <li>whether the
655 comparison algorithm used is string comparison or some other URL
656 comparison algorithm.</li></ul></div>
658 <p>The '@document' rule's condition is written as a
659 comma-separated list of <dfn>URL matching functions</dfn>, and the
660 condition evaluates to true whenever any one of those functions
661 evaluates to true. The following URL matching functions are
662 permitted:</p>
664 <dl>
665 <dt><dfn id="url-exact" title="url()|URL matching functions::exact"><url></dfn></dt>
667 <dd>
668 <p>The 'url()' function is the <dfn>exact url matching
669 function</dfn>. It evaluates to true whenever the <a
670 href="#url-of-doc">URL of the document being styled</a> is exactly
671 the URL given.</p>
673 <p class="Note">The 'url()' function, since it is a core syntax
674 element in CSS, is allowed (subject to different character
675 limitations and thus escaping requirements) to contain an unquoted
676 value (in addition to the string values that are allowed as
677 arguments for all four functions).</p>
679 <div class="example">
680 <p>For example, this rule:</p>
681 <pre>@document url("http://www.w3.org/Style/CSS/") {
682 #summary { background: yellow; color: black}
683 }</pre>
684 <p>styles the <code class="html">summary</code> element on the page
685 <code>http://www.w3.org/Style/CSS/</code>, but not on any other
686 pages.</p>
687 </div>
688 </dd>
690 <dt><dfn id="url-prefix" title="url-prefix()|URL matching functions::prefix">url-prefix(<string>)</dfn></dt>
692 <dd>
693 <p>The 'url-prefix()' function is the <dfn>url prefix
694 matching function</dfn>. It evaluates to true whenever the
695 <a href="#url-of-doc">URL of the document being styled</a>
696 has the argument to the function as an
697 initial substring (which is true when the two strings are equal).
698 When the argument is the empty string, it evaluates to true for all
699 documents.</p>
700 <div class="example">
701 <p>For example, this rule:</p>
702 <pre>@document url-prefix("http://www.w3.org/Style/CSS/") {
703 #summary { background: yellow; color: black}
704 }</pre>
705 <p>styles the <code class="html">summary</code> element on the page
706 <code>http://www.w3.org/Style/CSS/</code> and on the page
707 <code>http://www.w3.org/Style/CSS/Test</code>, but it does not
708 affect the page <code>http://www.w3.org/</code> or the page
709 <code>http://www.example.com/Style/CSS/</code>.</p>
710 </div>
711 </dd>
713 <dt><dfn id="url-domain" title="domain()|URL matching functions::domain">domain(<string>)</dfn></dt>
715 <dd>
716 <p>The 'domain()' function is the <dfn>domain
717 matching function</dfn>. It evaluates to true whenever
718 the <a href="#url-of-doc">URL of the document being styled</a>
719 has a host subcomponent (as defined in [[!URI]])
720 and that host subcomponent is exactly the argument to the
721 'domain()' function or a final substring of the host
722 component is a period (U+002E) immediately followed by the argument
723 to the 'domain()' function.</p>
724 <div class="example">
725 <p>For example, this rule:</p>
726 <pre>@document domain("w3.org") {
727 body { font-size: 16px ! important }
728 }</pre>
729 <p>changes the font size of the body element for pages such as
730 <code>http://www.w3.org/Style/CSS/</code> and
731 <code>http://w3.org/Style/CSS/</code> and
732 <code>http://lists.w3.org/Archives/Public/www-style/</code>
733 but it does not affect the page
734 <code>http://www.example.com/Style/CSS/</code>.</p>
735 </div>
736 </dd>
738 <dt><dfn id="url-regexp" title="regexp()|URL matching functions::regular expression">regexp(<string>)</dfn></dt>
740 <dd>
741 <p>The contents of the <string> argument <strong>must</strong>
742 match the JavaScript <code>Pattern</code> production
743 ([[!ECMA-262-5.1]], section 15.10.1). However,
744 failing to do so is not a CSS syntax error and does not trigger any
745 error handling for CSS syntax errors.</p>
747 <p>The ''regexp()'' function evaluates to true whenever the string
748 argument compiled as a JavaScript regular expression with the
749 <code>global</code>, <code>ignoreCase</code> and
750 <code>multiline</code> flags <em>disabled</em>
751 (see [[!ECMA-262-5.1]], sections 15.10.7.2 through 15.10.7.4)
752 compiles successfully and the resulting regular expression matches
753 the entirety of the
754 <a href="#url-of-doc">URL of the document being styled</a>.</p>
756 <p class="note">Note that regular expression must match the entire
757 URL, not just a part of it.</p>
759 <p class="note">Note that this definition intentionally matches the
760 behavior of the <a
761 href="http://dev.w3.org/html5/spec/common-input-element-attributes.html#attr-input-pattern"><code class="html">pattern</code>
762 attribute</a> on the <code class="html">input</code> element
763 in [[HTML5]].</p>
765 <div class="example">
766 <p>For example, this rule:</p>
767 <pre>@document regexp("http://www.w3.org/TR/\\d{4}/[^/]*-CSS2-\\d{8}/") {
768 body { font-size: 20px ! important }
769 }</pre>
770 <p>changes the font size of the body element for pages such as
771 <code>http://www.w3.org/TR/2011/PR-CSS2-20110412/</code>.</p>
772 <p class="note">Note that the backslashes in the regular
773 expression require CSS escaping as ''\\''.</p>
774 </div>
775 </dd>
777 </dl>
779 <p>Implementations <strong>must</strong> treat any unknown URL matching
780 functions as a syntax error, and thus ignore the '@document' rule.
781 <span class="issue">Should we instead have more complicated error
782 handling rules to make forward-compatibility work differently, or is
783 this rule the best solution for such future expansion anyway?</span></p>
785 <div class="issue">This syntax doesn't offer any ability to do negations,
786 which has been requested in <a
787 href="https://bugzilla.mozilla.org/show_bug.cgi?id=349813">Mozilla bug
788 349813</a>. Use cases that people have wanted negations for
789 include:
790 <ul>
791 <li>User style sheets that want a particular rule in general, but know
792 that that rule does more harm than good on specific sites.</li>
793 <li>Authors who have a rule that they want to apply to most of their
794 pages, but wish to make a few exceptions for.</li>
795 </ul>
796 </div>
798 <p>This extends the lexical scanner in the
799 <a href="http://www.w3.org/TR/CSS21/grammar.html">Grammar of CSS 2.1</a>
800 ([[!CSS21]], Appendix G) by adding:
801 <pre>@{D}{O}{C}{U}{M}{E}{N}{T} {return DOCUMENT_SYM;}</pre>
802 <p>and the grammar by adding</p>
803 <pre>document_rule
804 : DOCUMENT_SYM S+ url_match_fn ( "," S* url_match_fn )* group_rule_body
805 ;
807 url_match_fn
808 : (URI | FUNCTION S* STRING S* ')' ) S*
809 ;</pre>
811 <h2 id="apis">APIs</h2>
813 <h3 id='extentions-to-cssrule-interface'>
814 Extensions to the <code>CSSRule</code> interface</h3>
816 <p>The <code>CSSRule</code> interface is extended as follows:
818 <pre class='idl'>partial interface CSSRule {
819 const unsigned short SUPPORTS_RULE = 12;
820 const unsigned short DOCUMENT_RULE = 13;
821 }</pre>
824 <h3 id='the-cssgroupingrule-interface'>
825 The <code>CSSGroupingRule</code> interface</h3>
827 <p>The <dfn><code>CSSGroupingRule</code></dfn> interface represents an at-rule that contains other rules nested inside itself.
829 <pre class='idl'>interface CSSGroupingRule : CSSRule {
830 readonly attribute CSSRuleList cssRules;
831 unsigned long insertRule (DOMString rule, unsigned long index);
832 void deleteRule (unsigned long index);
833 }</pre>
835 <dl class='idl-attributes'>
836 <dt><code>cssRules</code> of type <code>CSSRuleList</code>, readonly
837 <dd>The <code>cssRules</code> attribute must return a <code>CSSRuleList</code>
838 object for the list of CSS rules nested inside the grouping rule.
839 </dl>
841 <dl class='idl-methods'>
842 <dt><code>insertRule(DOMString rule, unsigned long index)</code>, returns
843 <code>unsigned long</code>
844 <dd>The <code>insertRule</code> operation must insert a CSS rule <var>rule</var>
845 into the CSS rule list returned by <code>cssRules</code> at <var>index</var>.
847 <dt><code>deleteRule (unsigned long index)</code>, return <code>void</code>
848 <dd>The <code>deleteRule</code> operation must remove a CSS rule from the
849 CSS rule list returned by <code>cssRules</code> at <var>index</var>.
850 </dl>
853 <h3 id="the-cssconditionrule-interface">
854 The <code>CSSConditionRule</code> interface</h3>
856 <p>The <dfn><code>CSSConditionRule</code></dfn> interface represents all the "conditional" at-rules,
857 which consist of a condition and a statement block.
859 <pre class='idl'>interface CSSConditionRule : CSSGroupingRule {
860 attribute DOMString conditionText;
861 }</pre>
863 <dl class='idl-attributes'>
865 <dt><code>conditionText</code> of type <code>DOMString</code>
866 <dd>
867 <p>The <code>conditionText</code> attribute represents
868 the condition of the rule.
869 Since what this condition does
870 varies between the derived interfaces of <code>CSSConditionRule</code>,
871 those derived interfaces
872 may specify different behavior for this attribute
873 (see, for example, <code>CSSMediaRule</code> below).
874 In the absence of such rule-specific behavior,
875 the following rules apply:</p>
877 <p>The <code>conditionText</code> attribute, on getting, must return
878 the result of serializing the associated condition.
880 <p>On setting the <code>conditionText</code> attribute these steps
881 must be run:
883 <ol>
884 <li>Trim the given value of white space.
885 <li>If the given value matches the grammar of the
886 appropriate condition production for the given rule,
887 replace the associated CSS condition with the given value.
888 <li>Otherwise, do nothing.
889 </ol>
890 </dl>
893 <h3 id="the-cssmediarule-interface">
894 The <code>CSSMediaRule</code> interface</h3>
896 <p>The <dfn><code>CSSMediaRule</code></dfn> interface represents a ''@media'' rule:
898 <pre class='idl'>interface CSSMediaRule : CSSConditionRule {
899 readonly attribute MediaList media;
900 }</pre>
902 <dl class='idl-attributes'>
903 <dt><code>media</code> of type <code>MediaList</code>, readonly
904 <dd>The <code>media</code> attribute must return a <code>MediaList</code> object
905 for the list of media queries specified with the ''@media'' rule.
907 <dt><code>conditionText</code> of type <code>DOMString</code>
908 <dd>The <code>conditionText</code> attribute (defined on the <code>CSSConditionRule</code> parent rule),
909 on getting, must return the value of <code>media.mediaText</code> on the rule.
911 <p>Setting the <code>conditionText</code> attribute
912 must set the <code>media.mediaText</code> attribute on the rule.
913 </dl>
916 <h3 id="the-csssupportsrule-interface">
917 The <code>CSSSupportsRule</code> interface</h3>
919 <p>The <dfn><code>CSSSupportsRule</code></dfn> interface represents a ''@supports'' rule.</p>
921 <pre class='idl'>interface CSSSupportsRule : CSSConditionRule {
922 }</pre>
925 <h3 id="the-cssdocumentrule-interface">
926 The <code>CSSDocumentRule</code> interface</h3>
928 <p>The <dfn><code>CSSDocumentRule</code></dfn> interface represents a ''@document'' rule.</p>
930 <pre class='idl'>interface CSSDocumentRule : CSSConditionRule {
931 }</pre>
934 <h3 id='the-css-interface'>
935 The <code>CSS</code> interface, and the <code title=''>supports()</code> function</h3>
937 <p>The <dfn id='CSS-interface'><code>CSS</code></dfn> interface holds useful CSS-related functions that do not belong elsewhere.
939 <pre class='idl'>interface CSS {
940 boolean supports(DOMString property, DOMString value);
941 boolean supports(DOMString declaration);
942 }</pre>
944 <dl class='idl-methods'>
945 <dt><code>supports(DOMString property, DOMString value)</code>,
946 returns <code>boolean</code>
947 <dt><code>supports(DOMString conditionText)</code>,
948 returns <code>boolean</code>
949 <dd>
950 When the <code title=''>supports()</code> method is invoked with two arguments <var>property</var> and <var>value</var>,
951 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,
952 and <var>value</var> would be successfully parsed as a supported value for that property.
953 Otherwise, it must return <code>false</code>.
955 <p>
956 When invoked with a single <var>conditionText</var> argument,
957 it must return <code>true</code> if <var>conditionText</var>,
958 when parsed and evaluated as a <code>supports_condition</code>,
959 would return true.
960 Otherwise, it must return <code>false</code>.
961 </dl>
964 <h2 class=no-num id="grammar">Grammar</h2>
966 <p>In order to allow these new @-rules in CSS style sheets, this
967 specification modifies the <code>stylesheet</code> production in the <a
968 href="http://www.w3.org/TR/CSS21/grammar.html">Appendix G</a> grammar of
969 [[!CSS21]] by replacing the <code>media</code> production defined in
970 [[!CSS21]] with the <code>media</code> production defined in this one,
971 and additionally inserting <code>| supports_rule | document_rule</code>
972 alongside <code>ruleset | media | page</code>.</p>
975 <h2 id="conformance">Conformance</h2>
977 <h3 id="base-modules">Base Modules</h3>
979 <p>This specification defines conformance in terms of base modules,
980 which are modules that this specification builds on top of. The base
981 modules of this module are:</p>
983 <ul>
984 <li>[[!CSS21]]</li>
985 </ul>
987 <p>All of the conformance requirements of all base modules are
988 incorporated as conformance requirements of this module, except where
989 overridden by this module.</p>
991 <p>Additionally, all conformance requirements related to validity of
992 syntax in this module and all of its base modules are to be interpreted
993 as though all syntax in all of those modules is valid.</p>
995 <div class="example"><p>For example, this means that grammar presented
996 in modules other than [[!CSS21]] must obey the requirements that
997 [[!CSS21]] defines for the parsing of properties, and that requirements
998 for handling invalid syntax in [[!CSS21]] do not treat syntax added by
999 other modules as invalid.</p></div>
1001 <p>Additionally, the set of valid syntax can be increased by the
1002 conformance of a style sheet or processor to additional modules; use of
1003 such syntax does not make a style sheet nonconformant and failure to
1004 treat such syntax as invalid does not make a processor
1005 nonconformant.</p>
1007 <h3 id="conformance-classes">Conformance Classes</h3>
1009 <p>Conformance to the CSS Conditional Rules Module is defined for three
1010 conformance classes:
1011 <dl>
1012 <dt><dfn title="conformance::style sheet" id="conform-style-sheet">style sheet</dfn>
1013 <dd>A <a href="http://www.w3.org/TR/CSS21/conform.html#style-sheet">CSS
1014 style sheet</a>.</dd>
1015 <dt><dfn title="conformance::processor" id="conform-processor">processor</dfn></dt>
1016 <dd>A tool that reads CSS style sheets: it may be a renderer or
1017 <a
1018 href="http://www.w3.org/TR/CSS21/conform.html#user-agent">user-agent</a>
1019 that interprets the semantics of a style sheet and renders
1020 documents that use style sheets, or it may be a validator that
1021 checks style sheets.</dd>
1022 <dt><dfn title="conformance::authoring tool" id="conform-authoring-tool">authoring tool</dfn></dt>
1023 <dd>A tool that writes a style sheet.</dd>
1024 </dl>
1026 <p>A style sheet is conformant to the CSS Conditional Rules Module
1027 if it meets all of the conformance requirements in the module that are
1028 described as requirements of style sheets.</p>
1030 <p>A processor is conformant to the CSS Conditional Rules Module if it
1031 meets all applicable conformance requirements in the module that are
1032 described as requirements of processors. In general, all requirements
1033 are applicable to renderers. Requirements concerning a part of CSS
1034 not performed by a processor are not applicable, e.g., requirements
1035 related to rendering are not applicable to a validator. The inability
1036 of a processor to correctly render a document due to limitations of
1037 the device does not make it non-conformant. (For example, a renderer
1038 is not required to render color on a monochrome monitor.)</p>
1040 <p>An authoring tool is conformant to the CSS Conditional Rules Module
1041 if it writes style sheets that conform to the module and (if it reads
1042 CSS) it is a conformant processor.</p>
1044 <h3 id="partial">
1045 Partial Implementations</h3>
1047 <p>So that authors can exploit the forward-compatible parsing rules to
1048 assign fallback values, CSS renderers <strong>must</strong>
1049 treat as invalid (and <a href="http://www.w3.org/TR/CSS21/conform.html#ignore">ignore
1050 as appropriate</a>) any at-rules, properties, property values, keywords,
1051 and other syntactic constructs for which they have no usable level of
1052 support. In particular, user agents <strong>must not</strong> selectively
1053 ignore unsupported component values and honor supported values in a single
1054 multi-value property declaration: if any value is considered invalid
1055 (as unsupported values must be), CSS requires that the entire declaration
1056 be ignored.</p>
1058 <h3 id="experimental">Experimental Implementations</h3>
1060 <p>To avoid clashes with future CSS features, the CSS specifications
1061 reserve a <a href="http://www.w3.org/TR/CSS21/syndata.html#vendor-keywords">prefixed
1062 syntax</a> for proprietary property and value extensions to CSS. The CSS
1063 Working Group recommends that experimental implementations of features in
1064 CSS Working Drafts also use vendor-prefixed property or value names. This
1065 avoids any incompatibilities with future changes in the draft. Once a
1066 specification reaches the Candidate Recommendation stage, implementors
1067 should implement the non-prefixed syntax for any feature they consider to
1068 be correctly implemented according to spec.</p>
1070 <h3 id="cr-exit-criteria">CR Exit Criteria</h3>
1072 <p>For this specification to be advanced to Proposed Recommendation,
1073 there must be at least two independent, interoperable implementations
1074 of each feature. Each feature may be implemented by a different set of
1075 products, there is no requirement that all features be implemented by
1076 a single product. For the purposes of this criterion, we define the
1077 following terms:
1079 <dl>
1080 <dt>independent <dd>each implementation must be developed by a
1081 different party and cannot share, reuse, or derive from code
1082 used by another qualifying implementation. Sections of code that
1083 have no bearing on the implementation of this specification are
1084 exempt from this requirement.
1086 <dt>interoperable <dd>passing the respective test case(s) in the
1087 official CSS test suite, or, if the implementation is not a Web
1088 browser, an equivalent test. Every relevant test in the test
1089 suite should have an equivalent test created if such a user
1090 agent (UA) is to be used to claim interoperability. In addition
1091 if such a UA is to be used to claim interoperability, then there
1092 must one or more additional UAs which can also pass those
1093 equivalent tests in the same way for the purpose of
1094 interoperability. The equivalent tests must be made publicly
1095 available for the purposes of peer review.
1097 <dt>implementation <dd>a user agent which:
1099 <ol class=inline>
1100 <li>implements the specification.
1102 <li>is available to the general public. The implementation may
1103 be a shipping product or other publicly available version
1104 (i.e., beta version, preview release, or “nightly build”).
1105 Non-shipping product releases must have implemented the
1106 feature(s) for a period of at least one month in order to
1107 demonstrate stability.
1109 <li>is not experimental (i.e., a version specifically designed
1110 to pass the test suite and is not intended for normal usage
1111 going forward).
1112 </ol>
1113 </dl>
1115 <p>The specification will remain Candidate Recommendation for at least
1116 six months.
1118 <h2 id="changes">
1119 Changes</h2>
1121 <p>The following (non-editorial) changes were made to this specification since the
1122 <a href="http://www.w3.org/TR/2012/WD-css3-conditional-20120911/">11 September 2012 Working Draft</a>:
1124 <ul>
1125 <li>Corrected the grammar as follows:
1126 <pre>
1127 - : SUPPORTS_SYM S+ supports_condition group_rule_body
1128 + : SUPPORTS_SYM S* supports_condition group_rule_body
1129 </pre>
1130 </ul>
1132 <h2 class=no-num id="acknowledgments">Acknowledgments</h2>
1134 <p>
1135 Thanks to the ideas and feedback from
1136 Tab Atkins,
1137 <span lang="tr">Tantek Çelik</span>,
1138 Alex Danilo,
1139 Elika Etemad,
1140 Pascal Germroth,
1141 <span lang="de">Björn Höhrmann</span>,
1142 Paul Irish,
1143 Vitor Menezes,
1144 Alex Mogilevsky,
1145 Chris Moschini,
1146 Simon Sapin,
1147 Ben Ward,
1148 Zack Weinberg,
1149 Estelle Weyl,
1150 Boris Zbarsky,
1151 and all the rest of the <a href="http://lists.w3.org/Archives/Public/www-style/">www-style</a> community.
1153 </p>
1155 <h2 class=no-num id="references">References</h2>
1158 <h3 class="no-num" id="normative-references">Normative references</h3>
1159 <!--normative-->
1161 <h3 class="no-num" id="other-references">Other references</h3>
1162 <!--informative-->
1164 <h2 class="no-num" id="index">Index</h2>
1165 <!--index-->
1167 </body>
1168 </html>
1169 <!-- Keep this comment at the end of the file
1170 Local variables:
1171 mode: sgml
1172 sgml-declaration:"~/SGML/HTML4.decl"
1173 sgml-default-doctype-name:"html"
1174 sgml-minimize-attributes:t
1175 sgml-nofill-elements:("pre" "style" "br")
1176 sgml-live-element-indicator:t
1177 sgml-omittag:nil
1178 sgml-shorttag:nil
1179 sgml-namecase-general:t
1180 sgml-general-insert-case:lower
1181 sgml-always-quote-attributes:t
1182 sgml-indent-step:nil
1183 sgml-indent-data:t
1184 sgml-parent-document:nil
1185 sgml-exposed-tags:nil
1186 sgml-local-catalogs:nil
1187 sgml-local-ecat-files:nil
1188 End:
1189 -->