Skip to content

Commit 34a6dff

Browse files
committed
Rewrite to() with a convenience function.
1 parent ffd3abb commit 34a6dff

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

css-typed-om/Overview.bs

+41-19
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,8 @@ The following are the arithmetic operations you can perform on dimensions:
727727

728728
<div algorithm="CSSNumericValue.to()">
729729
The <dfn method for=CSSNumericValue>to(|unit|)</dfn> method converts an existing {{CSSNumericValue}} |this|
730-
into another one with the specified |unit|.
730+
into another one with the specified |unit|,
731+
if possible.
731732
When called, it must perform the following steps:
732733

733734
1. Let |type| be the result of [=creating a type=] from |unit|.
@@ -740,24 +741,46 @@ The following are the arithmetic operations you can perform on dimensions:
740741

741742
3. If |sum| has more than one [=list/item=],
742743
[=throw=] a {{TypeError}}.
743-
Otherwise, let |item| be the sole [=list/item=] in |sum|.
744-
745-
4. If |item| has more than one [=map/value=] in its [=sum value/unit map=],
746-
or that single [=map/value=]’s value is anything other than `1`,
744+
Otherwise, let |item| be the result of [=create a CSSUnitValue from a sum value item|creating a CSSUnitValue=]
745+
from the sole [=list/item=] in |sum|.
746+
If |item| is failure,
747747
[=throw=] a {{TypeError}}.
748-
Otherwise, let |item unit| be the sole [=map/key=] in the [=sum value/unit map=],
749-
and |item value| be |item|’s [=sum value/value=].
750748

751-
5. If |unit| and |item unit| are not [=compatible units=],
749+
4. If |unit| and |item|’s {{CSSUnitValue/unit}} internal slot are not [=compatible units=],
752750
[=throw=] a {{TypeError}}.
753751

754-
6. Return a new {{CSSUnitValue}}
752+
5. If |unit| is not equal to |item|’s {{CSSUnitValue/unit}} internal slot,
753+
multiply |item|’s {{CSSUnitValue/value}} internal slot
754+
by the conversion ratio between the two units,
755+
and set its {{CSSUnitValue/unit}} internal slot
756+
to |unit|.
757+
758+
6. Return |item|.
759+
</div>
760+
761+
<div algorithm>
762+
When asked to <dfn export>create a CSSUnitValue from a sum value item</dfn> |item|,
763+
perform the following steps:
764+
765+
1. If |item| has more than one [=map/entry=] in its [=sum value/unit map=],
766+
return failure.
767+
768+
2. If |item| has no [=map/entries=] in its [=sum value/unit map=],
769+
return a new {{CSSUnitValue}}
770+
whose {{CSSUnitValue/unit}} internal slot
771+
is set to "number",
772+
and whose {{CSSUnitValue/value}} internal slot
773+
is set to |item|’s [=sum value/value=].
774+
775+
3. Otherwise, |item| has a single [=map/entry=] in its [=sum value/unit map=].
776+
If that [=map/entry’s=] [=map/value=] is anything other than `1`,
777+
return failure.
778+
779+
4. Otherwise, return a new {{CSSUnitValue}}
755780
whose {{CSSUnitValue/unit}} internal slot
756-
is set to |unit|,
781+
is set to that [=map/entry’s=] [=map/key=],
757782
and whose {{CSSUnitValue/value}} internal slot
758-
is set to |item value|
759-
multiplied by the conversion ratio between |item unit|
760-
and |unit|.
783+
is set to |item|’s [=sum value/value=].
761784
</div>
762785

763786
<div algorithm="sum value">
@@ -1251,17 +1274,16 @@ are represented as {{CSSUnitValue}}s.
12511274
</div>
12521275

12531276
<div algorithm>
1254-
When asked to <dfn export local-lt="new unit value">create a new unit value</dfn>
1255-
from a numeric value |num|,
1277+
When asked to <dfn export>create a CSSUnitValue from a string</dfn> |str|,
12561278
return a new {{CSSUnitValue}} object
12571279
with its {{CSSUnitValue/value}} internal slot
1258-
set to the numeric portion of |num|,
1280+
set to the numeric portion of |str| parsed into a number,
12591281
and its {{CSSUnitValue/unit}} internal slot
1260-
set to the unit portion of |num|,
1261-
or "number" or "percent" if |num| is a plain number or percent.
1282+
set to the unit portion of |str|,
1283+
or "number" or "percent" if |str| is a plain number or percent.
12621284

12631285
<div class=example>
1264-
For example, creating a [=new unit value=] from ''5px''
1286+
For example, [=create a CSSUnitValue from a string|creating a CSSUnitValue=] from ''5px''
12651287
creates an object equivalent to
12661288
<code>new CSSUnitValue(5, "px")</code>.
12671289
</div>

0 commit comments

Comments
 (0)