Skip to content

Commit 27c2b0a

Browse files
committed
[css-variables] Rewrite the 'using variables' section to only have var().
1 parent ca100a2 commit 27c2b0a

2 files changed

Lines changed: 184 additions & 293 deletions

File tree

css-variables/Overview.html

Lines changed: 105 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222

2323
<h1>CSS Variables Module Level 1</h1>
2424

25-
<h2 class="no-num no-toc" id=longstatus-date>Editor's Draft 23 August 2012</h2>
25+
<h2 class="no-num no-toc" id=longstatus-date>Editor's Draft 24 August 2012</h2>
2626

2727
<dl>
2828
<dt>This version:
29-
<!--<dd><a href="http://www.w3.org/TR/2012/WD-css-variables-20120823/">http://www.w3.org/TR/2012/ED-css-variables-20120823/</a>-->
29+
<!--<dd><a href="http://www.w3.org/TR/2012/WD-css-variables-20120824/">http://www.w3.org/TR/2012/ED-css-variables-20120824/</a>-->
3030

3131

3232
<dd><a href="http://dev.w3.org/csswg/css-variables/">Editor's draft</a>
33-
(version of 23 August 2012)
33+
(version of 24 August 2012)
3434

3535
<dt>Latest version:
3636

@@ -143,23 +143,11 @@ <h2 class="no-num no-toc" id=contents>Table of contents</h2>
143143
<li><a href="#using-variables"><span class=secno>3. </span> Using
144144
Variables - the ‘<code class=css>var()</code>’ notation</a>
145145
<ul class=toc>
146-
<li><a href="#parent-var-notation"><span class=secno>3.1. </span>
147-
Getting the Parent's Variable - the ‘<code
148-
class=css>parent-var()</code>’ notation</a>
149-
150-
<li><a href="#using-invalid-variables"><span class=secno>3.2. </span>
146+
<li><a href="#using-invalid-variables"><span class=secno>3.1. </span>
151147
Using Invalid Variables</a>
152148
</ul>
153149

154150
<li><a href="#cssom"><span class=secno>4. </span> CSSOM</a>
155-
<ul class=toc>
156-
<li><a href="#cssom-cssvariable"><span class=secno>4.1. </span>
157-
Interface CSSVariableComponentValue</a>
158-
159-
<li><a href="#cssstyledeclaration-interface"><span class=secno>4.2.
160-
</span> Additions to the <code>CSSStyleDeclaration</code> Interface</a>
161-
162-
</ul>
163151

164152
<li><a href="#conformance"><span class=secno>5. </span> Conformance</a>
165153
<ul class=toc>
@@ -306,6 +294,15 @@ <h2 id=defining-variables><span class=secno>2. </span> Defining Custom
306294
brackets, or any character that would normally end a declaration (a
307295
top-level semicolon or close brace).
308296

297+
<p class=issue> This should be defined as "any sequence of tokens that can
298+
be parsed, starting from the ‘<code class=css>declarative-value
299+
mode</code>’ in Syntax, without switching out of that mode".
300+
301+
<p class=note> Note: Custom properties can contain a trailing ‘<code
302+
class=css>!important</code>’, but this is automatically removed from the
303+
property's value by the CSS parser, and makes the custom property
304+
"important" in the CSS cascade.
305+
309306
<p> There are an infinity of <a href="#custom-property"><i>custom
310307
properties</i></a>, but the initial value of a <a
311308
href="#custom-property"><i>custom property</i></a> is an empty invalid
@@ -488,10 +485,14 @@ <h2 id=defining-variables><span class=secno>2. </span> Defining Custom
488485
<h2 id=using-variables><span class=secno>3. </span> Using Variables - the
489486
<code class=css>var()</code>’ notation</h2>
490487

491-
<p> A <a href="#custom-property"><i>custom property</i></a> automatically
492-
defines The value of a <a href="#custom-property"><i>custom
493-
property</i></a> on an element can be substituted into another property
494-
using the ‘<code class=css>var()</code>’ function.
488+
<p> Every <a href="#custom-property"><i>custom property</i></a>
489+
automatically defines a corresponding <a
490+
href="#variable"><i>variable</i></a>, which can then be substituted into
491+
another property with the ‘<code class=css>var()</code>’ function. The
492+
syntax of ‘<code class=css>var()</code>’ is:
493+
494+
<pre><dfn
495+
id=default-var-type>&lt;variable></dfn> = var( <var>variable-name</var> [, <var>&lt;value></var> ]? )</pre>
495496

