Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for mask-border-* properties #2139

Merged
merged 4 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions tests/css/test_expanders.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,74 @@ def test_border_image_invalid(rule, reason):
assert_invalid(f'border-image: {rule}', reason)


@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('url(border.png) 27', {
'mask_border_source': ('url', 'https://weasyprint.org/foo/border.png'),
'mask_border_slice': ((27, None),),
}),
('url(border.png) 10 / 4 / 2 round stretch', {
'mask_border_source': ('url', 'https://weasyprint.org/foo/border.png'),
'mask_border_slice': ((10, None),),
'mask_border_width': ((4, None),),
'mask_border_outset': ((2, None),),
'mask_border_repeat': (('round', 'stretch')),
}),
('10 // 2', {
'mask_border_slice': ((10, None),),
'mask_border_outset': ((2, None),),
}),
('5.5%', {
'mask_border_slice': ((5.5, '%'),),
}),
('stretch 2 url("border.png")', {
'mask_border_source': ('url', 'https://weasyprint.org/foo/border.png'),
'mask_border_slice': ((2, None),),
'mask_border_repeat': (('stretch',)),
}),
('1/2 round', {
'mask_border_slice': ((1, None),),
'mask_border_width': ((2, None),),
'mask_border_repeat': (('round',)),
}),
('none', {
'mask_border_source': ('none', None),
}),
('url(border.png) 27 alpha', {
'mask_border_source': ('url', 'https://weasyprint.org/foo/border.png'),
'mask_border_slice': ((27, None),),
'mask_border_mode': 'alpha',
}),
('url(border.png) 27 luminance', {
'mask_border_source': ('url', 'https://weasyprint.org/foo/border.png'),
'mask_border_slice': ((27, None),),
'mask_border_mode': 'luminance',
}),
))
def test_mask_border(rule, result):
assert expand_to_dict(f'mask-border: {rule}') == result


@assert_no_logs
@pytest.mark.parametrize('rule, reason', (
('url(border.png) url(border.png)', 'multiple source'),
('10 10 10 10 10', 'multiple slice'),
('1 / 2 / 3 / 4', 'invalid'),
('/1', 'invalid'),
('/1', 'invalid'),
('round round round', 'invalid'),
('-1', 'invalid'),
('1 repeat 2', 'multiple slice'),
('1% // 1%', 'invalid'),
('1 / repeat', 'invalid'),
('', 'no value'),
('alpha alpha', 'multiple mode'),
('alpha luminance', 'multiple mode'),
))
def test_mask_border_invalid(rule, reason):
assert_invalid(f'mask-border: {rule}', reason)


