Skip to content

Commit db25c97

Browse files
committed
Write CSSNumericValue.toSum(). Fixes w3c#391.
1 parent 498a2e6 commit db25c97

File tree

1 file changed

+68
-2
lines changed

1 file changed

+68
-2
lines changed

css-typed-om/Overview.bs

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ spec:css-transforms-1;
7777
spec:css-syntax-3;
7878
type:dfn; text:identifier
7979
spec:infra;
80-
type: dfn; text: list
80+
type: dfn;
81+
text: list
82+
text: code point
8183
</pre>
8284

8385
Introduction {#intro}
@@ -598,6 +600,7 @@ are represented by subclasses of the {{CSSNumericValue}} interface.
598600
boolean equals(CSSNumberish... value);
599601

600602
CSSNumericValue to(DOMString unit);
603+
CSSMathSum toSum(DOMString... units);
601604
// ??? type();
602605

603606
static CSSNumericValue parse(DOMString cssText);
@@ -743,7 +746,7 @@ The following are the arithmetic operations you can perform on dimensions:
743746
[=throw=] a {{TypeError}}.
744747
Otherwise, let |item| be the result of [=create a CSSUnitValue from a sum value item|creating a CSSUnitValue=]
745748
from the sole [=list/item=] in |sum|,
746-
then [=converting a CSSUnitValue|converting=] it to |unit|.
749+
then [=convert a CSSUnitValue|converting=] it to |unit|.
747750
If |item| is failure,
748751
[=throw=] a {{TypeError}}.
749752

@@ -775,6 +778,69 @@ The following are the arithmetic operations you can perform on dimensions:
775778
is set to |item|’s [=sum value/value=].
776779
</div>
777780

781+
<div algorithm="CSSNumericValue.toSum()">
782+
The <dfn method for=CSSNumericValue>toSum(...|units|)</dfn> method converts an existing {{CSSNumericValue}} |this|
783+
into a {{CSSMathSum}} of only {{CSSUnitValue}}s with the specified units,
784+
if possible.
785+
(It's like {{CSSNumericValue/to()}},
786+
but allows the result to have multiple units in it.)
787+
If called without any units,
788+
it just simplifies |this| into a minimal sum of {{CSSUnitValue}}s.
789+
790+
When called, it must perform the following steps:
791+
792+
1. [=list/For each=] |unit| in |units|,
793+
if the result of [=creating a type=] from |unit| is failure,
794+
[=throw=] a {{SyntaxError}}.
795+
796+
2. Let |sum| be the result of [=creating a sum value=] from |this|.
797+
If |sum| is failure,
798+
[=throw=] a {{TypeError}}.
799+
800+
3. Let |values| be the result of [=create a CSSUnitValue from a sum value item|creating a CSSUnitValue=]
801+
[=list/for each=] [=list/item=] in |sum|.
802+
If any [=list/item=] of |values| is failure,
803+
[=throw=] a {{TypeError}}.
804+
805+
4. If |units| is [=list/empty=],
806+
sort |values| in [=code point=] order according to the {{CSSUnitValue/unit}} internal slot of its [=list/items=],
807+
then return a new {{CSSMathSum}} object
808+
whose {{CSSMathSum/values}} internal slot is set to |values|.
809+
810+
811+
5. Otherwise,
812+
let |result| initially be an empty [=list=].
813+
[=list/For each=] |unit| in |units|:
814+
815+
1. Let |temp| initially be a new {{CSSUnitValue}}
816+
whose {{CSSUnitValue/unit}} internal slot
817+
is set to |unit|
818+
and whose {{CSSUnitValue/value}} internal slot
819+
is set to `0`.
820+
821+
2. [=list/For each=] |value| in |values|:
822+
823+
1. Let |value unit| be |value|’s {{CSSUnitValue/unit}} internal slot.
824+
825+
2. If |value unit| is a [=compatible unit=] with |unit|,
826+
then:
827+
828+
1.[=convert a CSSUnitValue|convert=] |value| to |unit|.
829+
2. Increment |temp|’s {{CSSUnitValue/value}} internal slot
830+
by the value of |value|’s {{CSSUnitValue/value}} internal slot.
831+
3. [=list/Remove=] |value| from |values|.
832+
833+
3. [=list/Append=] |temp| to |result|.
834+
835+
6. If |values| is not [=list/empty=],
836+
[=throw=] a {{TypeError}}.
837+
<span class=note>|this| had units that you didn't ask for.</span>
838+
839+
7. Return a new {{CSSMathSum}} object
840+
whose {{CSSMathSum/values}} internal slot
841+
is set to |result|.
842+
</div>
843+
778844
<div algorithm="sum value">
779845
A <dfn>sum value</dfn>
780846
is an abstract representation of a {{CSSNumericValue}}

0 commit comments

Comments
 (0)