-
-
Notifications
You must be signed in to change notification settings - Fork 710
/
Copy path__init__.py
1146 lines (1024 loc) · 50.4 KB
/
__init__.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
"""Find and apply CSS.
This module takes care of steps 3 and 4 of “CSS 2.1 processing model”: Retrieve
stylesheets associated with a document and annotate every element with a value
for every CSS property.
https://www.w3.org/TR/CSS21/intro.html#processing-model
This module does this in more than two steps. The
:func:`get_all_computed_styles` function does everything, but it is itsef based
on other functions in this module.
"""
from collections import namedtuple
from itertools import groupby
from logging import DEBUG, WARNING
import cssselect2
import tinycss2
import tinycss2.ast
import tinycss2.nth
from .. import CSS
from ..logger import LOGGER, PROGRESS_LOGGER
from ..urls import URLFetchingError, get_url_attribute, url_join
from . import counters, media_queries
from .computed_values import COMPUTER_FUNCTIONS
from .properties import INHERITED, INITIAL_NOT_COMPUTED, INITIAL_VALUES, ZERO_PIXELS
from .validation import preprocess_declarations
from .validation.descriptors import preprocess_descriptors
from .utils import ( # isort:skip
InvalidValues, Pending, check_var_function, get_url, parse_function,
remove_whitespace)
# Reject anything not in here:
PSEUDO_ELEMENTS = (
None, 'before', 'after', 'marker', 'first-line', 'first-letter',
'footnote-call', 'footnote-marker')
PageSelectorType = namedtuple(
'PageSelectorType', ['side', 'blank', 'first', 'index', 'name'])
class StyleFor:
"""Convenience function to get the computed styles for an element."""
def __init__(self, html, sheets, presentational_hints, target_collector):
# keys: (element, pseudo_element_type)
# element: an ElementTree Element or the '@page' string
# pseudo_element_type: a string such as 'first' (for @page) or
# 'after', or None for normal elements
# values: dicts of
# keys: property name as a string
# values: (values, weight)
# values: a PropertyValue-like object
# weight: values with a greater weight take precedence, see
# https://www.w3.org/TR/CSS21/cascade.html#cascading-order
self._cascaded_styles = cascaded_styles = {}
# keys: (element, pseudo_element_type), like cascaded_styles
# values: style dict objects:
# keys: property name as a string
# values: a PropertyValue-like object
self._computed_styles = {}
self._sheets = sheets
PROGRESS_LOGGER.info('Step 3 - Applying CSS')
for specificity, attributes in find_style_attributes(
html.etree_element, presentational_hints, html.base_url):
element, declarations, base_url = attributes
style = cascaded_styles.setdefault((element, None), {})
for name, values, importance in preprocess_declarations(
base_url, declarations):
precedence = declaration_precedence('author', importance)
weight = (precedence, specificity)
old_weight = style.get(name, (None, None))[1]
if old_weight is None or old_weight <= weight:
style[name] = values, weight
# First, add declarations and set computed styles for "real" elements
# *in tree order*. Tree order is important so that parents have
# computed styles before their children, for inheritance.
# Iterate on all elements, even if there is no cascaded style for them.
for element in html.wrapper_element.iter_subtree():
for sheet, origin, sheet_specificity in sheets:
# Add declarations for matched elements
for selector in sheet.matcher.match(element):
specificity, order, pseudo_type, declarations = selector
specificity = sheet_specificity or specificity
style = cascaded_styles.setdefault(
(element.etree_element, pseudo_type), {})
for name, values, importance in declarations:
precedence = declaration_precedence(origin, importance)
weight = (precedence, specificity)
old_weight = style.get(name, (None, None))[1]
if old_weight is None or old_weight <= weight:
style[name] = values, weight
parent = element.parent.etree_element if element.parent else None
self.set_computed_styles(
element.etree_element, root=html.etree_element, parent=parent,
base_url=html.base_url, target_collector=target_collector)
# Then computed styles for pseudo elements, in any order.
# Pseudo-elements inherit from their associated element so they come
# last. Do them in a second pass as there is no easy way to iterate
# on the pseudo-elements for a given element with the current structure
# of cascaded_styles. (Keys are (element, pseudo_type) tuples.)
# Only iterate on pseudo-elements that have cascaded styles. (Others
# might as well not exist.)
for element, pseudo_type in cascaded_styles:
if pseudo_type:
self.set_computed_styles(
element, pseudo_type=pseudo_type,
# The pseudo-element inherits from the element.
root=html.etree_element, parent=element,
base_url=html.base_url, target_collector=target_collector)
# Clear the cascaded styles, we don't need them anymore. Keep the
# dictionary, it is used later for page margins.
self._cascaded_styles.clear()
def __call__(self, element, pseudo_type=None):
if style := self._computed_styles.get((element, pseudo_type)):
if 'table' in style['display'] and style['border_collapse'] == 'collapse':
# Padding does not apply.
for side in ('top', 'bottom', 'left', 'right'):
style[f'padding_{side}'] = ZERO_PIXELS
if len(style['display']) == 1:
display, = style['display']
if display.startswith('table-') and display != 'table-caption':
# Margins do not apply.
for side in ('top', 'bottom', 'left', 'right'):
style[f'margin_{side}'] = ZERO_PIXELS
return style
def set_computed_styles(self, element, parent, root=None, pseudo_type=None,
base_url=None, target_collector=None):
"""Set the computed values of styles to ``element``.
Take the properties left by ``apply_style_rule`` on an element or
pseudo-element and assign computed values with respect to the cascade,
declaration priority (ie. ``!important``) and selector specificity.
"""
cascaded_styles = self.get_cascaded_styles()
computed_styles = self.get_computed_styles()
if element == root and pseudo_type is None:
assert parent is None
parent_style = None
root_style = {
# When specified on the font-size property of the root element,
# the rem units refer to the property’s initial value.
'font_size': INITIAL_VALUES['font_size'],
}
else:
assert parent is not None
parent_style = computed_styles[parent, None]
root_style = computed_styles[root, None]
cascaded = cascaded_styles.get((element, pseudo_type), {})
computed_styles[element, pseudo_type] = computed_from_cascaded(
element, cascaded, parent_style, pseudo_type, root_style, base_url,
target_collector)
def add_page_declarations(self, page_type):
for sheet, origin, sheet_specificity in self._sheets:
for _rule, selector_list, declarations in sheet.page_rules:
for selector in selector_list:
specificity, pseudo_type, page_selector_type = selector
if self._page_type_match(page_selector_type, page_type):
specificity = sheet_specificity or specificity
style = self._cascaded_styles.setdefault(
(page_type, pseudo_type), {})
for name, values, importance in declarations:
precedence = declaration_precedence(origin, importance)
weight = (precedence, specificity)
old_weight = style.get(name, (None, None))[1]
if old_weight is None or old_weight <= weight:
style[name] = values, weight
def get_cascaded_styles(self):
return self._cascaded_styles
def get_computed_styles(self):
return self._computed_styles
@staticmethod
def _page_type_match(page_selector_type, page_type):
if page_selector_type.side not in (None, page_type.side):
return False
if page_selector_type.blank not in (None, page_type.blank):
return False
if page_selector_type.first not in (None, page_type.index == 0):
return False
if page_selector_type.name not in (None, page_type.name):
return False
if page_selector_type.index is not None:
a, b, name = page_selector_type.index
if name is None:
index = page_type.index
offset = index + 1 - b
return offset == 0 if a == 0 else (offset / a >= 0 and not offset % a)
if name != page_type.name:
return False
for group_name, index in page_type.groups:
if name != group_name:
continue
offset = index + 1 - b
if (offset == 0 if a == 0 else (offset / a >= 0 and not offset % a)):
return True
return False
return True
def get_child_text(element):
"""Return the text directly in the element, not descendants."""
content = [element.text] if element.text else []
for child in element:
if child.tail:
content.append(child.tail)
return ''.join(content)
def text_decoration(key, value, parent_value, cascaded):
# The text-decoration-* properties are not inherited but propagated
# using specific rules.
# See https://drafts.csswg.org/css-text-decor-3/#line-decoration
# TODO: these rules don’t follow the specification.
text_properties = (
'text_decoration_color', 'text_decoration_style', 'text_decoration_thickness')
if key in text_properties:
if not cascaded:
value = parent_value
elif key == 'text_decoration_line':
if parent_value != 'none':
if value == 'none':
value = parent_value
else:
value = value | parent_value
return value
def find_stylesheets(wrapper_element, device_media_type, url_fetcher, base_url,
font_config, counter_style, page_rules):
"""Yield the stylesheets in ``element_tree``.
The output order is the same as the source order.
"""
from ..html import element_has_link_type
for wrapper in wrapper_element.query_all('style', 'link'):
element = wrapper.etree_element
mime_type = element.get('type', 'text/css').split(';', 1)[0].strip()
# Only keep 'type/subtype' from 'type/subtype ; param1; param2'.
if mime_type != 'text/css':
continue
media_attr = element.get('media', '').strip() or 'all'
media = [media_type.strip() for media_type in media_attr.split(',')]
if not media_queries.evaluate_media_query(media, device_media_type):
continue
if element.tag == 'style':
# Content is text that is directly in the <style> element, not its
# descendants
content = get_child_text(element)
# ElementTree should give us either unicode or ASCII-only
# bytestrings, so we don't need `encoding` here.
css = CSS(
string=content, base_url=base_url,
url_fetcher=url_fetcher, media_type=device_media_type,
font_config=font_config, counter_style=counter_style,
page_rules=page_rules)
yield css
elif element.tag == 'link' and element.get('href'):
if not element_has_link_type(element, 'stylesheet') or \
element_has_link_type(element, 'alternate'):
continue
href = get_url_attribute(element, 'href', base_url)
if href is not None:
try:
yield CSS(
url=href, url_fetcher=url_fetcher,
_check_mime_type=True, media_type=device_media_type,
font_config=font_config, counter_style=counter_style,
page_rules=page_rules)
except URLFetchingError as exception:
LOGGER.error('Failed to load stylesheet at %s: %s', href, exception)
LOGGER.debug('Error while loading stylesheet:', exc_info=exception)
def find_style_attributes(tree, presentational_hints=False, base_url=None):
"""Yield ``specificity, (element, declaration, base_url)`` rules.
Rules from "style" attribute are returned with specificity
``(1, 0, 0)``.
If ``presentational_hints`` is ``True``, rules from presentational hints
are returned with specificity ``(0, 0, 0)``.
"""
def check_style_attribute(element, style_attribute):
declarations = tinycss2.parse_blocks_contents(style_attribute)
return element, declarations, base_url
for element in tree.iter():
specificity = (1, 0, 0)
style_attribute = element.get('style')
if style_attribute:
yield specificity, check_style_attribute(element, style_attribute)
if not presentational_hints:
continue
specificity = (0, 0, 0)
if element.tag == 'body':
# TODO: we should check the container frame element
for part, position in (
('height', 'top'), ('height', 'bottom'),
('width', 'left'), ('width', 'right')):
style_attribute = None
for prop in (f'margin{part}', f'{position}margin'):
if element.get(prop):
style_attribute = f'margin-{position}:{element.get(prop)}px'
break
if style_attribute:
yield specificity, check_style_attribute(element, style_attribute)
if element.get('background'):
style_attribute = f'background-image:url({element.get("background")})'
yield specificity, check_style_attribute(element, style_attribute)
if element.get('bgcolor'):
style_attribute = f'background-color:{element.get("bgcolor")}'
yield specificity, check_style_attribute(element, style_attribute)
if element.get('text'):
style_attribute = f'color:{element.get("text")}'
yield specificity, check_style_attribute(element, style_attribute)
# TODO: we should support link, vlink, alink
elif element.tag == 'center':
yield specificity, check_style_attribute(element, 'text-align:center')
elif element.tag == 'div':
align = element.get('align', '').lower()
if align == 'middle':
yield specificity, check_style_attribute(element, 'text-align:center')
elif align in ('center', 'left', 'right', 'justify'):
yield specificity, check_style_attribute(element, f'text-align:{align}')
elif element.tag == 'font':
if element.get('color'):
yield specificity, check_style_attribute(
element, f'color:{element.get("color")}')
if element.get('face'):
yield specificity, check_style_attribute(
element, f'font-family:{element.get("face")}')
if element.get('size'):
size = element.get('size').strip()
relative_plus = size.startswith('+')
relative_minus = size.startswith('-')
if relative_plus or relative_minus:
size = size[1:].strip()
try:
size = int(size)
except ValueError:
LOGGER.warning('Invalid value for size: %s', size)
else:
font_sizes = {
1: 'x-small',
2: 'small',
3: 'medium',
4: 'large',
5: 'x-large',
6: 'xx-large',
7: '48px', # 1.5 * xx-large
}
if relative_plus:
size += 3
elif relative_minus:
size -= 3
size = max(1, min(7, size))
yield specificity, check_style_attribute(
element, f'font-size:{font_sizes[size]}')
elif element.tag == 'table':
if element.get('cellspacing'):
yield specificity, check_style_attribute(
element, f'border-spacing:{element.get("cellspacing")}px')
if element.get('cellpadding'):
cellpadding = element.get('cellpadding')
if cellpadding.isdigit():
cellpadding += 'px'
# TODO: don't match subtables cells
for subelement in element.iter():
if subelement.tag in ('td', 'th'):
yield specificity, check_style_attribute(
subelement,
f'padding-left:{cellpadding};'
f'padding-right:{cellpadding};'
f'padding-top:{cellpadding};'
f'padding-bottom:{cellpadding};')
if element.get('hspace'):
hspace = element.get('hspace')
if hspace.isdigit():
hspace += 'px'
yield specificity, check_style_attribute(
element, f'margin-left:{hspace};margin-right:{hspace}')
if element.get('vspace'):
vspace = element.get('vspace')
if vspace.isdigit():
vspace += 'px'
yield specificity, check_style_attribute(
element, f'margin-top:{vspace};margin-bottom:{vspace}')
if element.get('width'):
style_attribute = f'width:{element.get("width")}'
if element.get('width').isdigit():
style_attribute += 'px'
yield specificity, check_style_attribute(element, style_attribute)
if element.get('height'):
style_attribute = f'height:{element.get("height")}'
if element.get('height').isdigit():
style_attribute += 'px'
yield specificity, check_style_attribute(element, style_attribute)
if element.get('background'):
style_attribute = (
f'background-image:url({element.get("background")})')
yield specificity, check_style_attribute(element, style_attribute)
if element.get('bgcolor'):
style_attribute = f'background-color:{element.get("bgcolor")}'
yield specificity, check_style_attribute(element, style_attribute)
if element.get('bordercolor'):
style_attribute = f'border-color:{element.get("bordercolor")}'
yield specificity, check_style_attribute(element, style_attribute)
if element.get('border'):
style_attribute = f'border-width:{element.get("border")}px'
yield specificity, check_style_attribute(element, style_attribute)
elif element.tag in ('tr', 'td', 'th', 'thead', 'tbody', 'tfoot'):
align = element.get('align', '').lower()
# TODO: we should align descendants too
if align == 'middle':
yield specificity, check_style_attribute(
element, 'text-align:center')
elif align in ('center', 'left', 'right', 'justify'):
yield specificity, check_style_attribute(element, f'text-align:{align}')
if element.get('background'):
style_attribute = f'background-image:url({element.get("background")})'
yield specificity, check_style_attribute(element, style_attribute)
if element.get('bgcolor'):
style_attribute = f'background-color:{element.get("bgcolor")}'
yield specificity, check_style_attribute(element, style_attribute)
if element.tag in ('tr', 'td', 'th'):
if element.get('height'):
style_attribute = f'height:{element.get("height")}'
if element.get('height').isdigit():
style_attribute += 'px'
yield specificity, check_style_attribute(element, style_attribute)
if element.tag in ('td', 'th'):
if element.get('width'):
style_attribute = f'width:{element.get("width")}'
if element.get('width').isdigit():
style_attribute += 'px'
yield specificity, check_style_attribute(
element, style_attribute)
elif element.tag == 'caption':
align = element.get('align', '').lower()
# TODO: we should align descendants too
if align == 'middle':
yield specificity, check_style_attribute(element, 'text-align:center')
elif align in ('center', 'left', 'right', 'justify'):
yield specificity, check_style_attribute(element, f'text-align:{align}')
elif element.tag == 'col':
if element.get('width'):
style_attribute = f'width:{element.get("width")}'
if element.get('width').isdigit():
style_attribute += 'px'
yield specificity, check_style_attribute(element, style_attribute)
elif element.tag == 'hr':
size = 0
if element.get('size'):
try:
size = int(element.get('size'))
except ValueError:
LOGGER.warning('Invalid value for size: %s', size)
if (element.get('color'), element.get('noshade')) != (None, None):
if size >= 1:
yield specificity, check_style_attribute(
element, f'border-width:{size / 2}px')
elif size == 1:
yield specificity, check_style_attribute(
element, 'border-bottom-width:0')
elif size > 1:
yield specificity, check_style_attribute(
element, f'height:{size - 2}px')
if element.get('width'):
style_attribute = f'width:{element.get("width")}'
if element.get('width').isdigit():
style_attribute += 'px'
yield specificity, check_style_attribute(element, style_attribute)
if element.get('color'):
yield specificity, check_style_attribute(
element, f'color:{element.get("color")}')
elif element.tag in (
'iframe', 'applet', 'embed', 'img', 'input', 'object'):
if (element.tag != 'input' or
element.get('type', '').lower() == 'image'):
align = element.get('align', '').lower()
if align in ('middle', 'center'):
# TODO: middle and center values are wrong
yield specificity, check_style_attribute(
element, 'vertical-align:middle')
if element.get('hspace'):
hspace = element.get('hspace')
if hspace.isdigit():
hspace += 'px'
yield specificity, check_style_attribute(
element, f'margin-left:{hspace};margin-right:{hspace}')
if element.get('vspace'):
vspace = element.get('vspace')
if vspace.isdigit():
vspace += 'px'
yield specificity, check_style_attribute(
element, f'margin-top:{vspace};margin-bottom:{vspace}')
# TODO: img seems to be excluded for width and height, but a
# lot of W3C tests rely on this attribute being applied to img
if element.get('width'):
style_attribute = f'width:{element.get("width")}'
if element.get('width').isdigit():
style_attribute += 'px'
yield specificity, check_style_attribute(element, style_attribute)
if element.get('height'):
style_attribute = f'height:{element.get("height")}'
if element.get('height').isdigit():
style_attribute += 'px'
yield specificity, check_style_attribute(element, style_attribute)
if element.tag in ('img', 'object', 'input'):
if element.get('border'):
yield specificity, check_style_attribute(
element,
f'border-width:{element.get("border")}px;'
f'border-style:solid')
elif element.tag == 'ol':
# From https://www.w3.org/TR/css-lists-3/#ua-stylesheet
if element.get('start'):
yield specificity, check_style_attribute(
element,
f'counter-reset:list-item {element.get("start")};'
'counter-increment:list-item -1')
elif element.tag == 'li':
# From https://www.w3.org/TR/css-lists-3/#ua-stylesheet
if element.get('value'):
yield specificity, check_style_attribute(
element,
f'counter-reset:list-item {element.get("value")};'
'counter-increment:none')
def declaration_precedence(origin, importance):
"""Return the precedence for a declaration.
Precedence values have no meaning unless compared to each other.
Acceptable values for ``origin`` are the strings ``'author'``, ``'user'``
and ``'user agent'``.
"""
# See https://www.w3.org/TR/CSS21/cascade.html#cascading-order
if origin == 'user agent':
return 1
elif origin == 'user' and not importance:
return 2
elif origin == 'author' and not importance:
return 3
elif origin == 'author': # and importance
return 4
else:
assert origin == 'user' # and importance
return 5
def resolve_var(computed, token, parent_style):
"""Return token with resolved CSS variables."""
if not check_var_function(token):
return
if token.lower_name != 'var':
arguments = []
for i, argument in enumerate(token.arguments):
if argument.type == 'function':
arguments.extend(resolve_var(computed, argument, parent_style))
else:
arguments.append(argument)
token = tinycss2.ast.FunctionBlock(
token.source_line, token.source_column, token.name, arguments)
return resolve_var(computed, token, parent_style) or (token,)
args = parse_function(token)[1]
variable_name = args.pop(0).value.replace('-', '_') # first arg is name
default = args # next args are default value
computed_value = []
for value in (computed[variable_name] or default):
resolved = resolve_var(computed, value, parent_style)
computed_value.extend((value,) if resolved is None else resolved)
return computed_value
class AnonymousStyle(dict):
"""Computed style used for anonymous boxes."""
def __init__(self, parent_style):
# border-*-style is none, so border-width computes to zero.
# Other than that, properties that would need computing are
# border-*-color, but they do not apply.
self.update({
'border_top_width': 0,
'border_bottom_width': 0,
'border_left_width': 0,
'border_right_width': 0,
'outline_width': 0,
})
self.parent_style = parent_style
self.specified = self
if parent_style:
self.cache = parent_style.cache
else:
self.cache = {'ratio_ch': {}, 'ratio_ex': {}}
def copy(self):
copy = AnonymousStyle(self.parent_style)
copy.update(self)
return copy
def __missing__(self, key):
if key in INHERITED or key[:2] == '__':
value = self[key] = self.parent_style[key]
elif key == 'page':
# page is not inherited but taken from the ancestor if 'auto'
value = self[key] = self.parent_style[key]
elif key[:16] == 'text_decoration_':
value = self[key] = text_decoration(
key, INITIAL_VALUES[key], self.parent_style[key], cascaded=False)
else:
value = self[key] = INITIAL_VALUES[key]
return value
class ComputedStyle(dict):
"""Computed style used for non-anonymous boxes."""
def __init__(self, parent_style, cascaded, element, pseudo_type,
root_style, base_url):
self.specified = {}
self.parent_style = parent_style
self.cascaded = cascaded
self.is_root_element = parent_style is None
self.element = element
self.pseudo_type = pseudo_type
self.root_style = root_style
self.base_url = base_url
if parent_style:
self.cache = parent_style.cache
else:
self.cache = {'ratio_ch': {}, 'ratio_ex': {}}
def copy(self):
copy = ComputedStyle(
self.parent_style, self.cascaded, self.element, self.pseudo_type,
self.root_style, self.base_url)
copy.update(self)
copy.specified = self.specified.copy()
return copy
def __missing__(self, key):
if key == 'float':
# Set specified value for position, needed for computed value.
self['position']
elif key == 'display':
# Set specified value for float, needed for computed value.
self['float']
parent_style = self.parent_style
if key in self.cascaded:
# Property defined in cascaded properties.
value = self.cascaded[key][0]
pending = isinstance(value, Pending)
else:
# Property not defined in cascaded properties, define as inherited
# or initial value.
if key in INHERITED or key[:2] == '__':
value = 'inherit'
else:
value = 'initial'
pending = False
if value == 'inherit' and parent_style is None:
# On the root element, 'inherit' from initial values
value = 'initial'
if pending:
# Property with pending values, validate them.
solved_tokens = []
for token in value.tokens:
tokens = resolve_var(self, token, parent_style)
if tokens is None:
solved_tokens.append(token)
else:
solved_tokens.extend(tokens)
original_key = key.replace('_', '-')
try:
value = value.solve(solved_tokens, original_key)
except InvalidValues:
if key in INHERITED and parent_style is not None:
# Values in parent_style are already computed.
self[key] = value = parent_style[key]
else:
value = INITIAL_VALUES[key]
if key not in INITIAL_NOT_COMPUTED:
# The value is the same as when computed.
self[key] = value
if value == 'initial':
value = [] if key[:2] == '__' else INITIAL_VALUES[key]
if key not in INITIAL_NOT_COMPUTED:
# The value is the same as when computed.
self[key] = value
elif value == 'inherit':
# Values in parent_style are already computed.
self[key] = value = parent_style[key]
if key[:16] == 'text_decoration_' and parent_style is not None:
# Text decorations are not inherited but propagated. See
# https://www.w3.org/TR/css-text-decor-3/#line-decoration.
value = text_decoration(key, value, parent_style[key], key in self.cascaded)
if key in self:
del self[key]
elif key == 'page' and value == 'auto':
# The page property does not inherit. However, if the page value on
# an element is auto, then its used value is the value specified on
# its nearest ancestor with a non-auto value. When specified on the
# root element, the used value for auto is the empty string. See
# https://www.w3.org/TR/css-page-3/#using-named-pages.
value = '' if parent_style is None else parent_style['page']
if key in self:
del self[key]
elif key in ('position', 'float', 'display'):
# Save specified values to define computed values for these
# specific properties. See
# https://www.w3.org/TR/CSS21/visuren.html#dis-pos-flo.
self.specified[key] = value
if key in self:
# Value already computed and saved: return.
return self[key]
if key in COMPUTER_FUNCTIONS:
# Value not computed yet: compute.
value = COMPUTER_FUNCTIONS[key](self, key, value)
self[key] = value
return value
def computed_from_cascaded(element, cascaded, parent_style, pseudo_type=None,
root_style=None, base_url=None,
target_collector=None):
"""Get a dict of computed style mixed from parent and cascaded styles."""
if not cascaded and parent_style is not None:
return AnonymousStyle(parent_style)
style = ComputedStyle(
parent_style, cascaded, element, pseudo_type, root_style, base_url)
if target_collector and style['anchor']:
target_collector.collect_anchor(style['anchor'])
return style
def parse_page_selectors(rule):
"""Parse a page selector rule.
Return a list of page data if the rule is correctly parsed. Page data are a
dict containing:
- 'side' ('left', 'right' or None),
- 'blank' (True or None),
- 'first' (True or None),
- 'index' (page number or None),
- 'name' (page name string or None), and
- 'specificity' (list of numbers).
Return ``None` if something went wrong while parsing the rule.
"""
# See https://drafts.csswg.org/css-page-3/#syntax-page-selector
tokens = list(remove_whitespace(rule.prelude))
page_data = []
# TODO: Specificity is probably wrong, should clean and test that.
if not tokens:
page_data.append({
'side': None, 'blank': None, 'first': None, 'index': None,
'name': None, 'specificity': [0, 0, 0]})
return page_data
while tokens:
types = {
'side': None, 'blank': None, 'first': None, 'index': None,
'name': None, 'specificity': [0, 0, 0]}
if tokens[0].type == 'ident':
token = tokens.pop(0)
types['name'] = token.value
types['specificity'][0] = 1
if len(tokens) == 1:
return None
elif not tokens:
page_data.append(types)
return page_data
while tokens:
literal = tokens.pop(0)
if literal.type != 'literal':
return None
if literal.value == ':':
if not tokens:
return None
if tokens[0].type == 'ident':
ident = tokens.pop(0)
pseudo_class = ident.lower_value
if pseudo_class in ('left', 'right'):
if types['side'] and types['side'] != pseudo_class:
return None
types['side'] = pseudo_class
types['specificity'][2] += 1
continue
elif pseudo_class in ('blank', 'first'):
types[pseudo_class] = True
types['specificity'][1] += 1
continue
elif tokens[0].type == 'function':
function = tokens.pop(0)
if function.name != 'nth':
return None
for i, argument in enumerate(function.arguments):
if argument.type == 'ident' and argument.value == 'of':
nth = function.arguments[:i - 1]
group = function.arguments[i + 1:]
break
else:
nth = function.arguments
group = None
nth_values = tinycss2.nth.parse_nth(nth)
if nth_values is None:
return None
if group is not None:
group = [
token for token in group
if token.type not in ('comment', 'whitespace')]
if len(group) != 1:
return None
group, = group
if group.type != 'ident':
return None
group = group.value
types['index'] = (*nth_values, group)
# TODO: specificity is not specified yet
# https://github.com/w3c/csswg-drafts/issues/3524
types['specificity'][1] += 1
if group:
types['specificity'][0] += 1
continue
return None
elif literal.value == ',':
if tokens and any(types['specificity']):
break
else:
return None
page_data.append(types)
return page_data
def preprocess_stylesheet(device_media_type, base_url, stylesheet_rules, url_fetcher,
matcher, page_rules, font_config, counter_style,
ignore_imports=False):
"""Do what can be done early on stylesheet, before being in a document."""
for rule in stylesheet_rules:
if getattr(rule, 'content', None) is None:
if rule.type == 'error':
LOGGER.warning(
"Parse error at %d:%d: %s",
rule.source_line, rule.source_column, rule.message)
if rule.type != 'at-rule':
continue
if rule.lower_at_keyword != 'import':
LOGGER.warning(
"Unknown empty rule %s at %d:%d",
rule, rule.source_line, rule.source_column)
continue
if rule.type == 'qualified-rule':
try:
logger_level = WARNING
contents = tinycss2.parse_blocks_contents(rule.content)
selectors_declarations = list(
preprocess_declarations(base_url, contents, rule.prelude))
if selectors_declarations:
selectors_declarations = groupby(
selectors_declarations, key=lambda x: x[0])
for selectors, declarations in selectors_declarations:
declarations = [
declaration[1] for declaration in declarations]
for selector in selectors:
matcher.add_selector(selector, declarations)
if selector.pseudo_element not in PSEUDO_ELEMENTS:
prelude = tinycss2.serialize(rule.prelude)
if selector.pseudo_element.startswith('-'):
logger_level = DEBUG
raise cssselect2.SelectorError(
f"'{prelude}', "
'ignored prefixed pseudo-element: '
f'{selector.pseudo_element}')
else:
raise cssselect2.SelectorError(
f"'{prelude}', "
'unknown pseudo-element: '
f'{selector.pseudo_element}')
ignore_imports = True
else:
ignore_imports = True
except cssselect2.SelectorError as exc:
LOGGER.log(logger_level, 'Invalid or unsupported selector, %s', exc)
continue
elif rule.type == 'at-rule' and rule.lower_at_keyword == 'import':
if ignore_imports:
LOGGER.warning(
'@import rule %r not at the beginning of the '
'the whole rule was ignored at %d:%d.',
tinycss2.serialize(rule.prelude),
rule.source_line, rule.source_column)
continue
tokens = remove_whitespace(rule.prelude)
url = None
if tokens:
if tokens[0].type == 'string':
url = url_join(
base_url, tokens[0].value, allow_relative=False,
context='@import at %s:%s',
context_args=(rule.source_line, rule.source_column))
else:
url_tuple = get_url(tokens[0], base_url)
if url_tuple and url_tuple[1][0] == 'external':
url = url_tuple[1][1]
if url is None:
continue
media = media_queries.parse_media_query(tokens[1:])
if media is None:
LOGGER.warning(
'Invalid media type %r '
'the whole @import rule was ignored at %d:%d.',
tinycss2.serialize(rule.prelude),
rule.source_line, rule.source_column)
continue
if not media_queries.evaluate_media_query(media, device_media_type):
continue
if url is not None:
try:
CSS(
url=url, url_fetcher=url_fetcher, media_type=device_media_type,
font_config=font_config, counter_style=counter_style,
matcher=matcher, page_rules=page_rules)
except URLFetchingError as exception:
LOGGER.error('Failed to load stylesheet at %s : %s', url, exception)
LOGGER.debug('Error while loading stylesheet:', exc_info=exception)
elif rule.type == 'at-rule' and rule.lower_at_keyword == 'media':
media = media_queries.parse_media_query(rule.prelude)
if media is None:
LOGGER.warning(
'Invalid media type %r '
'the whole @media rule was ignored at %d:%d.',
tinycss2.serialize(rule.prelude),
rule.source_line, rule.source_column)
continue
ignore_imports = True
if not media_queries.evaluate_media_query(media, device_media_type):
continue
content_rules = tinycss2.parse_rule_list(rule.content)
preprocess_stylesheet(
device_media_type, base_url, content_rules, url_fetcher, matcher,
page_rules, font_config, counter_style, ignore_imports=True)