@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('12px My Fancy Font, serif', {
Expand Down
129 changes: 129 additions & 0 deletions tests/css/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,135 @@ def test_border_image_repeat_invalid(rule):
assert_invalid(f'border-image-repeat: {rule}')


@assert_no_logs
@pytest.mark.parametrize('rule, value', (
('1', ((1, None),)),
('1 2 3 4', ((1, None), (2, None), (3, None), (4, None))),
('50% 1000.1 0', ((50, '%'), (1000.1, None), (0, None))),
('1% 2% 3% 4%', ((1, '%'), (2, '%'), (3, '%'), (4, '%'))),
('fill 10% 20', ('fill', (10, '%'), (20, None))),
('0 1 0.5 fill', ((0, None), (1, None), (0.5, None), 'fill')),
))
def test_mask_border_slice(rule, value):
assert get_value(f'mask-border-slice: {rule}') == value


@assert_no_logs
@pytest.mark.parametrize('rule', (
'none',
'1, 2',
'-10',
'-10%',
'1 2 3 -10%',
'-0.3',
'1 fill 2',
'fill 1 2 3 fill',
))
def test_mask_border_slice_invalid(rule):
assert_invalid(f'mask-border-slice: {rule}')


@assert_no_logs
@pytest.mark.parametrize('rule, value', (
('1', ((1, None),)),
('1 2 3 4', ((1, None), (2, None), (3, None), (4, None))),
('50% 1000.1 0', ((50, '%'), (1000.1, None), (0, None))),
('1% 2px 3em 4', ((1, '%'), (2, 'px'), (3, 'em'), (4, None))),
('auto', ('auto',)),
('1 auto', ((1, None), 'auto')),
('auto auto', ('auto', 'auto')),
('auto auto auto 2', ('auto', 'auto', 'auto', (2, None))),
))
def test_mask_border_width(rule, value):
assert get_value(f'mask-border-width: {rule}') == value


@assert_no_logs
@pytest.mark.parametrize('rule', (
'none',
'1, 2',
'1 -2',
'-10',
'-10%',
'1px 2px 3px -10%',
'-3px',
'auto auto auto auto auto',
'1 2 3 4 5',
))
def test_mask_border_width_invalid(rule):
assert_invalid(f'mask-border-width: {rule}')


@assert_no_logs
@pytest.mark.parametrize('rule, value', (
('1', ((1, None),)),
('1 2 3 4', ((1, None), (2, None), (3, None), (4, None))),
('50px 1000.1 0', ((50, 'px'), (1000.1, None), (0, None))),
('1in 2px 3em 4', ((1, 'in'), (2, 'px'), (3, 'em'), (4, None))),
))
def test_mask_border_outset(rule, value):
assert get_value(f'mask-border-outset: {rule}') == value


@assert_no_logs
@pytest.mark.parametrize('rule', (
'none',
'auto',
'1, 2',
'-10',
'1 -2',
'10%',
'1px 2px 3px -10px',
'-3px',
'1 2 3 4 5',
))
def test_mask_border_outset_invalid(rule):
assert_invalid(f'mask-border-outset: {rule}')


@assert_no_logs
@pytest.mark.parametrize('rule, value', (
('stretch', ('stretch',)),
('repeat repeat', ('repeat', 'repeat')),
('round space', ('round', 'space')),
))
def test_mask_border_repeat(rule, value):
assert get_value(f'mask-border-repeat: {rule}') == value


@assert_no_logs
@pytest.mark.parametrize('rule', (
'none',
'test',
'round round round',
'stretch space round',
'repeat test',
))
def test_mask_border_repeat_invalid(rule):
assert_invalid(f'mask-border-repeat: {rule}')


@assert_no_logs
@pytest.mark.parametrize('rule, value', (
('alpha', 'alpha'),
('luminance', 'luminance'),
('alpha ', 'alpha'),
))
def test_mask_border_mode(rule, value):
assert get_value(f'mask-border-mode: {rule}') == value


@assert_no_logs
@pytest.mark.parametrize('rule', (
'none',
'test',
'alpha alpha',
'alpha luminance',
))
def test_mask_border_mode_invalid(rule):
assert_invalid(f'mask-border-mode: {rule}')


@assert_no_logs
@pytest.mark.parametrize('rule, value', (
('test content(text)', (('test', (('content()', 'text'),)),)),
Expand Down
62 changes: 62 additions & 0 deletions tests/draw/test_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,3 +576,65 @@ def test_border_image_gradient(assert_pixels):
</style>
<div></div>
''')


@assert_no_logs
def test_mask_border(assert_pixels):
assert_pixels('''
__________
__RR__RRR_
_R______R_
_R______R_
_s______R_
_s______R_
_R______R_
_R______R_
__RRRRRRR_
__________
''', '''
<style>
@page {
size: 10px 10px;
}
div {
background: red;
mask-border-source: url(mask.svg);
mask-border-slice: 20%;
height: 8px;
width: 8px;
margin: 1px;
}
</style>
<div></div>
''')


@assert_no_logs
def test_mask_border_fill(assert_pixels):
assert_pixels('''
__________
__RR__RRR_
_RRRRRRRR_
_RRRRRRRR_
_sRR__RRR_
_sRR__RRR_
_RRRRRRRR_
_RRRRRRRR_
__RRRRRRR_
__________
''', '''
<style>
@page {
size: 10px 10px;
}
div {
background: red;
mask-border-source: url(mask.svg);
mask-border-slice: 20% fill;
height: 8px;
width: 8px;
margin: 1px;
}
</style>
<div></div>
''')
4 changes: 4 additions & 0 deletions tests/resources/mask.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions weasyprint/css/computed_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ def border_width(style, name, value):


@register_computer('border-image-slice')
@register_computer('mask-border-slice')
def border_image_slice(style, name, values):
"""Compute the ``border-image-slice`` property."""
computed_values = []
Expand All @@ -385,6 +386,7 @@ def border_image_slice(style, name, values):


@register_computer('border-image-width')
@register_computer('mask-border-width')
def border_image_width(style, name, values):
"""Compute the ``border-image-width`` property."""
computed_values = []
Expand All @@ -404,6 +406,7 @@ def border_image_width(style, name, values):


@register_computer('border-image-outset')
@register_computer('mask-border-outset')
def border_image_outset(style, name, values):
"""Compute the ``border-image-outset`` property."""
computed_values = [
Expand All @@ -419,6 +422,7 @@ def border_image_outset(style, name, values):


@register_computer('border-image-repeat')
@register_computer('mask-border-repeat')
def border_image_repeat(style, name, values):
"""Compute the ``border-image-repeat`` property."""
return (values * 2) if len(values) == 1 else values
Expand Down
11 changes: 11 additions & 0 deletions weasyprint/css/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@
Dimension(0, None), Dimension(0, None),
Dimension(0, None), Dimension(0, None)),
'border_image_repeat': ('stretch', 'stretch'),
'mask_border_source': ('none', None),
'mask_border_slice': (
Dimension(100, '%'), Dimension(100, '%'),
Dimension(100, '%'), Dimension(100, '%'),
None),
'mask_border_width': ('auto', 'auto', 'auto', 'auto'),
'mask_border_outset': (
Dimension(0, None), Dimension(0, None),
Dimension(0, None), Dimension(0, None)),
'mask_border_repeat': ('stretch', 'stretch'),
'mask_border_mode': 'alpha',


# Color 3 (REC): https://www.w3.org/TR/css-color-3/
Expand Down
Loading
Loading