Skip to content

Commit

Permalink
Use default value when variable is not defined
Browse files Browse the repository at this point in the history
Fix #2075.
  • Loading branch information
liZe committed Feb 16, 2024
1 parent a42c406 commit 9405a84
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
63 changes: 61 additions & 2 deletions tests/test_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@ def test_variable_inherit_override():
assert paragraph.width == 10


@assert_no_logs
def test_variable_default_unknown():
page, = render_pages('''
<style>
p { width: var(--x, 10px) }
</style>
<p></p>
''')
html, = page.children
body, = html.children
paragraph, = body.children
assert paragraph.width == 10


@assert_no_logs
def test_variable_default_var():
page, = render_pages('''
<style>
p { --var: 10px; width: var(--x, var(--var)) }
</style>
<p></p>
''')
html, = page.children
body, = html.children
paragraph, = body.children
assert paragraph.width == 10


@assert_no_logs
def test_variable_case_sensitive():
page, = render_pages('''
Expand Down Expand Up @@ -304,17 +332,48 @@ def test_variable_shorthand_background_invalid(var, background):

@assert_no_logs
def test_variable_initial():
# Regression test for https://github.com/Kozea/WeasyPrint/issues/2075
page, = render_pages('''
<style>
html { --var: initial }
p { width: var(--var, 10px) }
p { width: var(--var) }
</style>
<p></p>
''')
html, = page.children
body, = html.children
paragraph, = body.children
assert paragraph.width == 10
assert paragraph.width == body.width


@assert_no_logs
def test_variable_initial_default():
# Regression test for https://github.com/Kozea/WeasyPrint/issues/2075
page, = render_pages('''
<style>
p { --var: initial; width: var(--var, 10px) }
</style>
<p></p>
''')
html, = page.children
body, = html.children
paragraph, = body.children
assert paragraph.width == body.width


@assert_no_logs
def test_variable_initial_default_var():
# Regression test for https://github.com/Kozea/WeasyPrint/issues/2075
page, = render_pages('''
<style>
p { --var: initial; width: var(--var, var(--var)) }
</style>
<p></p>
''')
html, = page.children
body, = html.children
paragraph, = body.children
assert paragraph.width == body.width


@pytest.mark.parametrize('prop', sorted(KNOWN_PROPERTIES))
Expand Down
6 changes: 1 addition & 5 deletions weasyprint/css/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,13 +613,9 @@ def resolve_var(computed, token, parent_style):
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]:
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)
if len(computed_value) == 1:
token, = computed_value
if token.type == 'ident' and token.value == 'initial':
return default
return computed_value


Expand Down

0 comments on commit 9405a84

Please sign in to comment.