Skip to content

Commit

Permalink
Support mask-border shorthand property.
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed Jun 8, 2024
1 parent 4ce7e56 commit 03781c3
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 2 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
70 changes: 68 additions & 2 deletions weasyprint/css/validation/expanders.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
border_width, box, column_count, column_width, flex_basis, flex_direction,
flex_grow_shrink, flex_wrap, font_family, font_size, font_stretch,
font_style, font_weight, gap, grid_line, grid_template, line_height,
list_style_image, list_style_position, list_style_type, other_colors,
overflow_wrap, validate_non_shorthand)
list_style_image, list_style_position, list_style_type, mask_border_mode,
other_colors, overflow_wrap, validate_non_shorthand)

EXPANDERS = {}

Expand Down Expand Up @@ -350,6 +350,72 @@ def expand_border_image(tokens, name, base_url):
raise InvalidValues


@expander('mask-border')
@generic_expander('-outset', '-repeat', '-slice', '-source', '-width', '-mode',
wants_base_url=True)
def expand_mask_border(tokens, name, base_url):
"""Expand the ``mask-border-*`` shorthand properties.
See https://drafts.fxtf.org/css-masking/#the-mask-border
"""
tokens = list(tokens)
while tokens:
if border_image_source(tokens[:1], base_url):
yield '-source', [tokens.pop(0)]
elif mask_border_mode(tokens[:1]):
yield '-mode', [tokens.pop(0)]
elif border_image_repeat(tokens[:1]):
repeats = [tokens.pop(0)]
while tokens and border_image_repeat(tokens[:1]):
repeats.append(tokens.pop(0))
yield '-repeat', repeats
elif border_image_slice(tokens[:1]) or get_keyword(tokens[0]) == 'fill':
slices = [tokens.pop(0)]
while tokens and border_image_slice(slices + tokens[:1]):
slices.append(tokens.pop(0))
yield '-slice', slices
if tokens and tokens[0].type == 'literal' and tokens[0].value == '/':
# slices / *
tokens.pop(0)
else:
# slices other
continue
if not tokens:
# slices /
raise InvalidValues
if border_image_width(tokens[:1]):
widths = [tokens.pop(0)]
while tokens and border_image_width(widths + tokens[:1]):
widths.append(tokens.pop(0))
yield '-width', widths
if tokens and tokens[0].type == 'literal' and tokens[0].value == '/':
# slices / widths / slash *
tokens.pop(0)
else:
# slices / widths other
continue
elif tokens and tokens[0].type == 'literal' and tokens[0].value == '/':
# slices / / *
tokens.pop(0)
else:
# slices / other
raise InvalidValues
if not tokens:
# slices / * /
raise InvalidValues
if border_image_outset(tokens[:1]):
outsets = [tokens.pop(0)]
while tokens and border_image_outset(outsets + tokens[:1]):
outsets.append(tokens.pop(0))
yield '-outset', outsets
else:
# slash / * / other
raise InvalidValues
else:
raise InvalidValues


@expander('background')
def expand_background(tokens, name, base_url):
"""Expand the ``background`` shorthand property.
Expand Down

0 comments on commit 03781c3

Please sign in to comment.