Skip to content

Commit

Permalink
Use parse_blocks_contents()
Browse files Browse the repository at this point in the history
  • Loading branch information
grewn0uille committed Jan 19, 2024
1 parent 9685ab5 commit f4343be
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
12 changes: 6 additions & 6 deletions tests/test_css_descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_font_face_1():
assert at_rule.at_keyword == 'font-face'
font_family, src = list(preprocess_descriptors(
'font-face', 'https://weasyprint.org/foo/',
tinycss2.parse_declaration_list(at_rule.content)))
tinycss2.parse_blocks_contents(at_rule.content)))
assert font_family == ('font_family', 'Gentium Hard')
assert src == (
'src', (('external', 'https://example.com/fonts/Gentium.woff'),))
Expand All @@ -40,7 +40,7 @@ def test_font_face_2():
font_family, src, font_style, font_weight, font_stretch = list(
preprocess_descriptors(
'font-face', 'https://weasyprint.org/foo/',
tinycss2.parse_declaration_list(at_rule.content)))
tinycss2.parse_blocks_contents(at_rule.content)))
assert font_family == ('font_family', 'Fonty Smiley')
assert src == (
'src', (('external', 'https://weasyprint.org/foo/Fonty-Smiley.woff'),))
Expand All @@ -60,7 +60,7 @@ def test_font_face_3():
assert at_rule.at_keyword == 'font-face'
font_family, src = list(preprocess_descriptors(
'font-face', 'https://weasyprint.org/foo/',
tinycss2.parse_declaration_list(at_rule.content)))
tinycss2.parse_blocks_contents(at_rule.content)))
assert font_family == ('font_family', 'Gentium Hard')
assert src == ('src', (('local', None),))

Expand All @@ -77,7 +77,7 @@ def test_font_face_4():
assert at_rule.at_keyword == 'font-face'
font_family, src = list(preprocess_descriptors(
'font-face', 'https://weasyprint.org/foo/',
tinycss2.parse_declaration_list(at_rule.content)))
tinycss2.parse_blocks_contents(at_rule.content)))
assert font_family == ('font_family', 'Gentium Hard')
assert src == ('src', (('local', 'Gentium Hard'),))

Expand All @@ -96,7 +96,7 @@ def test_font_face_5():
with capture_logs() as logs:
font_family, src = list(preprocess_descriptors(
'font-face', 'https://weasyprint.org/foo/',
tinycss2.parse_declaration_list(at_rule.content)))
tinycss2.parse_blocks_contents(at_rule.content)))
assert font_family == ('font_family', 'Gentium Hard')
assert src == ('src', (('local', 'Gentium Hard'),))
assert len(logs) == 1
Expand All @@ -119,7 +119,7 @@ def test_font_face_bad_1():
font_family, src, font_stretch = list(
preprocess_descriptors(
'font-face', 'https://weasyprint.org/foo/',
tinycss2.parse_declaration_list(at_rule.content)))
tinycss2.parse_blocks_contents(at_rule.content)))
assert font_family == ('font_family', 'Bad Font')
assert src == (
'src', (('external', 'https://weasyprint.org/foo/BadFont.woff'),))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_css_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

def expand_to_dict(css, expected_error=None):
"""Helper to test shorthand properties expander functions."""
declarations = tinycss2.parse_declaration_list(css)
declarations = tinycss2.parse_blocks_contents(css)

