-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathOverview.bs
3051 lines (2412 loc) · 125 KB
/
Overview.bs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<pre class='metadata'>
Title: CSS Typed OM Level 1
Status: ED
Group: houdini
ED: https://drafts.css-houdini.org/css-typed-om-1/
TR: https://www.w3.org/TR/css-typed-om-1/
Previous Version: https://www.w3.org/TR/2016/WD-css-typed-om-1-20160607/
Shortname: css-typed-om
Level: 1
Abstract: Converting CSSOM value strings into meaningfully typed JavaScript representations and back can incur a significant performance overhead. This specification exposes CSS values as typed JavaScript objects to facilitate their performant manipulation.
Editor: Shane Stephens, [email protected], w3cid 47691
Editor: Tab Atkins-Bittner, Google, http://xanthir.com/contact/, w3cid 42199
Editor: Naina Raisinghani, Google, [email protected], w3cid 100915
Repository: w3c/css-houdini-drafts
Inline Github Issues: title
Ignored Vars: type, unit
Ignored Terms: Drawable, paint image definition, list-valued property, list-valued property iteration, parse a CSSStyleValue, values to iterate over, CSS
Markup Shorthands: markdown yes
</pre>
<style>
/* Put nice boxes around each algorithm. */
[data-algorithm]:not(.heading) {
padding: .5em;
border: thin solid #ddd; border-radius: .5em;
margin: .5em calc(-0.5em - 1px);
}
[data-algorithm]:not(.heading) > :first-child {
margin-top: 0;
}
[data-algorithm]:not(.heading) > :last-child {
margin-bottom: 0;
}
[data-algorithm] [data-algorithm] {
margin: 1em 0;
}
</style>
<pre class=anchors>
urlPrefix: http://www.ecma-international.org/ecma-262/6.0/#sec-; type: dfn; spec: ecma-262
text: RangeError; url: native-error-types-used-in-this-standard-rangeerror
</pre>
<pre class=link-defaults>
spec:dom; type:interface;
text:Document
text:Element;
spec:css-color-3; type:property; text:color;
spec:css-speech-1; type:property;
text:cue-after;
text:cue-before;
text:pause-after;
text:pause-before;
text:speak;
text:voice-family;
spec:css21; type:property;
text:max-height;
text:max-width;
text:min-height;
text:min-width;
spec:css-tables-3; type:property;
text:border-collapse;
text:caption-side;
text:empty-cells;
text:table-layout;
spec:css-position-3; type:value; for:left; text:auto;
spec:css-transforms-1;
type:type;
text:<transform-list>;
text:<transform-function>;
type:property;
text:transform;
text:backface-visibility;
text:perspective;
text:perspective-origin;
text:transform-origin;
text:transform-style;
spec:css-syntax-3;
type:dfn; text:identifier
spec:css-values-4;
type:type; text:<position>
type:value; text:in
spec:infra;
type: dfn;
text: list
text: code point
</pre>
Introduction {#intro}
=====================
Converting CSSOM value strings into meaningfully typed JavaScript representations and
back can incur a significant performance overhead. This specification exposes CSS values
as typed JavaScript objects to facilitate their performant manipulation.
The API exposed by this specification is designed for performance rather than ergonomics.
Some particular considerations:
* retrieved JavaScript representations of CSS values are not mutable - instead updates
must explicitly be set using the API.
* objects are organized for consistency rather than ease of access. For example, even
though lengths are often numeric pixel values, a declared {{CSSNumericValue}} can't be
treated as a number without first explicitly casting it to a {{CSSUnitValue}}, as calc
expressions and keywords are also valid lengths.
<!--
██████ ██████ ██████ ██████ ████████ ██ ██ ██ ████████ ██ ██ ███ ██ ██ ██ ████████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██████ ██████ ██████ ██ ██ ██ ██████ ██ ██ ██ ██ ██ ██ ██ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ █████████ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██████ ██████ ██████ ██ ██ ████████ ████████ ███ ██ ██ ████████ ███████ ████████
-->
{{CSSStyleValue}} objects {#stylevalue-objects}
============================================
<pre class='idl'>
interface CSSStyleValue {
stringifier;
static CSSStyleValue? parse(DOMString property, DOMString cssText);
static sequence<CSSStyleValue>? parseAll(DOMString property, DOMString cssText);
};
</pre>
{{CSSStyleValue}} objects are the base class of all CSS Values accessible via the Typed OM API.
Values that can't yet be directly supported by a {{CSSStyleValue}} subclass
are also represented as {{CSSStyleValue}} objects.
The <a for="CSSStyleValue">stringification behavior</a> of {{CSSStyleValue}} objects is to
return a normalized representation
(see [[#stylevalue-normalization]])
of the value the {{CSSStyleValue}} object represents.
The <dfn method for=CSSStyleValue>parse(DOMString <var>property</var>, DOMString <var>cssText</var>)</dfn>,
when invoked, must [=parse a CSSStyleValue=] with property <var>property</var>, cssText <var>cssText</var>,
and parseMultiple set to false.
The <dfn method for=CSSStyleValue>parseAll(DOMString <var>property</var>, DOMString <var>cssText</var>)</dfn>,
when invoked, must [=parse a CSSStyleValue=] with property <var>property</var>, cssText <var>cssText</var>,
and parseMultiple set to true.
<div algorithm="parse a CSSStyleValue">
To <dfn>parse a CSSStyleValue</dfn> given a <var>property</var>, <var>cssText</var>, and a
<var>parseMultiple</var> flag, run these steps:
1. Attempt to parse |property| as an <<ident>>.
If this fails,
[=throw=] a {{SyntaxError}} and exit this algorithm.
Otherwise, let |property| be the parsed result.
If |property| does not start with two dashes (U+002D HYPHEN),
let |property| be |property| [=ASCII lowercased=].
2. If |property| is not a [=supported property name=],
[=throw=] a {{TypeError}} and exit this algorithm.
3. Attempt to <a lt="parse something according to a CSS grammar">parse</a> |cssText| according to |property|’s grammar.
If this fails,
[=throw=] a {{SyntaxError}} and exit this algorithm.
Otherwise,
let |value| be the parsed result.
4. If |property| is a [=list-valued property=], and |parseMultiple| is true,
subdivide |value| into a list of {{CSSStyleValue}} objects,
each representing one [=list-valued property iteration=],
and let |value| be the result. Return |value| as a sequence of
{{CSSStyleValue}} objects.
5. If |property| is a [=list-valued property=], and |parseMultiple| is false
subdivide |value| into a list of {{CSSStyleValue}} objects,
each representing one [=list-valued property iteration=],
and let |value| be the first entry in this list.
6. return a {{CSSStyleValue}} representing |value|.
</div>
Issue(305): appropriate failure indicators for this algorithm
<!--
██████ ████████ ██ ██ ██ ████████ ██ ██ ███ ████████
██ ██ ██ ██ ██ ██ ██ ███ ███ ██ ██ ██ ██
██ ██ ████ ██ ██ ████ ████ ██ ██ ██ ██
██████ ██ ██ ██ ██████ ██ ███ ██ ██ ██ ████████
██ ██ ██ ██ ██ ██ ██ █████████ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ██ ████████ ████████ ██ ██ ██ ██ ██
-->
The {{StylePropertyMap}} {#the-stylepropertymap}
================================================
<pre class='idl'>
interface StylePropertyMapReadOnly {
CSSStyleValue? get(DOMString property);
sequence<CSSStyleValue> getAll(DOMString property);
boolean has(DOMString property);
iterable<DOMString, (CSSStyleValue or sequence<CSSStyleValue>)>;
sequence<DOMString> getProperties();
stringifier;
};
callback UpdateFunction = CSSStyleValue (CSSStyleValue oldValue);
interface StylePropertyMap : StylePropertyMapReadOnly {
void append(DOMString property, (CSSStyleValue or DOMString)... values);
void delete(DOMString property);
void set(DOMString property, (CSSStyleValue or DOMString)... values);
void update(DOMString property, UpdateFunction updateFunction);
};
</pre>
A {{StylePropertyMapReadOnly}} object has an associated <dfn>property model</dfn>,
which is a list of property - sequence<{{CSSStyleValue}}> pairs. This list
is initialized differently depending on where the {{CSSStyleValue}} is used
(see
[[#computed-stylepropertymapreadonly-objects]],
[[#declared-stylepropertymap-objects]], and
[[#inline-stylepropertymap-objects]]).
All properties stored on the [=property model=] in {{StylePropertyMapReadOnly}} are [=ASCII case-insensitive=] within the ASCII range (i.e., [a-z] and [A-Z] are equivalent).
<div class='note'>
The sequence of {{CSSStyleValue}}s associated with a property do
not represent multiple successive definitions of that property's value.
Instead, sequences represent values associated with list-valued properties.
This approach allows single-valued properties to become list-valued in the
future without breaking code that relies on calling
{{StylePropertyMapReadOnly/get()}} and/or
{{StylePropertyMap/set()}} for those properties.
</div>
The <dfn method for=StylePropertyMap>append(DOMString <var>property</var>,
(CSSStyleValue or DOMString)... <var>values</var>)</dfn> method, when invoked, must
[=append to a StylePropertyMap=] with property <var>property</var> and values <var>values</var>.
<div algorithm>
To <dfn>append to a StylePropertyMap</dfn> given a <var>property</var> and a list of
<var>values</var>, run these steps:
1. If |property| does not start with two dashes (U+002D HYPHEN),
let |property| be |property| [=ASCII lowercased=].
2. If |property| is not a [=supported property name=],
[=throw=] a {{TypeError}} and exit this algorithm.
3. If |property| is not a [=list-valued property=],
[=throw=] a {{TypeError}} and exit this algorithm.
4. If {{StylePropertyMap}}’s [=property model=] contains an entry for |property|,
let |entry| be that entry.
Otherwise, create a new entry for |property| with an empty list,
add it to the [=property model=],
and let |entry| be the newly-created entry.
5. Let |values to append| be the empty list.
6. For each |value| in |values| if the <a>algorithm that coerces |value| into an appropriate type for a given property</a> does not throw an error, append the returned object to |values to append|.
7. Append |values to append| to the end of |entry|’s list.
</div>
<div algorithm>
To <dfn>delete a StylePropertyMap</dfn> given a <var>property</var> and a list of
<var>values</var>, run these steps:
1. If |property| does not start with two dashes (U+002D HYPHEN),
let |property| be |property| [=ASCII lowercased=].
2. If |property| is not a [=supported property name=],
[=throw=] a {{TypeError}} and exit this algorithm.
3. If {{StylePropertyMap}}’s [=property model=] contains an entry for |property|,
remove that entry from the [=property model=].
</div>
<div algorithm>
This section describes the <dfn>algorithm that coerces value into an appropriate type for a given property</dfn>, or fails and throws a {{TypeError}}:
: If |value| is a {{CSSStyleValue}},
:: If |value| does not match the grammar of a [=list-valued property iteration=] of |property|,
[=throw=] a {{TypeError}} and exit this algorithm.
Otherwise, return the |value|.
: If |value| is a {{DOMString}},
:: [=Parse a CSSStyleValue=] with property |property| and value |value| and return the resulting |value|.
If the result is null,
[=throw=] a {{TypeError}} and exit this algorithm.
Otherwise, append each [=list-valued property iteration=] in the result to a |values to append| object
and return |values to append|.
</div>
<div algorithm>
To <dfn>get a value from a StylePropertyMap</dfn>, run these steps:
1. If |property| does not start with two dashes (U+002D HYPHEN),
let |property| be |property| [=ASCII lowercased=].
2. If |property| is not a [=supported property name=],
[=throw=] a {{TypeError}} and exit this algorithm.
3. If {{StylePropertyMap}}’s [=property model=] contains an entry for |property|,
return the first value found in that entry.
4. Else, return `null`.
</div>
<div algorithm>
To <dfn>check if StylePropertyMap has a property</dfn>, run these steps:
1. Run the algorithm to [=get a value from a StylePropertyMap=] with property <var>property</var>,
1. If the algoritm returns a {{CSSStyleValue}} return true.
2. If the algorithm returns `null` return false.
</div>
<div algorithm>
To <dfn>set a value on a StylePropertyMap</dfn>, run these steps:
1. If |property| does not start with two dashes (U+002D HYPHEN),
let |property| be |property| [=ASCII lowercased=].
2. If |property| is not a [=supported property name=],
[=throw=] a {{TypeError}} and exit this algorithm.
3. If {{StylePropertyMap}}’s [=property model=] contains an entry for |property|,
let |entry| be that entry.
Otherwise, exit the algorithm.
4. Empty the list of |values| currently stored on the |entry|.
5. Let |values to set| be an empty list.
6. For each |value| in |values| if the [=algorithm that coerces value into an appropriate type for a given property=] does not throw an error, append the returned object to |values to set|.
7. Set |values to set| to be |entry|’s list.
</div>
The <dfn method for=StylePropertyMap>update(DOMString <var>property</var>,
UpdateFunction <var>updateFunction</var>)</dfn> method, when invoked, must
[=update a value in a StylePropertyMap=] with property name
<var>property</var>, update function <var>updateFunction</var>, and property
map set to the object this method was invoked on .
<div algorithm>
To <dfn>update a value in a StylePropertyMap</dfn> given a <var>property
name</var>, <var>update function</var>, and <var>property map</var>, run these
steps:
1. Let <var>old value</var> be the result of running the algorithm to
[=get a value from a StylePropertyMap=] with property name <var>property
name</var> and property map <var>property map</var>.
2. Let <var>new value</var> be the return value given by [=invoking=] the callback
<var>update function</var> with a single input of <var>old value</var>.
3. Run the algorithm to [=set a value on a StylePropertyMap=] with property name <var>property name</var>,
value <var>new value</var>, and property map <var>property map</var>.
</div>
The <dfn method for=StylePropertyMap>getProperties()</dfn> method returns all of the properties listed in
the [=property model=]. This list of properties is sorted in the following manner:
* normal properties are sorted alphabetically.
* custom properties are sorted by increasing code point order.
* custom properties are sorted after normal properties.
[=computed StylePropertyMap=], [=declared StylePropertyMap=] and [=inline StylePropertyMap=] are all live objects: the attributes and methods on these objects must operate on the actual underlying data, not a snapshot of the data.
Issue(148): add detailed descriptions of the rest of the methods on {{StylePropertyMap}}
Issue(268): stringification
Issue(276): interaction with custom properties
Issue(310): consider using properties in addition to get/set
Computed {{StylePropertyMapReadOnly}} objects {#computed-stylepropertymapreadonly-objects}
--------------------------------------------------------------------------
<pre class='idl'>
partial interface Element {
StylePropertyMapReadOnly computedStyleMap();
};
</pre>
<dfn>Computed StylePropertyMap</dfn> objects represent the computed style of an
{{Element}} or pseudo element, and are accessed by calling the
{{Element/computedStyleMap()}} method.
When constructed, the [=property model=] for [=computed StylePropertyMap=]
objects is initialized to contain an entry for every valid CSS property supported by the User Agent.
Note: The StylePropertyMap returned by computedStyleMap represents computed style,
not resolved style. In this regard it provides different values than those
in objects returned by getComputedStyle.
Declared {{StylePropertyMap}} objects {#declared-stylepropertymap-objects}
----------------------------------------------------------------------------
<pre class='idl'>
partial interface CSSStyleRule {
[SameObject] readonly attribute StylePropertyMap attributeStyleMap;
};
</pre>
<dfn>Declared StylePropertyMap</dfn> objects represent style property-value pairs embedded
in a style rule, and are accessed via the <dfn attribute for=CSSStyleRule>attributeStyleMap</dfn>
attribute of {{CSSStyleRule}} objects.
When constructed, the [=property model=] for [=declared StylePropertyMap=]
objects is initialized to contain
an entry for each property that is paired with at least one valid value inside the
{{CSSStyleRule}} that the object represents. The value for a given property is
the last valid value provided by the {{CSSStyleRule}} object.
Inline {{StylePropertyMap}} objects {#inline-stylepropertymap-objects}
----------------------------------------------------------------------
<pre class='idl'>
partial interface Element {
[SameObject] readonly attribute StylePropertyMap attributeStyleMap;
};
</pre>
<dfn>Inline StylePropertyMap</dfn> objects represent inline style declarations attached
directly to {{Element}}s. They are accessed via the <dfn attribute for=Element>attributeStyleMap</dfn>
attribute of {{Element}} objects.
When constructed, the [=property model=] for [=inline StylePropertyMap=] objects
is initialized to contain an
entry for each property that is paired with at least one valid value in the string
representing the style attribute for the Element that the object is associated with.
The value for a given property is the last valid value provided in the string.
{{CSSStyleValue}} subclasses {#stylevalue-subclasses}
==================================================
<!--
██ ██ ██ ██ ████████ ███ ████████ ██████ ████████ ████████
██ ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ████████ ██ ██ ████████ ██████ ██████ ██ ██
██ ██ ██ ████ ██ █████████ ██ ██ ██ ██ ██ ██
██ ██ ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
███████ ██ ██ ██ ██ ██ ██ ██ ██████ ████████ ████████
-->
{{CSSUnparsedValue}} objects {#unparsedvalue-objects}
----------------------------------------------
<pre class='idl'>
[Constructor((DOMString or CSSVariableReferenceValue)... members)]
interface CSSUnparsedValue : CSSStyleValue {
iterable<(DOMString or CSSVariableReferenceValue)>;
readonly attribute unsigned long length;
getter (DOMString or CSSVariableReferenceValue) (unsigned long index);
};
interface CSSVariableReferenceValue {
attribute DOMString variable;
attribute CSSUnparsedValue? fallback;
};
</pre>
Issue(239): Is this the best representation for this object?
{{CSSUnparsedValue}} objects represent values that reference custom properties.
They represent a list of string fragments and variable references.
They have a <dfn attribute for=CSSUnparsedValue>\[[tokens]]</dfn> internal slot,
which is a [=list=] of alternating {{DOMString}} and {{CSSVariableReferenceValue}} objects.
This list is the object's [=values to iterate over=].
The <dfn attribute for=CSSUnparsedValue>length</dfn> attribute indicates how many string fragments and variable references are contained within the {{CSSUnparsedValue}}.
The <dfn for=CSSUnparsedValue>indexed getter</dfn> retrieves the string fragment or variable reference at the provided index.
<!--
██ ██ ████████ ██ ██ ██ ██ ███████ ████████ ████████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██
█████ ██████ ██ ██ ██ ██ ██ ██ ████████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ████████ ██ ███ ███ ███████ ██ ██ ████████
-->
{{CSSKeywordValue}} objects {#keywordvalue-objects}
---------------------------------------------------
{{CSSKeywordValue}} objects represent CSS keywords and other [=identifiers=].
<pre class='idl'>
[Constructor(DOMString value)]
interface CSSKeywordValue : CSSStyleValue {
attribute DOMString value;
stringifier;
};
</pre>
<div algorithm>
The <dfn constructor for=CSSKeywordValue>CSSKeywordValue(|value|)</dfn> constructor must,
when called,
perform the following steps:
1. Return a new {{CSSKeywordValue}}
with its {{CSSKeywordValue/value}} internal slot
set to |value|.
</div>
<div algorithm="CSSKeywordValue stringification behavior">
The <dfn for=CSSKeywordValue>stringification behavior</dfn> of a {{CSSKeywordValue}} |this| is:
1. Let |token| be an <<ident-token>>
with its value set to |this|’s {{CSSKeywordValue/value}} internal slot.
2. Return the serialization of |token|.
</div>
Any place that accepts a {{CSSKeywordValue}}
also accepts a raw {{DOMString}},
by using the following typedef and algorithm:
<pre class=idl>
typedef (DOMString or CSSKeywordValue) CSSKeywordish;
</pre>
<div algorithm>
To <dfn export>rectify a keywordish value</dfn> |val|,
perform the following steps:
1. If |val| is a {{CSSKeywordValue}},
return |val|.
2. If |val| is a {{DOMString}},
return a new {{CSSKeywordValue}}
with its {{CSSKeywordValue/value}} internal slot
set to |val|.
</div>
<!--
██ ██ ██ ██ ██ ██ ████████ ████████ ████ ██████
███ ██ ██ ██ ███ ███ ██ ██ ██ ██ ██ ██
████ ██ ██ ██ ████ ████ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ███ ██ ██████ ████████ ██ ██
██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ███████ ██ ██ ████████ ██ ██ ████ ██████
-->
Numeric Values: {#numeric-objects}
----------------------------------
{{CSSNumericValue}} objects represent CSS values that are numeric in nature
(<<number>>s, <<percentage>>s, <<dimension>>s).
* {{CSSUnitValue}} objects represent values that contain a single unit type
(for example "42px").
* {{CSSMathValue}} objects represent math expressions,
which can contain more than one value/unit
(for example "calc(56em + 10%)").
{{CSSNumericValue}} objects are not range-restricted.
Any valid numeric value can be represented by a {{CSSNumericValue}},
and that value will not be clamped, rounded, or rejected
when set on a [=declared StylePropertyMap=] or [=inline StylePropertyMap=].
Instead, clamping and/or rounding will occur during computation of style.
<div class='example'>
The following code is valid
<pre class=lang-js>
myElement.attributeStyleMap.set("opacity", CSS.number(3));
myElement.attributeStyleMap.set("z-index", CSS.number(15.4));
console.log(myElement.attributeStyleMap.get("opacity").value); // 3
console.log(myElement.attributeStyleMap.get("z-index").value); // 15.4
var computedStyle = myElement.computedStyleMap();
var opacity = computedStyle.get("opacity");
var zIndex = computedStyle.get("z-index");
</pre>
After execution, the value of `opacity` is `1` ('opacity' is range-restricted),
and the value of `zIndex` is `15` ('z-index' is rounded to an integer value).
</div>
Note: "Numeric values" which incorporate variable references
will instead be represented as {{CSSUnparsedValue}} objects,
and keywords as {{CSSKeywordValue}} objects.
Any place that accepts a {{CSSNumericValue}}
also accepts a raw {{double}},
by using the following typedef and algorithm:
<pre class=idl>
typedef (double or CSSNumericValue) CSSNumberish;
</pre>
<div algorithm>
To <dfn export lt="rectify a numberish value | rectifying a numberish value">rectify a numberish value</dfn> |num|,
perform the following steps:
1. If |num| is a {{CSSNumericValue}},
return |num|.
2. If |num| is a {{double}},
return a new {{CSSUnitValue}}
with its {{CSSUnitValue/value}} internal slot set to |num|
and its {{CSSUnitValue/unit}} internal slot set to "number".
</div>
### Common Numeric Operations, and the {{CSSNumericValue}} Superclass ### {#numeric-value}
All numeric CSS values
(<<number>>s, <<percentage>>s, and <<dimension>>s)
are represented by subclasses of the {{CSSNumericValue}} interface.
<xmp class=idl>
interface CSSNumericValue : CSSStyleValue {
CSSNumericValue add(CSSNumberish... values);
CSSNumericValue sub(CSSNumberish... values);
CSSNumericValue mul(CSSNumberish... values);
CSSNumericValue div(CSSNumberish... values);
CSSNumericValue min(CSSNumberish... values);
CSSNumericValue max(CSSNumberish... values);
boolean equals(CSSNumberish... value);
CSSNumericValue to(DOMString unit);
CSSMathSum toSum(DOMString... units);
// ??? type();
static CSSNumericValue parse(DOMString cssText);
};
</xmp>
Issue: Figure out how we want to represent the type of an expression in JS,
and define the type() method accordingly.
The methods on the {{CSSNumericValue}} superclass
represent operations that all numeric values can perform.
The following are the arithmetic operations you can perform on dimensions:
<div algorithm="CSSNumericValue.add()">
The <dfn for=CSSNumericValue method>add(...|values|)</dfn> method,
when called on a {{CSSNumericValue}} |this|,
must perform the following steps:
1. Replace each [=list/item=] of |values|
with the result of [=rectifying a numberish value=] for the [=list/item=].
2. If |this| is a {{CSSMathSum}} object,
[=list/prepend=] the [=list/items=] in |this|’s {{CSSMathSum/values}} internal slot
to |values|.
Otherwise,
prepend |this| to |values|.
3. If all of the [=list/items=] in |values| are {{CSSUnitValue}}s
and have the same {{CSSUnitValue/unit}},
return a new {{CSSUnitValue}}
whose {{CSSUnitValue/unit}} internal slot is set to |this|’s {{CSSUnitValue/unit}} internal slot,
and {{CSSUnitValue/value}} internal slot is set to the sum of
the {{CSSUnitValue/value}} internal slots
of the [=list/items=] in |values|.
4. Let |type| be the result of [=adding=] the [=types=] of every [=list/item=] in |values|.
If |type| is failure,
[=throw=] a {{TypeError}}.
5. Return a new {{CSSMathSum}} object
whose {{CSSMathSum/values}} internal slot
is set to |values|.
</div>
<div algorithm="CSSNumericValue.sub()">
The <dfn for=CSSNumericValue method>sub(...|values|)</dfn> method,
when called on a {{CSSNumericValue}} |this|,
must perform the following steps:
1. Replace each [=list/item=] of |values|
with the result of [=rectifying a numberish value=] for the [=list/item=],
then [=CSSMath/negating=] the value.
2. Return the result of calling the {{CSSNumericValue/add()}} internal algorithm
with |this| and |values|.
</div>
<div algorithm>
To <dfn noexport for=CSSMath>negate</dfn> a {{CSSNumericValue}} |this|:
1. If |this| is a {{CSSMathNegate}} object,
return |this|’s {{CSSMathNegate/value}} internal slot.
2. If |this| is a {{CSSUnitValue}} object,
return a new {{CSSUnitValue}}
with the same {{CSSUnitValue/unit}} internal slot as |this|,
and a {{CSSUnitValue/value}} internal slot set to the negation of |this|’s.
2. Otherwise,
return a new {{CSSMathNegate}} object
whose {{CSSMathNegate/value}} internal slot
is set to |this|.
</div>
<div algorithm="CSSNumericValue.mul()">
The <dfn for=CSSNumericValue method>mul(...|values|)</dfn> method,
when called on a {{CSSNumericValue}} |this|,
must perform the following steps:
1. Replace each [=list/item=] of |values|
with the result of [=rectifying a numberish value=] for the [=list/item=].
2. If |this| is a {{CSSMathProduct}} object,
[=list/prepend=] the [=list/items=] in |this|’s {{CSSMathProduct/values}} internal slot
to |values|.
Otherwise,
prepend |this| to |values|.
3. Let |type| be the result of [=multiplying=] the [=types=] of every [=list/item=] in |values|.
If |type| is failure,
[=throw=] a {{TypeError}}.
5. Return a new {{CSSMathProduct}} object
whose {{CSSMathProduct/values}} internal slot
is set to |values|.
</div>
<div algorithm="CSSNumericValue.div()">
The <dfn for=CSSNumericValue method>div(...|values|)</dfn> method,
when called on a {{CSSNumericValue}} |this|,
must perform the following steps:
1. Replace each [=list/item=] of |values|
with the result of [=rectifying a numberish value=] for the [=list/item=],
then [=CSSMath/inverting=] the value.
2. Return the result of calling the {{CSSNumericValue/mul()}} internal algorithm
with |this| and |values|.
</div>
<div algorithm>
To <dfn noexport for=CSSMath>invert</dfn> a {{CSSNumericValue}} |this|:
1. If |this| is a {{CSSMathInvert}} object,
return |this|’s {{CSSMathInvert/value}} internal slot.
2. Otherwise,
return a new {{CSSMathInvert}} object
whose {{CSSMathInvert/value}} internal slot
is set to |this|.
</div>
<div algorithm="CSSNumericValue.min()">
The <dfn for=CSSNumericValue method>min(...|values|)</dfn> method,
when called on a {{CSSNumericValue}} |this|,
must perform the following steps:
1. Replace each [=list/item=] of |values|
with the result of [=rectifying a numberish value=] for the [=list/item=].
2. If |this| is a {{CSSMathMin}} object,
[=list/prepend=] the [=list/items=] in |this|’s {{CSSMathMin/values}} internal slot
to |values|.
Otherwise,
prepend |this| to |values|.
3. Let |type| be the result of [=adding=] the [=types=] of every [=list/item=] in |values|.
If |type| is failure,
[=throw=] a {{TypeError}}.
4. Return a new {{CSSMathMin}} object
whose {{CSSMathMin/values}} internal slot
is set to |values|.
</div>
<div algorithm="CSSNumericValue.max()">
The <dfn for=CSSNumericValue method>max(...|values|)</dfn> method,
when called on a {{CSSNumericValue}} |this|,
must perform the following steps:
1. Replace each [=list/item=] of |values|
with the result of [=rectifying a numberish value=] for the [=list/item=].
2. If |this| is a {{CSSMathMax}} object,
[=list/prepend=] the [=list/items=] in |this|’s {{CSSMathMax/values}} internal slot
to |values|.
Otherwise,
prepend |this| to |values|.
3. Let |type| be the result of [=adding=] the [=types=] of every [=list/item=] in |values|.
If |type| is failure,
[=throw=] a {{TypeError}}.
4. Return a new {{CSSMathMax}} object
whose {{CSSMathMax/values}} internal slot
is set to |values|.
</div>
<div algorithm="CSSNumericValue.equals(value)">
The <dfn for=CSSNumericValue method>equals(...|values|)</dfn> method,
when called on a {{CSSNumericValue}} |this|,
must perform the following steps:
1. Replace each [=list/item=] of |values|
with the result of [=rectifying a numberish value=] for the [=list/item=].
2. Let |thisAndValues| be |this| prepended to |values|.
3. If all |item|s of |thisAndValues| do not belong to the same class return false. Example: all items in |thisAndValues| must be {{CSSUnitValue}}.
4. If all |item|s in |thisAndValues| are {{CSSUnitValue}s and have the same {{CSSUnitValue/unit}} and {{CSSUnitValue/value}} return true.
5. If all |item|s in |thisAndValues| are {{CSSMathValue}}s :
1. If |thisAndValues| are all either {{CSSMathSum}}, {{CSSMathProduct}}, {{CSSMathMin}} or {{CSSMathMax}}, iterate over the {{CSSNumericArray}}s on |thisAndValues|,
1. For each {{CSSNumericValue}} in each of the {{CSSNumericArray}}s go back to step 1 to check that the {{CSSUnitValue}}s on all items in |thisAndValues| are equal.
2. If |thisAndValues| are all either {{CSSMathNegate}} or {{CSSMathInvert}}:
1. Go back to step 1 to check that the {{CSSUnitValue}} on all items in |thisAndValues| are equal.
</div>
<div algorithm="CSSNumericValue.to()">
The <dfn method for=CSSNumericValue>to(|unit|)</dfn> method converts an existing {{CSSNumericValue}} |this|
into another one with the specified |unit|,
if possible.
When called, it must perform the following steps:
1. Let |type| be the result of [=creating a type=] from |unit|.
If |type| is failure,
[=throw=] a {{SyntaxError}}.
2. Let |sum| be the result of [=creating a sum value=] from |this|.
If |sum| is failure,
[=throw=] a {{TypeError}}.
3. If |sum| has more than one [=list/item=],
[=throw=] a {{TypeError}}.
Otherwise, let |item| be the result of [=create a CSSUnitValue from a sum value item|creating a CSSUnitValue=]
from the sole [=list/item=] in |sum|,
then [=convert a CSSUnitValue|converting=] it to |unit|.
If |item| is failure,
[=throw=] a {{TypeError}}.
4. Return |item|.
</div>
<div algorithm>
When asked to <dfn export>create a CSSUnitValue from a sum value item</dfn> |item|,
perform the following steps:
1. If |item| has more than one [=map/entry=] in its [=sum value/unit map=],
return failure.
2. If |item| has no [=map/entries=] in its [=sum value/unit map=],
return a new {{CSSUnitValue}}
whose {{CSSUnitValue/unit}} internal slot
is set to "number",
and whose {{CSSUnitValue/value}} internal slot
is set to |item|’s [=sum value/value=].
3. Otherwise, |item| has a single [=map/entry=] in its [=sum value/unit map=].
If that [=map/entry’s=] [=map/value=] is anything other than `1`,
return failure.
4. Otherwise, return a new {{CSSUnitValue}}
whose {{CSSUnitValue/unit}} internal slot
is set to that [=map/entry’s=] [=map/key=],
and whose {{CSSUnitValue/value}} internal slot
is set to |item|’s [=sum value/value=].
</div>
<div algorithm="CSSNumericValue.toSum()">
The <dfn method for=CSSNumericValue>toSum(...|units|)</dfn> method converts an existing {{CSSNumericValue}} |this|
into a {{CSSMathSum}} of only {{CSSUnitValue}}s with the specified units,
if possible.
(It's like {{CSSNumericValue/to()}},
but allows the result to have multiple units in it.)
If called without any units,
it just simplifies |this| into a minimal sum of {{CSSUnitValue}}s.
When called, it must perform the following steps:
1. [=list/For each=] |unit| in |units|,
if the result of [=creating a type=] from |unit| is failure,
[=throw=] a {{SyntaxError}}.
2. Let |sum| be the result of [=creating a sum value=] from |this|.
If |sum| is failure,
[=throw=] a {{TypeError}}.
3. Let |values| be the result of [=create a CSSUnitValue from a sum value item|creating a CSSUnitValue=]
[=list/for each=] [=list/item=] in |sum|.
If any [=list/item=] of |values| is failure,
[=throw=] a {{TypeError}}.
4. If |units| is [=list/empty=],
sort |values| in [=code point=] order according to the {{CSSUnitValue/unit}} internal slot of its [=list/items=],
then return a new {{CSSMathSum}} object
whose {{CSSMathSum/values}} internal slot is set to |values|.
5. Otherwise,
let |result| initially be an empty [=list=].
[=list/For each=] |unit| in |units|:
1. Let |temp| initially be a new {{CSSUnitValue}}
whose {{CSSUnitValue/unit}} internal slot
is set to |unit|
and whose {{CSSUnitValue/value}} internal slot
is set to `0`.
2. [=list/For each=] |value| in |values|:
1. Let |value unit| be |value|’s {{CSSUnitValue/unit}} internal slot.
2. If |value unit| is a [=compatible unit=] with |unit|,
then:
1.[=convert a CSSUnitValue|convert=] |value| to |unit|.
2. Increment |temp|’s {{CSSUnitValue/value}} internal slot
by the value of |value|’s {{CSSUnitValue/value}} internal slot.
3. [=list/Remove=] |value| from |values|.
3. [=list/Append=] |temp| to |result|.
6. If |values| is not [=list/empty=],
[=throw=] a {{TypeError}}.
<span class=note>|this| had units that you didn't ask for.</span>
7. Return a new {{CSSMathSum}} object
whose {{CSSMathSum/values}} internal slot
is set to |result|.
</div>
<div algorithm="sum value">
A <dfn>sum value</dfn>
is an abstract representation of a {{CSSNumericValue}}
as a sum of numbers with (possibly complex) units.
Not all {{CSSNumericValue}}s can be expressed as a [=sum value=].
A [=sum value=] is a [=list=].
Each entry in the list is a [=tuple=] of a <dfn for="sum value">value</dfn>,
which is a number,
and a <dfn for="sum value">unit map</dfn>,
which is a [=ordered map|map=] of units (strings) to powers (integers).
<div class=example>
Here are a few examples of CSS values,
and their equivalent [=sum values=]:
* ''1px'' becomes `«(1, «["px" → 1]»)»`
* ''calc(1px + 1in)'' becomes `«(97, «["px" → 1]»)»`
(because ''in'' and ''px'' are [=compatible units=],
and ''px'' is the [=canonical unit=] for them)
* ''calc(1px + 2em)'' becomes `«(1, «["px" → 1]»), (2, «["em" → 1]»)»`
* ''calc(1px + 2%)'' becomes `«(1, «["px" → 1]»), (2, «["percent" → 1]»)»`
(percentages are allowed to add to other units,
but aren't resolved into another unit,
like they are in a [=type=])
* ''calc(1px * 2em)'' becomes `«(2, «["em" → 1, "px" → 1]»)»`
* ''calc(1px + 1deg)'' can't be represented as a [=sum value=]
because it's an invalid computation
* ''calc(1px * 1deg)'' becomes `«(2, «["deg" → 1, "px" → 1]»)»`
</div>
To <dfn lt="create a sum value|creating a sum value">create a sum value</dfn> from a {{CSSNumericValue}} |this|,
the steps differ based on |this|’s class:
<dl class=switch>
: {{CSSUnitValue}}
::
<div algorithm="sum value from CSSUnitValue">
1. Let |unit| be the value of |this|’s {{CSSUnitValue/unit}} internal slot,
and |value| be the value of |this|’s {{CSSUnitValue/value}} internal slot.
2. If |unit| is a member of a set of [=compatible units=],
and is not the set's [=canonical unit=],
multiply |value| by the conversion ratio between |unit| and the [=canonical unit=],
and change |unit| to the [=canonical unit=].
3. If |unit| is `"number"`,
return «(|value|, «[ ]»)».
3. Otherwise, return <code>«(|value|, «[|unit| → 1]»)»</code>.
</div>
: {{CSSMathSum}}
::
<div algorithm="sum value from CSSMathSum">
1. Let |values| initially be an empty list.
2. [=list/For each=] |item| in <var ignore>this</var>’s {{CSSMathSum/values}} internal slot:
1. Let |value| be the result of [=creating a sum value=] from |item|.
If |value| is failure,