You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To <dfn export lt="serialize a calculation tree|serialize the calculation tree|serializing a calculation tree|serializing the calculation tree">serialize a calculation tree</dfn>:
3614
+
3615
+
1. Let |root| be the root node
3616
+
of the [=calculation tree=].
3617
+
3618
+
2. If |root| is a numeric value,
3619
+
or a non-[=math function=],
3620
+
serialize |root| per the normal rules for it
3621
+
and return the result.
3622
+
3623
+
3. If |root| is anything but
3624
+
a Sum,
3625
+
Negate,
3626
+
Product,
3627
+
or Invert node,
3628
+
[=serialize a math function=]
3629
+
for the function corresponding to the node type,
3630
+
treating the node's children as the function's comma-separated [=calculation=] arguments,
3631
+
and return the result.
3632
+
3633
+
4. If |root| is a Negate node,
3634
+
let |s| be a [=string=]
3635
+
initially containing "(-1 * ".
3636
+
3637
+
[=serialize a calculation tree|Serialize=] |root|'s child,
3638
+
and append it to |s|.
3639
+
3640
+
Append ")" to |s|,
3641
+
then return it.
3608
3642
3609
-
<div algorithm="serialize a min()/max() value">
3610
-
To <dfn export lt="serialize a non-calc() math function">serialize a non-''calc()'' [=math function=]</dfn>:
3643
+
5. If |root| is an Invert node,
3644
+
let |s| be a [=string=]
3645
+
initially containing "(1 / ".
3611
3646
3612
-
1. For each comma-separated [=calculation=] inside of the [=math function=],
3613
-
[=simplify the calculation=].
3647
+
[=serialize a calculation tree|Serialize=] |root|'s child,
3648
+
and append it to |s|.
3614
3649
3615
-
2. Let |s| initially be the name of the [=math function=],
3616
-
followed by "(".
3617
-
(E.g. "min(", "sin(", etc.)
3650
+
Append ")" to |s|,
3651
+
then return it.
3618
3652
3619
-
3. [=serialize the summation|Serialize each summation=],
3620
-
then join them into a single string,
3621
-
with ", " between each term.
3622
-
Append the result to |s|.
3653
+
6. If |root| is a Sum node,
3654
+
let |s| be a [=string=]
3655
+
initially containing "(".
3623
3656
3624
-
4. Append ")" to |s|,
3625
-
then return it.
3626
-
</div>
3657
+
[=sort a calculation's children|Sort root's children=].
3658
+
3659
+
[=serialize a calculation tree|Serialize=] |root|'s first child,
3660
+
and append it to |s|.
3661
+
3662
+
[=list/For each=] |child| of |root| beyond the first:
3663
+
3664
+
1. If |child| is a Negate node,
3665
+
append " - " to |s|,
3666
+
then [=serialize a calculation tree|serialize=] the Negate's child
3667
+
and append the result to |s|.
3668
+
3669
+
3. If |child| is a negative numeric value,
3670
+
append " - " to |s|,
3671
+
then serialize the negation of |child| as normal
3672
+
and append the result to |s|.
3673
+
3674
+
2. Otherwise,
3675
+
append " + " to |s|,
3676
+
then [=serialize a calculation tree|serialize=] |child|
3677
+
and append the result to |s|.
3678
+
3679
+
Finally, append ")" to |s|
3680
+
and return it.
3681
+
3682
+
7. If |root| is a Product node,
3683
+
let |s| be a [=string=]
3684
+
initially containing "(".
3627
3685
3628
-
<div algorithm="simplify a calculation">
3629
-
To <dfn export lt="simplify a calculation | simplify the calculation" for="math function">simplify a calculation</dfn>:
3686
+
[=sort a calculation's children|Sort root's children=].
3630
3687
3631
-
1. Replace any ''calc()'' values with parentheses containing their contents.
3632
-
2. Resolve all multiplications and divisions.
3633
-
3. Combine identical units.
3634
-
4. Recurse into remaining [=math functions=].
3635
-
5. Return the result.
3688
+
[=serialize a calculation tree|Serialize=] |root|'s first child,
3689
+
and append it to |s|.
3636
3690
3637
-
Note: The value-computation process can transform disparate units into identical ones.
3638
-
For example, ''em'' and ''px'' are obviously different at specified-value time,
3639
-
but at computed-value time they're both absolutized to ''px''.
3691
+
[=list/For each=] |child| of |root| beyond the first:
3640
3692
3641
-
The result must be a summation of unique units and/or [=math functions=].
3642
-
(Terms with a value of zero <strong>must</strong> be preserved in this summation.)
3693
+
1. If |child| is an Invert node,
3694
+
append " / " to |s|,
3695
+
then [=serialize a calculation tree|serialize=] the Invert's child
3696
+
and append the result to |s|.
3697
+
3698
+
2. Otherwise,
3699
+
append " * " to |s|,
3700
+
then [=serialize a calculation tree|serialize=] |child|
3701
+
and append the result to |s|.
3702
+
3703
+
Finally, append ")" to |s|
3704
+
and return it.
3643
3705
</div>
3644
3706
3645
-
<wpt>
3646
-
css/css-values/calc-nesting-002.html
3647
-
</wpt>
3707
+
<div algorithm>
3708
+
To <dfn>sort a calculation's children</dfn> |nodes|:
3648
3709
3649
-
<div algorithm="serialize a summation">
3650
-
To <dfn export lt="serialize a summation|serialize the summation" for="math function">serialize a summation</dfn>:
3710
+
1. Let |ret| be an empty list.
3651
3711
3652
-
1. Sort the terms in the following order:
3712
+
2. If |nodes| contains a number,
3713
+
remove it from |nodes| and append it to |ret|.
3653
3714
3654
-
1. The number, if present
3655
-
2. The percentage, if present
3656
-
3. The dimensions, ordered by their units <a>ASCII case-insensitive</a> alphabetically
0 commit comments