Skip to content

Commit

Permalink
Merge pull request #2139 from xavidotron/main
Browse files Browse the repository at this point in the history
Add support for mask-border-* properties
  • Loading branch information
liZe authored Jun 8, 2024
2 parents 1efe1b6 + 03b8f1a commit dd57e17
Show file tree
Hide file tree
Showing 11 changed files with 429 additions and 49 deletions.
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

0 comments on commit dd57e17

Please sign in to comment.