Skip to content

Commit

Permalink
Test grid shorthand and fix minor bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed May 23, 2024
1 parent 0b6c69e commit a4993ba
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
64 changes: 64 additions & 0 deletions tests/css/test_expanders.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,70 @@ def test_grid_template_invalid(rule):
assert_invalid(f'grid-template: {rule}')


@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('none', {
'template_rows': 'none', 'template_columns': 'none',
'template_areas': 'none',
'auto_rows': ('auto',), 'auto_columns': ('auto',),
'auto_flow': ('row',),
}),
('subgrid / [outer-edge] 20px [main-start]', {
'template_rows': ('subgrid', ()),
'template_columns': (('outer-edge',), (20, 'px'), ('main-start',)),
'template_areas': 'none',
'auto_rows': ('auto',), 'auto_columns': ('auto',),
'auto_flow': ('row',),
}),
('repeat(2, [e] 40px) repeat(5, auto) / subgrid [a] repeat(auto-fill, [b])', {
'template_rows': (
(), ('repeat()', 2, (('e',), (40, 'px'), ())), (),
('repeat()', 5, ((), 'auto', ())), ()),
'template_columns': ('subgrid', (('a',), ('repeat()', 'auto-fill', (('b',),)))),
'template_areas': 'none',
'auto_rows': ('auto',), 'auto_columns': ('auto',),
'auto_flow': ('row',),
}),
('auto-flow 1fr / 100px', {
'template_rows': 'none', 'template_columns': ((), (100, 'px'), ()),
'template_areas': 'none',
'auto_rows': ((1, 'fr'),), 'auto_columns': ('auto',),
'auto_flow': ('row',),
}),
('none / dense auto-flow 1fr', {
'template_rows': 'none', 'template_columns': 'none',
'template_areas': 'none',
'auto_rows': ('auto',), 'auto_columns': ((1, 'fr'),),
'auto_flow': ('column', 'dense'),
}),
# TODO: support last grid-template syntax
# ('[a b] "x y y" [c] [d] "x y y" 1fr [e] / auto 2fr auto', {
# }),
# ('[a b c] "x x x" 2fr', {
# }),
))
def test_grid(rule, result):
assert expand_to_dict(f'grid: {rule}') == dict(
(f'grid_{key}', value) for key, value in result.items())


@assert_no_logs
@pytest.mark.parametrize('rule', (
'none none',
'auto',
'subgrid / subgrid / subgrid',
'[a] 1px [b] / none /',
'[a] 1px [b] // none',
'[a] 1px [b] none',
'none / auto-flow 1fr dense',
'none / dense 1fr auto-flow',
'100px auto-flow / none',
'dense 100px / auto-flow 1fr'
))
def test_grid_invalid(rule):
assert_invalid(f'grid: {rule}')


@assert_no_logs
@pytest.mark.parametrize('rule, result', (
('page-break-after: left', {'break_after': 'left'}),
Expand Down
9 changes: 7 additions & 2 deletions weasyprint/css/validation/expanders.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,17 +830,22 @@ def expand_grid(tokens, name):
templates = {'row': [], 'column': []}
iterable = zip(split_tokens, templates.items())
for tokens, (track, track_templates) in iterable:
auto_flow_token = False
for token in tokens:
if get_keyword(token) == 'dense':
if dense or (auto_track and auto_track != track):
raise InvalidValues
dense = token
auto_track = track
elif get_keyword(token) == 'auto-flow':
if auto_track:
if auto_flow_token or (auto_track and auto_track != track):
raise InvalidValues
auto_flow_token = True
auto_track = track
else:
elif token == tokens[-1]:
track_templates.append(token)
else:
raise InvalidValues
if not auto_track:
raise InvalidValues
non_auto_track = 'row' if auto_track == 'column' else 'column'
Expand Down

0 comments on commit a4993ba

Please sign in to comment.