with capture_logs() as logs:
base_url = 'https://weasyprint.org/foo/'
Expand Down
13 changes: 7 additions & 6 deletions weasyprint/css/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def find_style_attributes(tree, presentational_hints=False, base_url=None):
"""
def check_style_attribute(element, style_attribute):
declarations = tinycss2.parse_declaration_list(style_attribute)
declarations = tinycss2.parse_blocks_contents(style_attribute)
return element, declarations, base_url

for element in tree.iter():
Expand Down Expand Up @@ -941,7 +941,7 @@ def preprocess_stylesheet(device_media_type, base_url, stylesheet_rules,
logger_level = WARNING
selectors_declarations = list(
preprocess_declarations(
base_url, tinycss2.parse_declaration_list(rule.content),
base_url, tinycss2.parse_blocks_contents(rule.content),
rule.prelude))

if selectors_declarations:
Expand All @@ -950,6 +950,7 @@ def preprocess_stylesheet(device_media_type, base_url, stylesheet_rules,
for selectors, declarations in selectors_declarations:
declarations = [
declaration[1] for declaration in declarations]
print(selectors, declarations)
for selector in selectors:
matcher.add_selector(selector, declarations)
if selector.pseudo_element not in PSEUDO_ELEMENTS:
Expand Down Expand Up @@ -1053,7 +1054,7 @@ def preprocess_stylesheet(device_media_type, base_url, stylesheet_rules,
for page_type in data:
specificity = page_type.pop('specificity')
page_type = PageType(**page_type)
content = tinycss2.parse_declaration_list(rule.content)
content = tinycss2.parse_blocks_contents(rule.content)
declarations = list(preprocess_declarations(base_url, content))

if declarations:
Expand All @@ -1066,7 +1067,7 @@ def preprocess_stylesheet(device_media_type, base_url, stylesheet_rules,
continue
declarations = list(preprocess_declarations(
base_url,
tinycss2.parse_declaration_list(margin_rule.content)))
tinycss2.parse_blocks_contents(margin_rule.content)))
if declarations:
selector_list = [(
specificity, f'@{margin_rule.lower_at_keyword}',
Expand All @@ -1076,7 +1077,7 @@ def preprocess_stylesheet(device_media_type, base_url, stylesheet_rules,

elif rule.type == 'at-rule' and rule.lower_at_keyword == 'font-face':
ignore_imports = True
content = tinycss2.parse_declaration_list(rule.content)
content = tinycss2.parse_blocks_contents(rule.content)
rule_descriptors = dict(
preprocess_descriptors('font-face', base_url, content))
for key in ('src', 'font_family'):
Expand All @@ -1103,7 +1104,7 @@ def preprocess_stylesheet(device_media_type, base_url, stylesheet_rules,
continue

ignore_imports = True
content = tinycss2.parse_declaration_list(rule.content)
content = tinycss2.parse_blocks_contents(rule.content)
counter = {
'system': None,
'negative': None,
Expand Down
16 changes: 10 additions & 6 deletions weasyprint/css/validation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


from cssselect2 import SelectorError, compile_selector_list
from tinycss2 import parse_declaration_list, serialize
from tinycss2 import parse_blocks_contents, serialize
from tinycss2.ast import FunctionBlock, LiteralToken

from ... import LOGGER
Expand Down Expand Up @@ -116,6 +116,12 @@ def preprocess_declarations(base_url, declarations, prelude=None):
Return a iterable of ``(name, value, important)`` tuples.
"""
if prelude is not None:
try:
selectors = compile_selector_list(prelude)
except SelectorError as exc:
raise SelectorError(f"'{serialize(prelude)}'")

for declaration in declarations:
if declaration.type == 'error':
LOGGER.warning(
Expand All @@ -124,6 +130,8 @@ def preprocess_declarations(base_url, declarations, prelude=None):
declaration.source_line, declaration.source_column)

if declaration.type == 'qualified-rule':
if prelude is None:
continue
declaration_prelude = declaration.prelude
if LiteralToken(1, 1, '&') in declaration.prelude:
is_token = LiteralToken(1, 1, ':'), FunctionBlock(1, 1, 'is', prelude)
Expand All @@ -134,7 +142,7 @@ def preprocess_declarations(base_url, declarations, prelude=None):
else:
declaration_prelude.append(token)
yield from preprocess_declarations(
base_url, parse_declaration_list(declaration.content),
base_url, parse_blocks_contents(declaration.content),
declaration_prelude)

if declaration.type != 'declaration':
Expand Down Expand Up @@ -200,10 +208,6 @@ def validation_error(level, reason):
important = declaration.important
for long_name, value in result:
if prelude is not None:
try:
selectors = compile_selector_list(prelude)
except SelectorError as exc:
raise SelectorError(f"'{serialize(prelude)}'")
declaration = (long_name.replace('-', '_'), value, important)
yield selectors, declaration
else:
Expand Down
2 changes: 1 addition & 1 deletion weasyprint/svg/css.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def parse_declarations(input):
"""Parse declarations in a given rule content."""
normal_declarations = []
important_declarations = []
for declaration in tinycss2.parse_declaration_list(input):
for declaration in tinycss2.parse_blocks_contents(input):
# TODO: warn on error
# if declaration.type == 'error':
if (declaration.type == 'declaration' and
Expand Down

0 comments on commit f4343be

Please sign in to comment.