Skip to content

Commit

Permalink
Handle 'inherit' value in text-decoration
Browse files Browse the repository at this point in the history
Related to #1007.
  • Loading branch information
liZe committed Dec 14, 2021
1 parent 6d0165a commit b18b67a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
15 changes: 8 additions & 7 deletions tests/test_css_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import pytest
import tinycss2
from tinycss2.color3 import parse_color
from weasyprint.css import preprocess_declarations
from weasyprint.css.computed_values import ZERO_PIXELS
from weasyprint.css.properties import INITIAL_VALUES
Expand Down Expand Up @@ -153,15 +154,15 @@ def test_decoration_style(rule, result):
('text-decoration: none', {'text_decoration_line': 'none'}),
('text-decoration: overline', {'text_decoration_line': {'overline'}}),
('text-decoration: overline blink line-through', {
'text_decoration_line': {'blink', 'line-through', 'overline'}}),
('text-decoration: red', {'text_decoration_color': (1, 0, 0, 1)}),
'text_decoration_line': {'blink', 'line-through', 'overline'},
}),
('text-decoration: red', {'text_decoration_color': parse_color('red')}),
('text-decoration: inherit', {
f'text_decoration_{key}': 'inherit'
for key in ('color', 'line', 'style')}),
))
def test_decoration(rule, result):
default = {
key: value for key, value in INITIAL_VALUES.items()
if key.startswith('text_decoration_')}
real_result = {**default, **result}
assert expand_to_dict(rule) == real_result
assert expand_to_dict(rule) == result


@assert_no_logs
Expand Down
42 changes: 22 additions & 20 deletions weasyprint/css/validation/expanders.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,41 +349,43 @@ def add(name, value):


@expander('text-decoration')
def expand_text_decoration(base_url, name, tokens):
@generic_expander('-line', '-color', '-style')
def expand_text_decoration(name, tokens):
"""Expand the ``text-decoration`` shorthand property."""
text_decoration_line = set()
text_decoration_color = None
text_decoration_style = None
text_decoration_line = []
text_decoration_color = []
text_decoration_style = []
none_in_line = False

for token in tokens:
keyword = get_keyword(token)
if keyword in (
'none', 'underline', 'overline', 'line-through', 'blink'):
text_decoration_line.add(keyword)
text_decoration_line.append(token)
if none_in_line:
raise InvalidValues
elif keyword == 'none':
none_in_line = True
elif keyword in ('solid', 'double', 'dotted', 'dashed', 'wavy'):
if text_decoration_style is not None:
if text_decoration_style:
raise InvalidValues
else:
text_decoration_style = keyword
text_decoration_style.append(token)
else:
color = parse_color(token)
if color is None:
raise InvalidValues
elif text_decoration_color is not None:
elif text_decoration_color:
raise InvalidValues
else:
text_decoration_color = color

if 'none' in text_decoration_line:
if len(text_decoration_line) != 1:
raise InvalidValues
text_decoration_line = 'none'
elif not text_decoration_line:
text_decoration_line = 'none'

yield 'text_decoration_line', text_decoration_line
yield 'text_decoration_color', text_decoration_color or 'currentColor'
yield 'text_decoration_style', text_decoration_style or 'solid'
text_decoration_color.append(token)

if text_decoration_line:
yield '-line', text_decoration_line
if text_decoration_color:
yield '-color', text_decoration_color
if text_decoration_style:
yield '-style', text_decoration_style


@expander('page-break-after')
Expand Down

0 comments on commit b18b67a

Please sign in to comment.