496497
<p> A variable can be used in place of any component value in any property
497498
on an element. Variables can not be used as property names, selectors, or
@@ -500,6 +501,54 @@ <h2 id=using-variables><span class=secno>3. </span> Using Variables - the
500501
attribute selector, produces a valid value that nonetheless has no
501502
relation to the variable of that name.
502503

504+
<div class=example>
505+
<p> For example, the following code incorrectly attempts to use a variable
506+
as a property name:
507+
508+
<pre><!--
509+
-->.foo {
510+
<!--
511+
--> var-side: margin-top;
512+
<!--
513+
--> var(side): 20px;
514+
<!--
515+
-->}</pre>
516+
517+
<p> This is <em>not</em> equivalent to setting ‘<code
518+
class=css>margin-top: 20px;</code>’. Instead, the second declaration is
519+
simply thrown away as a syntax error for having an invalid property name.
520+
521+
522+
<p> Similarly, you can't build up a single component value where part of
523+
it is provided by a variable:
524+
525+
<pre><!--
526+
-->.foo {
527+
<!--
528+
--> var-gap: 20;
529+
<!--
530+
--> margin-top: var(gap)px;
531+
<!--
532+
-->}</pre>
533+
534+
<p> Again, this is <em>not</em> equivalent to setting ‘<code
535+
class=css>margin-top: 20px;</code>’ (a length). Instead, it's
536+
equivalent to ‘<code class=css>margin-top: 20 px;</code>’ (a number
537+
followed by an ident), which is simply an invalid value for the ‘<code
538+
class=property>margin-top</code>’ property. Note, though, that ‘<code
539+
class=css>calc()</code>’ can be used to validly achieve the same thing,
540+
like so:
541+
542+
<pre><!--
543+
-->.foo {
544+
<!--
545+
--> var-gap: 20;
546+
<!--
547+
--> margin-top: calc(var(gap) * 1px);
548+
<!--
549+
-->}</pre>
550+
</div>
551+
503552
<p> A variable is substituted for its value in the property value at
504553
computed-value time. If a declaration, once all variables are substituted
505554
in, is invalid, the declaration is <a
@@ -510,15 +559,20 @@ <h2 id=using-variables><span class=secno>3. </span> Using Variables - the
510559
<p> For example, the following usage is fine from a syntax standpoint, but
511560
results in nonsense when the variable is substituted in:
512561

513-
<pre>
514-
:root { var-looks-valid: 20px; }
515-
p { background-color: $looks-valid; }</pre>
562+
<pre><!--
563+
-->:root { var-looks-valid: 20px; }
564+
<!--
565+
-->p { background-color: $looks-valid; }</pre>
516566

517567
<p> Since ‘<code class=css>20px</code>’ is an invalid value for
518568
<code class=property>background-color</code>’, this instance of the
519569
property computes to ‘<code class=property>transparent</code>’ (the
520570
initial value for ‘<code class=property>background-color</code>’)
521571
instead.
572+
573+
<p> If the property was one that's inherited by default, such as ‘<code
574+
class=property>color</code>’, it would compute to the inherited value
575+
rather than the initial value.
522576
</div>
523577

524578
<p> In some cases, it can be useful to provide a "default" value for a
@@ -531,95 +585,24 @@ <h2 id=using-variables><span class=secno>3. </span> Using Variables - the
531585
about, rather than being forced to provide values for all of them.
532586

533587
<p> The ‘<code class=css>var()</code>’ notation allows a default value
534-
to be specified when referencing a variable. The syntax of ‘<code
535-
class=css>var()</code>’ is:
588+
to be specified when referencing a variable. After the variable name,
589+
insert a comma and then provide any value, just like the value of a <a
590+
href="#custom-property"><i>custom property</i></a>.
536591

537-
<pre><dfn
538-
id=default-var-type>&lt;default-var></dfn> = var( <var>&lt;var></var> [, <var>&lt;value></var> ]? )</pre>
592+
<p class=issue> The syntax for custom properties and var fallbacks are
593+
actually slightly different - you can't use an unbalanced ) in the
594+
fallback, since it ends a function, but you *can* use a semicolon or an
595+
unbalanced }. This should be defined as "any sequence of tokens that can
596+
be parsed by the ‘<code class=css>consume a function</code>’ algo in
597+
Syntax without returning the function".
539598

540599
<p> If the variable named by the first argument is valid, the variable's
541600
value is substituted as normal. If it's invalid, and a second argument was
542601
provided, the value of the second argument is substituted instead.
543602
Otherwise, the ‘<code class=css>var()</code>’ represents an <a
544603
href="#invalid-variable"><i>invalid variable</i></a>.
545604

