Skip to content

Commit 1d4c6ed

Browse files
committed
[css-values] Define how to serialize calc() values.
1 parent 6c52b09 commit 1d4c6ed

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

css-values/Overview.bs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,52 @@ Range Checking</h4>
13011301
</pre>
13021302
</div>
13031303

1304+
<h4 id='calc-serialize'>
1305+
Serialization</h4>
1306+
1307+
To serialize a ''calc()'' value:
1308+
1309+
1. Simplify the expression by
1310+
resolving all multiplications and divisions,
1311+
and combining compatible units.
1312+
(Which units are compatible can vary based on what stage of the value computation pipeline it's in.
1313+
For example, as a specified value, ''px'' and ''em'' are incompatible;
1314+
as a computed value, they're compatible.)
1315+
1316+
The result must be a summation of unique incompatible units.
1317+
(Terms with a value of zero <strong>must</strong> be preserved in this summation.)
1318+
1319+
2. If the result is a single value,
1320+
serialize as that value,
1321+
discarding the ''calc()'' wrapper.
1322+
1323+
3. Otherwise, serialize as a ''calc()'' containing the summation,
1324+
with the units ordered <a>ASCII case-insensitive</a> alphabetically,
1325+
numbers coming before all units,
1326+
and percentages coming after all units.
1327+
1328+
<div class="example">
1329+
For example, ''calc(20px + 30px)'' would first simplify to a single ''px'' value,
1330+
then serialize as just ''50px''.
1331+
1332+
A value like ''calc(20px + 0%)'' would serialize as ''calc(20px + 0%)'',
1333+
maintaining both terms in the serialized value.
1334+
(It's important to maintain zero-valued terms,
1335+
so the ''calc()'' doesn't suddenly "change shape" in the middle of a transition
1336+
when one of the values happens to have a zero value temporarily.
1337+
This also removes the need to "pick a unit" when all the terms are zero.)
1338+
1339+
A value like ''calc(20px + 2em)'' would serialize as ''calc(2em + 20px)'' as a specified value
1340+
(maintaining both units as they're incompatible at specified-value time,
1341+
but sorting them alphabetically),
1342+
or as something like ''52px'' as a computed value
1343+
(''em'' values are converted to absolute lengths at computed-value time,
1344+
so assuming ''1em'' = ''16px'',
1345+
they combine into ''52px'',
1346+
which then drops the ''calc()'' wrapper.)
1347+
</div>
1348+
1349+
13041350
<h3 id="toggle-notation">
13051351
Toggling Between Values: ''toggle()''</h3>
13061352

css-values/Overview.html

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
<h1 class="p-name no-ref" id="title">CSS Values and Units Module Level 3</h1>
9393

9494
<h2 class="no-num no-toc no-ref heading settled" id="subtitle"><span class="content">Editor’s Draft,
95-
<time class="dt-updated" datetime="2015-06-08">8 June 2015</time></span></h2>
95+
<time class="dt-updated" datetime="2015-06-25">25 June 2015</time></span></h2>
9696

9797
<div data-fill-with="spec-metadata">
9898
<dl>
@@ -454,6 +454,8 @@ <h2 class="no-num no-toc no-ref heading settled" id="contents"><span class="cont
454454
Computed Value</span></a>
455455
<li><a href="#calc-range"><span class="secno">8.1.4</span> <span class="content">
456456
Range Checking</span></a>
457+
<li><a href="#calc-serialize"><span class="secno">8.1.5</span> <span class="content">
458+
Serialization</span></a>
457459
</ul>
458460
<li><a href="#toggle-notation"><span class="secno">8.2</span> <span class="content">
459461
Toggling Between Values: <span class="css">toggle()</span></span></a>
@@ -2687,6 +2689,72 @@ <h4 class="heading settled" data-level="8.1.4" id="calc-range"><span class="secn
26872689
</div>
26882690

26892691

2692+
<h4 class="heading settled" data-level="8.1.5" id="calc-serialize"><span class="secno">8.1.5. </span><span class="content">
2693+
Serialization</span><a class="self-link" href="#calc-serialize"></a></h4>
2694+
2695+
2696+
<p>To serialize a <a class="css" data-link-type="maybe" href="#funcdef-calc">calc()</a> value:</p>
2697+
2698+
2699+
<ol>
2700+
<li data-md="">
2701+
<p>Simplify the expression by
2702+
resolving all multiplications and divisions,
2703+
and combining compatible units.
2704+
(Which units are compatible can vary based on what stage of the value computation pipeline it’s in.
2705+
For example, as a specified value, <span class="css">px</span> and <span class="css">em</span> are incompatible;
2706+
as a computed value, they’re compatible.)</p>
2707+
2708+
2709+
2710+
<p>The result must be a summation of unique incompatible units.
2711+
(Terms with a value of zero <strong>must</strong> be preserved in this summation.)</p>
2712+
2713+
2714+
2715+
<li data-md="">
2716+
<p>If the result is a single value,
2717+
serialize as that value,
2718+
discarding the <a class="css" data-link-type="maybe" href="#funcdef-calc">calc()</a> wrapper.</p>
2719+
2720+
2721+
2722+
<li data-md="">
2723+
<p>Otherwise, serialize as a <a class="css" data-link-type="maybe" href="#funcdef-calc">calc()</a> containing the summation,
2724+
with the units ordered <a data-link-type="dfn" href="http://dom.spec.whatwg.org/#ascii-case_insensitive">ASCII case-insensitive</a> alphabetically,
2725+
numbers coming before all units,
2726+
and percentages coming after all units.</p>
2727+
2728+
</ol>
2729+
2730+
<div class="example" id="example-65075dff"><a class="self-link" href="#example-65075dff"></a>
2731+
For example, <span class="css">calc(20px + 30px)</span> would first simplify to a single <span class="css">px</span> value,
2732+
then serialize as just <span class="css">50px</span>.
2733+
2734+
2735+
<p>A value like <span class="css">calc(20px + 0%)</span> would serialize as <span class="css">calc(20px + 0%)</span>,
2736+
maintaining both terms in the serialized value.
2737+
(It’s important to maintain zero-valued terms,
2738+
so the <a class="css" data-link-type="maybe" href="#funcdef-calc">calc()</a> doesn’t suddenly "change shape" in the middle of a transition
2739+
when one of the values happens to have a zero value temporarily.
2740+
This also removes the need to "pick a unit" when all the terms are zero.)</p>
2741+
2742+
2743+
2744+
<p>A value like <span class="css">calc(20px + 2em)</span> would serialize as <span class="css">calc(2em + 20px)</span> as a specified value
2745+
(maintaining both units as they’re incompatible at specified-value time,
2746+
but sorting them alphabetically),
2747+
or as something like <span class="css">52px</span> as a computed value
2748+
(<span class="css">em</span> values are converted to absolute lengths at computed-value time,
2749+
so assuming <span class="css">1em</span> = <span class="css">16px</span>,
2750+
they combine into <span class="css">52px</span>,
2751+
which then drops the <a class="css" data-link-type="maybe" href="#funcdef-calc">calc()</a> wrapper.)</p>
2752+
2753+
2754+
</div>
2755+
2756+
2757+
26902758
<h3 class="heading settled" data-level="8.2" id="toggle-notation"><span class="secno">8.2. </span><span class="content">
26912759
Toggling Between Values: <a class="css" data-link-type="maybe" href="#funcdef-toggle">toggle()</a></span><a class="self-link" href="#toggle-notation"></a></h3>
26922760

0 commit comments

Comments
 (0)