-
-
Notifications
You must be signed in to change notification settings - Fork 644
/
properties.py
1528 lines (1270 loc) · 44.4 KB
/
properties.py
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
"""
weasyprint.css.validation.properties
------------------------------------
Validate properties.
See http://www.w3.org/TR/CSS21/propidx.html and various CSS3 modules.
"""
from tinycss2.color3 import parse_color
from .. import computed_values
from ..properties import KNOWN_PROPERTIES, Dimension
from ..utils import (
InvalidValues, check_var_function, comma_separated_list, get_angle,
get_content_list, get_content_list_token, get_custom_ident, get_image,
get_keyword, get_length, get_resolution, get_single_keyword, get_url,
parse_2d_position, parse_function, parse_position, remove_whitespace,
single_keyword, single_token)
PREFIX = '-weasy-'
PROPRIETARY = set()
UNSTABLE = set()
MULTIVAL_PROPERTIES = set()
# Yes/no validators for non-shorthand properties
# Maps property names to functions taking a property name and a value list,
# returning a value or None for invalid.
# For properties that take a single value, that value is returned by itself
# instead of a list.
PROPERTIES = {}
# Validators
def property(property_name=None, proprietary=False, unstable=False,
multiple_values=False, wants_base_url=False):
"""Decorator adding a function to the ``PROPERTIES``.
The name of the property covered by the decorated function is set to
``property_name`` if given, or is inferred from the function name
(replacing underscores by hyphens).
:param proprietary:
Proprietary (vendor-specific, non-standard) are prefixed: anchors can
for example be set using ``-weasy-anchor: attr(id)``.
See https://www.w3.org/TR/CSS/#proprietary
:param unstable:
Mark properties that are defined in specifications that didn't reach
the Candidate Recommandation stage. They can be used both
vendor-prefixed or unprefixed.
See https://www.w3.org/TR/CSS/#unstable-syntax
:param wants_base_url:
The function takes the stylesheet’s base URL as an additional
parameter.
"""
def decorator(function):
"""Add ``function`` to the ``PROPERTIES``."""
if property_name is None:
name = function.__name__.replace('_', '-')
else:
name = property_name
assert name in KNOWN_PROPERTIES, name
assert name not in PROPERTIES, name
function.wants_base_url = wants_base_url
PROPERTIES[name] = function
if proprietary:
PROPRIETARY.add(name)
if unstable:
UNSTABLE.add(name)
if multiple_values:
MULTIVAL_PROPERTIES.add(name)
return function
return decorator
def validate_non_shorthand(base_url, name, tokens, required=False):
"""Default validator for non-shorthand properties."""
if name.startswith('--'):
# TODO: validate content
return ((name, tokens),)
if not required and name not in KNOWN_PROPERTIES:
hyphens_name = name.replace('_', '-')
if hyphens_name in KNOWN_PROPERTIES:
raise InvalidValues(f'you probably mean "{hyphens_name}"')
else:
raise InvalidValues('unknown property')
if not required and name not in PROPERTIES:
raise InvalidValues('property not supported yet')
if name not in MULTIVAL_PROPERTIES:
for token in tokens:
var_function = check_var_function(token)
if var_function:
return ((name, var_function),)
keyword = get_single_keyword(tokens)
if keyword in ('initial', 'inherit'):
value = keyword
else:
function = PROPERTIES[name]
if function.wants_base_url:
value = function(tokens, base_url)
else:
value = function(tokens)
if value is None:
raise InvalidValues
return ((name, value),)
@property()
@comma_separated_list
@single_keyword
def background_attachment(keyword):
"""``background-attachment`` property validation."""
return keyword in ('scroll', 'fixed', 'local')
@property('background-color')
@property('border-top-color')
@property('border-right-color')
@property('border-bottom-color')
@property('border-left-color')
@property('column-rule-color', unstable=True)
@property('text-decoration-color')
@single_token
def other_colors(token):
return parse_color(token)
@property()
@single_token
def outline_color(token):
if get_keyword(token) == 'invert':
return 'currentColor'
else:
return parse_color(token)
@property()
@single_keyword
def border_collapse(keyword):
return keyword in ('separate', 'collapse')
@property()
@single_keyword
def empty_cells(keyword):
"""``empty-cells`` property validation."""
return keyword in ('show', 'hide')
@property('color')
@single_token
def color(token):
"""``*-color`` and ``color`` properties validation."""
result = parse_color(token)
if result == 'currentColor':
return 'inherit'
else:
return result
@property('background-image', wants_base_url=True)
@comma_separated_list
@single_token
def background_image(token, base_url):
if token.type != 'function':
if get_keyword(token) == 'none':
return 'none', None
return get_image(token, base_url)
@property('list-style-image', wants_base_url=True)
@single_token
def list_style_image(token, base_url):
"""``list-style-image`` property validation."""
if get_keyword(token) == 'none':
return 'none', None
parsed_url = get_url(token, base_url)
if parsed_url:
if parsed_url[0] == 'url' and parsed_url[1][0] == 'external':
return 'url', parsed_url[1][1]
@property()
def transform_origin(tokens):
"""``transform-origin`` property validation."""
if len(tokens) == 3:
# Ignore third parameter as 3D transforms are ignored.
tokens = tokens[:2]
return parse_2d_position(tokens)
@property()
@comma_separated_list
def background_position(tokens):
"""``background-position`` property validation."""
return parse_position(tokens)
@property()
@comma_separated_list
def object_position(tokens):
"""``object-position`` property validation."""
return parse_position(tokens)
@property()
@comma_separated_list
def background_repeat(tokens):
"""``background-repeat`` property validation."""
keywords = tuple(map(get_keyword, tokens))
if keywords == ('repeat-x',):
return ('repeat', 'no-repeat')
if keywords == ('repeat-y',):
return ('no-repeat', 'repeat')
if keywords in (('no-repeat',), ('repeat',), ('space',), ('round',)):
return keywords * 2
if len(keywords) == 2 and all(
k in ('no-repeat', 'repeat', 'space', 'round')
for k in keywords):
return keywords
@property()
@comma_separated_list
def background_size(tokens):
"""Validation for ``background-size``."""
if len(tokens) == 1:
token = tokens[0]
keyword = get_keyword(token)
if keyword in ('contain', 'cover'):
return keyword
if keyword == 'auto':
return ('auto', 'auto')
length = get_length(token, negative=False, percentage=True)
if length:
return (length, 'auto')
elif len(tokens) == 2:
values = []
for token in tokens:
length = get_length(token, negative=False, percentage=True)
if length:
values.append(length)
elif get_keyword(token) == 'auto':
values.append('auto')
if len(values) == 2:
return tuple(values)
@property('background-clip')
@property('background-origin')
@comma_separated_list
@single_keyword
def box(keyword):
"""Validation for the ``<box>`` type used in ``background-clip``
and ``background-origin``."""
return keyword in ('border-box', 'padding-box', 'content-box')
@property()
def border_spacing(tokens):
"""Validator for the `border-spacing` property."""
lengths = [get_length(token, negative=False) for token in tokens]
if all(lengths):
if len(lengths) == 1:
return (lengths[0], lengths[0])
elif len(lengths) == 2:
return tuple(lengths)
@property('border-top-right-radius')
@property('border-bottom-right-radius')
@property('border-bottom-left-radius')
@property('border-top-left-radius')
def border_corner_radius(tokens):
"""Validator for the `border-*-radius` properties."""
lengths = [
get_length(token, negative=False, percentage=True) for token in tokens]
if all(lengths):
if len(lengths) == 1:
return (lengths[0], lengths[0])
elif len(lengths) == 2:
return tuple(lengths)
@property('border-top-style')
@property('border-right-style')
@property('border-left-style')
@property('border-bottom-style')
@property('column-rule-style', unstable=True)
@single_keyword
def border_style(keyword):
"""``border-*-style`` properties validation."""
return keyword in ('none', 'hidden', 'dotted', 'dashed', 'double',
'inset', 'outset', 'groove', 'ridge', 'solid')
@property('break-before')
@property('break-after')
@single_keyword
def break_before_after(keyword):
"""``break-before`` and ``break-after`` properties validation."""
# 'always' is defined as an alias to 'page' in multi-column
# https://www.w3.org/TR/css3-multicol/#column-breaks
return keyword in ('auto', 'avoid', 'avoid-page', 'page', 'left', 'right',
'recto', 'verso', 'avoid-column', 'column', 'always')
@property()
@single_keyword
def break_inside(keyword):
"""``break-inside`` property validation."""
return keyword in ('auto', 'avoid', 'avoid-page', 'avoid-column')
@property()
@single_keyword
def box_decoration_break(keyword):
"""``box-decoration-break`` property validation."""
return keyword in ('slice', 'clone')
@property()
@single_token
def block_ellipsis(token):
"""``box-ellipsis`` property validation."""
if token.type == 'string':
return ('string', token.value)
else:
keyword = get_keyword(token)
if keyword in ('none', 'auto'):
return keyword
@property('continue', unstable=True)
@single_keyword
def continue_(keyword):
"""``continue`` property validation."""
return keyword in ('auto', 'discard')
@property(unstable=True)
@single_token
def max_lines(token):
if token.type == 'number' and token.int_value is not None:
if token.int_value >= 1:
return token.int_value
keyword = get_keyword(token)
if keyword == 'none':
return keyword
@property(unstable=True)
@single_keyword
def margin_break(keyword):
"""``margin-break`` property validation."""
return keyword in ('auto', 'keep', 'discard')
@property(unstable=True)
@single_token
def page(token):
"""``page`` property validation."""
if token.type == 'ident':
return 'auto' if token.lower_value == 'auto' else token.value
@property('bleed-left', unstable=True)
@property('bleed-right', unstable=True)
@property('bleed-top', unstable=True)
@property('bleed-bottom', unstable=True)
@single_token
def bleed(token):
"""``bleed`` property validation."""
keyword = get_keyword(token)
if keyword == 'auto':
return 'auto'
else:
return get_length(token)
@property(unstable=True)
def marks(tokens):
"""``marks`` property validation."""
if len(tokens) == 2:
keywords = tuple(get_keyword(token) for token in tokens)
if 'crop' in keywords and 'cross' in keywords:
return keywords
elif len(tokens) == 1:
keyword = get_keyword(tokens[0])
if keyword in ('crop', 'cross'):
return (keyword,)
elif keyword == 'none':
return ()
@property('outline-style')
@single_keyword
def outline_style(keyword):
"""``outline-style`` properties validation."""
return keyword in ('none', 'dotted', 'dashed', 'double', 'inset',
'outset', 'groove', 'ridge', 'solid')
@property('border-top-width')
@property('border-right-width')
@property('border-left-width')
@property('border-bottom-width')
@property('column-rule-width', unstable=True)
@property('outline-width')
@single_token
def border_width(token):
"""Border, column rule and outline widths properties validation."""
length = get_length(token, negative=False)
if length:
return length
keyword = get_keyword(token)
if keyword in ('thin', 'medium', 'thick'):
return keyword
@property(unstable=True)
@single_token
def column_width(token):
"""``column-width`` property validation."""
length = get_length(token, negative=False)
if length:
return length
keyword = get_keyword(token)
if keyword == 'auto':
return keyword
@property(unstable=True)
@single_keyword
def column_span(keyword):
"""``column-span`` property validation."""
return keyword in ('all', 'none')
@property()
@single_keyword
def box_sizing(keyword):
"""Validation for the ``box-sizing`` property from css3-ui"""
return keyword in ('padding-box', 'border-box', 'content-box')
@property()
@single_keyword
def caption_side(keyword):
"""``caption-side`` properties validation."""
return keyword in ('top', 'bottom')
@property()
@single_keyword
def clear(keyword):
"""``clear`` property validation."""
return keyword in ('left', 'right', 'both', 'none')
@property()
@single_token
def clip(token):
"""Validation for the ``clip`` property."""
function = parse_function(token)
if function:
name, args = function
if name == 'rect' and len(args) == 4:
values = []
for arg in args:
if get_keyword(arg) == 'auto':
values.append('auto')
else:
length = get_length(arg)
if length:
values.append(length)
if len(values) == 4:
return tuple(values)
if get_keyword(token) == 'auto':
return ()
@property(multiple_values=True, wants_base_url=True)
def content(tokens, base_url):
"""``content`` property validation."""
# See https://www.w3.org/TR/css-content-3/#content-property
tokens = list(tokens)
parsed_tokens = []
while tokens:
if len(tokens) >= 2 and (
tokens[1].type == 'literal' and tokens[1].value == ','):
token, tokens = tokens[0], tokens[2:]
parsed_token = (
get_image(token, base_url) or get_url(token, base_url))
if parsed_token:
parsed_tokens.append(parsed_token)
else:
return
else:
break
if len(tokens) == 0:
return
if len(tokens) >= 3 and tokens[-1].type == 'string' and (
tokens[-2].type == 'literal' and tokens[-2].value == '/'):
# Ignore text for speech
tokens = tokens[:-2]
keyword = get_single_keyword(tokens)
if keyword in ('normal', 'none'):
return (keyword,)
return get_content_list(tokens, base_url)
@property()
def counter_increment(tokens):
"""``counter-increment`` property validation."""
return counter(tokens, default_integer=1)
@property()
def counter_reset(tokens):
"""``counter-reset`` property validation."""
return counter(tokens, default_integer=0)
@property()
def counter_set(tokens):
"""``counter-set`` property validation."""
return counter(tokens, default_integer=0)
def counter(tokens, default_integer):
"""``counter-increment`` and ``counter-reset`` properties validation."""
if get_single_keyword(tokens) == 'none':
return ()
tokens = iter(tokens)
token = next(tokens, None)
assert token, 'got an empty token list'
results = []
while token is not None:
if token.type != 'ident':
return # expected a keyword here
counter_name = token.value
if counter_name in ('none', 'initial', 'inherit'):
raise InvalidValues(f'Invalid counter name: {counter_name}')
token = next(tokens, None)
if token is not None and (
token.type == 'number' and token.int_value is not None):
# Found an integer. Use it and get the next token
integer = token.int_value
token = next(tokens, None)
else:
# Not an integer. Might be the next counter name.
# Keep `token` for the next loop iteration.
integer = default_integer
results.append((counter_name, integer))
return tuple(results)
@property('top')
@property('right')
@property('left')
@property('bottom')
@property('margin-top')
@property('margin-right')
@property('margin-bottom')
@property('margin-left')
@single_token
def lenght_precentage_or_auto(token):
"""``margin-*`` properties validation."""
length = get_length(token, percentage=True)
if length:
return length
if get_keyword(token) == 'auto':
return 'auto'
@property('height')
@property('width')
@single_token
def width_height(token):
"""Validation for the ``width`` and ``height`` properties."""
length = get_length(token, negative=False, percentage=True)
if length:
return length
if get_keyword(token) == 'auto':
return 'auto'
@property(unstable=True)
@single_token
def column_gap(token):
"""Validation for the ``column-gap`` property."""
length = get_length(token, negative=False)
if length:
return length
keyword = get_keyword(token)
if keyword == 'normal':
return keyword
@property(unstable=True)
@single_keyword
def column_fill(keyword):
"""``column-fill`` property validation."""
return keyword in ('auto', 'balance')
@property()
@single_keyword
def direction(keyword):
"""``direction`` property validation."""
return keyword in ('ltr', 'rtl')
@property()
def display(tokens):
"""``display`` property validation."""
for token in tokens:
if token.type != 'ident':
return
if len(tokens) == 1:
value = tokens[0].value
if value in (
'none', 'table-caption', 'table-row-group', 'table-cell',
'table-header-group', 'table-footer-group', 'table-row',
'table-column-group', 'table-column'):
return (value,)
elif value in ('inline-table', 'inline-flex', 'inline-grid'):
return tuple(value.split('-'))
elif value == 'inline-block':
return ('inline', 'flow-root')
outside = inside = list_item = None
for token in tokens:
value = token.value
if value in ('block', 'inline'):
if outside:
return
outside = value
elif value in ('flow', 'flow-root', 'table', 'flex', 'grid'):
if inside:
return
inside = value
elif value == 'list-item':
if list_item:
return
list_item = value
else:
return
outside = outside or 'block'
inside = inside or 'flow'
if list_item:
if inside in ('flow', 'flow-root'):
return (outside, inside, list_item)
else:
return (outside, inside)
@property('float')
@single_keyword
def float_(keyword): # XXX do not hide the "float" builtin
"""``float`` property validation."""
return keyword in ('left', 'right', 'none')
@property()
@comma_separated_list
def font_family(tokens):
"""``font-family`` property validation."""
if len(tokens) == 1 and tokens[0].type == 'string':
return tokens[0].value
elif tokens and all(token.type == 'ident' for token in tokens):
return ' '.join(token.value for token in tokens)
@property()
@single_keyword
def font_kerning(keyword):
return keyword in ('auto', 'normal', 'none')
@property()
@single_token
def font_language_override(token):
keyword = get_keyword(token)
if keyword == 'normal':
return keyword
elif token.type == 'string':
return token.value
@property()
def font_variant_ligatures(tokens):
if len(tokens) == 1:
keyword = get_keyword(tokens[0])
if keyword in ('normal', 'none'):
return keyword
values = []
couples = (
('common-ligatures', 'no-common-ligatures'),
('historical-ligatures', 'no-historical-ligatures'),
('discretionary-ligatures', 'no-discretionary-ligatures'),
('contextual', 'no-contextual'))
all_values = []
for couple in couples:
all_values.extend(couple)
for token in tokens:
if token.type != 'ident':
return None
if token.value in all_values:
concurrent_values = [
couple for couple in couples if token.value in couple][0]
if any(value in values for value in concurrent_values):
return None
else:
values.append(token.value)
else:
return None
if values:
return tuple(values)
@property()
@single_keyword
def font_variant_position(keyword):
return keyword in ('normal', 'sub', 'super')
@property()
@single_keyword
def font_variant_caps(keyword):
return keyword in (
'normal', 'small-caps', 'all-small-caps', 'petite-caps',
'all-petite-caps', 'unicase', 'titling-caps')
@property()
def font_variant_numeric(tokens):
if len(tokens) == 1:
keyword = get_keyword(tokens[0])
if keyword == 'normal':
return keyword
values = []
couples = (
('lining-nums', 'oldstyle-nums'),
('proportional-nums', 'tabular-nums'),
('diagonal-fractions', 'stacked-fractions'),
('ordinal',), ('slashed-zero',))
all_values = []
for couple in couples:
all_values.extend(couple)
for token in tokens:
if token.type != 'ident':
return None
if token.value in all_values:
concurrent_values = [
couple for couple in couples if token.value in couple][0]
if any(value in values for value in concurrent_values):
return None
else:
values.append(token.value)
else:
return None
if values:
return tuple(values)
@property()
def font_feature_settings(tokens):
"""``font-feature-settings`` property validation."""
if len(tokens) == 1 and get_keyword(tokens[0]) == 'normal':
return 'normal'
@comma_separated_list
def font_feature_settings_list(tokens):
feature, value = None, None
if len(tokens) == 2:
tokens, token = tokens[:-1], tokens[-1]
if token.type == 'ident':
value = {'on': 1, 'off': 0}.get(token.value)
elif (token.type == 'number' and
token.int_value is not None and token.int_value >= 0):
value = token.int_value
elif len(tokens) == 1:
value = 1
if len(tokens) == 1:
token, = tokens
if token.type == 'string' and len(token.value) == 4:
if all(0x20 <= ord(letter) <= 0x7f for letter in token.value):
feature = token.value
if feature is not None and value is not None:
return feature, value
return font_feature_settings_list(tokens)
@property()
@single_keyword
def font_variant_alternates(keyword):
# TODO: support other values
# See https://www.w3.org/TR/css-fonts-3/#font-variant-caps-prop
return keyword in ('normal', 'historical-forms')
@property()
def font_variant_east_asian(tokens):
if len(tokens) == 1:
keyword = get_keyword(tokens[0])
if keyword == 'normal':
return keyword
values = []
couples = (
('jis78', 'jis83', 'jis90', 'jis04', 'simplified', 'traditional'),
('full-width', 'proportional-width'),
('ruby',))
all_values = []
for couple in couples:
all_values.extend(couple)
for token in tokens:
if token.type != 'ident':
return None
if token.value in all_values:
concurrent_values = [
couple for couple in couples if token.value in couple][0]
if any(value in values for value in concurrent_values):
return None
else:
values.append(token.value)
else:
return None
if values:
return tuple(values)
@property()
@single_token
def font_size(token):
"""``font-size`` property validation."""
length = get_length(token, negative=False, percentage=True)
if length:
return length
font_size_keyword = get_keyword(token)
if font_size_keyword in ('smaller', 'larger'):
return font_size_keyword
if font_size_keyword in computed_values.FONT_SIZE_KEYWORDS:
return font_size_keyword
@property()
@single_keyword
def font_style(keyword):
"""``font-style`` property validation."""
return keyword in ('normal', 'italic', 'oblique')
@property()
@single_keyword
def font_stretch(keyword):
"""Validation for the ``font-stretch`` property."""
return keyword in (
'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed',
'normal',
'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded')
@property()
@single_token
def font_weight(token):
"""``font-weight`` property validation."""
keyword = get_keyword(token)
if keyword in ('normal', 'bold', 'bolder', 'lighter'):
return keyword
if token.type == 'number' and token.int_value is not None:
if token.int_value in (100, 200, 300, 400, 500, 600, 700, 800, 900):
return token.int_value
@property()
@single_keyword
def object_fit(keyword):
# TODO: Figure out what the spec means by "'scale-down' flag".
# As of this writing, neither Firefox nor chrome support
# anything other than a single keyword as is done here.
return keyword in ('fill', 'contain', 'cover', 'none', 'scale-down')
@property(unstable=True)
@single_token
def image_resolution(token):
# TODO: support 'snap' and 'from-image'
return get_resolution(token)
@property('letter-spacing')
@property('word-spacing')
@single_token
def spacing(token):
"""Validation for ``letter-spacing`` and ``word-spacing``."""
if get_keyword(token) == 'normal':
return 'normal'
length = get_length(token)
if length:
return length
@property()
@single_token
def line_height(token):
"""``line-height`` property validation."""
if get_keyword(token) == 'normal':
return 'normal'
if token.type == 'number' and token.value >= 0:
return Dimension(token.value, None)
if token.type == 'percentage' and token.value >= 0:
return Dimension(token.value, '%')
elif token.type == 'dimension' and token.value >= 0:
return get_length(token)
@property()
@single_keyword
def list_style_position(keyword):
"""``list-style-position`` property validation."""
return keyword in ('inside', 'outside')
@property()
@single_token
def list_style_type(token):
"""``list-style-type`` property validation."""
if token.type == 'ident':
return token.value
elif token.type == 'string':
return ('string', token.value)
elif token.type == 'function' and token.name == 'symbols':
allowed_types = (
'cyclic', 'numeric', 'alphabetic', 'symbolic', 'fixed')
function_arguments = remove_whitespace(token.arguments)
if len(function_arguments) >= 1:
arguments = []
if function_arguments[0].type == 'ident':
if function_arguments[0].value in allowed_types:
index = 1
arguments.append(function_arguments[0].value)
else:
return
else:
arguments.append('symbolic')
index = 0
if len(function_arguments) < index + 1:
return
for i in range(index, len(function_arguments)):
if function_arguments[i].type != 'string':
return
arguments.append(function_arguments[i].value)
if arguments[0] in ('alphabetic', 'numeric'):
if len(arguments) < 3:
return
return ('symbols()', tuple(arguments))
@property('min-width')
@property('min-height')
@single_token
def min_width_height(token):
"""``min-width`` and ``min-height`` properties validation."""
# See https://www.w3.org/TR/css-flexbox-1/#min-size-auto
keyword = get_keyword(token)
if keyword == 'auto':
return keyword
else:
return length_or_precentage([token])
@property('padding-top')
@property('padding-right')
@property('padding-bottom')
@property('padding-left')
@single_token
def length_or_precentage(token):
"""``padding-*`` properties validation."""
length = get_length(token, negative=False, percentage=True)
if length:
return length
@property('max-width')
@property('max-height')