546-
<p class=note> Note that using ‘<code class=css>var()</code>’ without a
547-
second argument offers no benefits over using the variable by itself. This
548-
is allowed for consistency with ‘<code class=css>parent-var()</code>’,
549-
which <em>does</em> do something useful with only one argument. Future
550-
variable functions will likely be similar.
551-
552-
<h3 id=parent-var-notation><span class=secno>3.1. </span> Getting the
553-
Parent's Variable - the ‘<code class=css>parent-var()</code>’ notation</h3>
554-
555-
<p> Several advanced use-cases for variables need the ability to reference
556-
the value that a variable has on the element's parent, rather than on the
557-
element itself. This can be accomplished with the ‘<code
558-
class=css>parent-var()</code>’ notation:
559-
560-
<pre><dfn
561-
id=parent-var-notation-type>&lt;parent-var></dfn> = parent-var( <var>&lt;var></var> [, <var>&lt;value></var> ]? )</pre>
562-
563-
<p> If the element has a parent, and the variable named by the first
564-
argument is valid on the parent, the variable's value on the parent is
565-
substituted as normal. Otherwise, if a second argument was provided, the
566-
value of the second argument is substituted instead. Otherwise, the
567-
<code class=css>parent-var()</code>’ represents an <a
568-
href="#invalid-variable"><i>invalid variable</i></a>.
569-
570-
<div class=example>
571-
<p> For example, ‘<code class=css>parent-var()</code>’ can be used to
572-
implement a behavior that's very similar to the ‘<code
573-
class=css>toggle()</code>’ notation from [[CSS3VALUES]]. In this
574-
example, each nested comment alternates between a white and silver
575-
background.
576-
577-
<pre>
578-
&lt;div class='comment'>
579-
&lt;div class='main'>
580-
...
581-
&lt;/div>
582-
&lt;div class='actions'>&lt;a>Reply&lt;/a> &lt;a>Comment&lt;/a> ...&lt;/div>
583-
&lt;/div>
584-
&lt;div class='comment'>
585-
&lt;div class='main'>
586-
&lt;div class='comment'>
587-
&lt;div class='main'>
588-
...
589-
&lt;/div>
590-
&lt;div class='actions'>&lt;a>Reply&lt;/a> &lt;a>Comment&lt;/a> ...&lt;/div>
591-
&lt;/div>
592-
&lt;/div>
593-
&lt;div class='actions'>&lt;a>Reply&lt;/a> &lt;a>Comment&lt;/a> ...&lt;/div>
594-
&lt;/div>
595-
&lt;style>
596-
.comment {
597-
var-bg: parent-var($bg2, white);
598-
var-bg2: parent-var($bg, silver);
599-
background: url(comment.png) $bg;
600-
}
601-
&lt;/style></pre>
602-
603-
<p> The ‘<code class=css>toggle()</code>’ notation can't be used here,
604-
because ‘<code class=property>background-color</code>’ isn't
605-
inherited (and thus the nested comments see the initial value of ‘<code
606-
class=css>transparent</code>’ rather than ‘<code
607-
class=css>white</code>’ or ‘<code class=css>silver</code>’). Even
608-
if the author explicitly sets ‘<code
609-
class=css>background-color:inherit</code>’ on <code>&lt;div
610-
class='main'></code> so that ‘<code class=css>toggle()</code>’ can
611-
actually see the right value, that will make the ".main" elements cover
612-
up the ‘<code class=property>background-image</code>’ that ".comment"
613-
has on it as well. Because variables don't have any effect on rendering
614-
until they're used, they can pass these values through the document
615-
without these kinds of side effects.
616-
617-
<p> The ‘<code class=css>var()</code>’ notation can't be used here
618-
either as it would immediately produce a cycle between $bg and $bg2,
619-
making them both invalid.
620-
</div>
621-
622-
<h3 id=using-invalid-variables><span class=secno>3.2. </span> Using Invalid
605+
<h3 id=using-invalid-variables><span class=secno>3.1. </span> Using Invalid
623606
Variables</h3>
624607

625608
<p> When a <a href="#custom-property"><i>custom property</i></a> has its
@@ -642,10 +625,12 @@ <h3 id=using-invalid-variables><span class=secno>3.2. </span> Using Invalid
642625
<div class=example>
643626
<p> For example, in the following code:
644627

645-
<pre>
646-
:root { var-not-a-color: 20px; }
647-
p { background-color: red; }
648-
p { background-color: $not-a-color; }</pre>
628+
<pre><!--
629+
-->:root { var-not-a-color: 20px; }
630+
<!--
631+
-->p { background-color: red; }
632+
<!--
633+
-->p { background-color: $not-a-color; }</pre>
649634

650635
<p> the &lt;p> elements will have transparent backgrounds (the initial
651636
value for ‘<code class=property>background-color</code>’), rather
@@ -671,53 +656,13 @@ <h2 id=cssom><span class=secno>4. </span> CSSOM</h2>
671656
properties, and can be read or modified using all of the existing CSSOM
672657
APIs for reading or modifying properties.
673658

674-
<p> The specification extends the IDL definitions in the CSSOM spec to
675-
represent the use of the ‘<code class=css>var()</code>’ function.
676-
677-
<h3 id=cssom-cssvariable><span class=secno>4.1. </span> Interface
678-
CSSVariableComponentValue</h3>
679-
680-
<p> The CSSVariableComponentValue interface represents a use of the
681-
<code class=css>var()</code>’ function.
682-
683-
<dl>
684-
<dt>IDL Definition
685-
686-
<dd>
687-
<pre class=idl>
688-
[NoInterfaceObject] interface CSSVariableComponentValue {
689-
attribute DOMString variableName;
690-
readonly attribute any variableValue;
691-
}</pre>
692-
693-
<dt>Attributes
694-
695-
<dd>
696-
<dl>
697-
<dt><code>variableName</code> of type <code>DOMString</code>
698-
699-
<dd>This attribute represents the argument to the function. Changing
700-
this attribute changes the <a href="#custom-property"><i>custom
701-
property</i></a> being referred to.
702-
703-
<dt><code>variableValue</code> of type <code>any</code>, readonly
704-
705-
<dd>This attribute represents the value of the <a
706-
href="#custom-property"><i>custom property</i></a> the function is
707-
referencing.
708-
</dl>
709-
</dl>
710-
711-
<h3 id=cssstyledeclaration-interface><span class=secno>4.2. </span>
712-
Additions to the <code>CSSStyleDeclaration</code> Interface</h3>
713-
714-
<p class=issue> The set of custom properties is open-ended, so it's not
715-
clear how best to represent this. Ideally, the CSSOM would expose the
716-
current set of properties with a non-initial value and allow setting of
717-
arbitrary properties. The most natural way seems to be to first, set up a
718-
getter behavior on the interface somehow that deals with custom
719-
properties, and second, set up a <code>vars</code> map that exposes the
720-
custom properties that aren't set to their initial value.
659+
<p class=issue> Right now, the CSSStyleDeclaration interface can be specced
660+
and implemented as a normal interface that just has a *ton* of attributes.
661+
(In practice, we don't spec it this way, because it was difficult to
662+
extend before partial interfaces were created, but I think it's
663+
implemented this way.) However, variables mess this up - there's an
664+
infinite number of them. Ideally we'd only expose the valid ones (those
665+
set to a non-initial value). How to spec this?
721666

722667
<h2 id=conformance><span class=secno>5. </span> Conformance</h2>
723668

@@ -980,18 +925,12 @@ <h2 class=no-num id=index>Index</h2>
980925
<li>custom property, <a href="#custom-property"
981926
title="custom property"><strong>2.</strong></a>
982927

983-
<li>&lt;default-var>, <a href="#default-var-type"
984-
title="&lt;default-var>"><strong>3.</strong></a>
985-
986928
<li>invalid at computed-value time, <a
987929
href="#invalid-at-computed-value-time"
988-
title="invalid at computed-value time"><strong>3.2.</strong></a>
930+
title="invalid at computed-value time"><strong>3.1.</strong></a>
989931

990932
<li>invalid variable, <a href="#invalid-variable"
991-
title="invalid variable"><strong>3.2.</strong></a>
992-
993-
<li>&lt;parent-var>, <a href="#parent-var-notation-type"
994-
title="&lt;parent-var>"><strong>3.1.</strong></a>
933+
title="invalid variable"><strong>3.1.</strong></a>
995934

996935
<li>renderer, <a href="#renderer" title=renderer><strong>5.2.</strong></a>
997936

@@ -1005,6 +944,9 @@ <h2 class=no-num id=index>Index</h2>
1005944
<li>var-*, <a href="#var-" title="var-*"><strong>2.</strong></a>
1006945

1007946
<li>variable, <a href="#variable" title=variable><strong>2.</strong></a>
947+
948+
<li>&lt;variable>, <a href="#default-var-type"
949+
title="&lt;variable>"><strong>3.</strong></a>
1008950
</ul>
1009951
<!--end-index-->
1010952

0 commit comments

Comments
 (0)