Skip to content

Commit 3359db5

Browse files
committed
Test grid shorthand and fix minor bugs
1 parent 4048869 commit 3359db5

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

tests/css/test_expanders.py

+64
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,70 @@ def test_grid_template_invalid(rule):
750750
assert_invalid(f'grid-template: {rule}')
751751

752752

753+
@assert_no_logs
754+
@pytest.mark.parametrize('rule, result', (
755+
('none', {
756+
'template_rows': 'none', 'template_columns': 'none',
757+
'template_areas': 'none',
758+
'auto_rows': ('auto',), 'auto_columns': ('auto',),
759+
'auto_flow': ('row',),
760+
}),
761+
('subgrid / [outer-edge] 20px [main-start]', {
762+
'template_rows': ('subgrid', ()),
763+
'template_columns': (('outer-edge',), (20, 'px'), ('main-start',)),
764+
'template_areas': 'none',
765+
'auto_rows': ('auto',), 'auto_columns': ('auto',),
766+
'auto_flow': ('row',),
767+
}),
768+
('repeat(2, [e] 40px) repeat(5, auto) / subgrid [a] repeat(auto-fill, [b])', {
769+
'template_rows': (
770+
(), ('repeat()', 2, (('e',), (40, 'px'), ())), (),
771+
('repeat()', 5, ((), 'auto', ())), ()),
772+
'template_columns': ('subgrid', (('a',), ('repeat()', 'auto-fill', (('b',),)))),
773+
'template_areas': 'none',
774+
'auto_rows': ('auto',), 'auto_columns': ('auto',),
775+
'auto_flow': ('row',),
776+
}),
777+
('auto-flow 1fr / 100px', {
778+
'template_rows': 'none', 'template_columns': ((), (100, 'px'), ()),
779+
'template_areas': 'none',
780+
'auto_rows': ((1, 'fr'),), 'auto_columns': ('auto',),
781+
'auto_flow': ('row',),
782+
}),
783+
('none / dense auto-flow 1fr', {
784+
'template_rows': 'none', 'template_columns': 'none',
785+
'template_areas': 'none',
786+
'auto_rows': ('auto',), 'auto_columns': ((1, 'fr'),),
787+
'auto_flow': ('column', 'dense'),
788+
}),
789+
# TODO: support last grid-template syntax
790+
# ('[a b] "x y y" [c] [d] "x y y" 1fr [e] / auto 2fr auto', {
791+
# }),
792+
# ('[a b c] "x x x" 2fr', {
793+
# }),
794+
))
795+
def test_grid(rule, result):
796+
assert expand_to_dict(f'grid: {rule}') == dict(
797+
(f'grid_{key}', value) for key, value in result.items())
798+
799+
800+
@assert_no_logs
801+
@pytest.mark.parametrize('rule', (
802+
'none none',
803+
'auto',
804+
'subgrid / subgrid / subgrid',
805+
'[a] 1px [b] / none /',
806+
'[a] 1px [b] // none',
807+
'[a] 1px [b] none',
808+
'none / auto-flow 1fr dense',
809+
'none / dense 1fr auto-flow',
810+
'100px auto-flow / none',
811+
'dense 100px / auto-flow 1fr'
812+
))
813+
def test_grid_invalid(rule):
814+
assert_invalid(f'grid: {rule}')
815+
816+
753817
@assert_no_logs
754818
@pytest.mark.parametrize('rule, result', (
755819
('page-break-after: left', {'break_after': 'left'}),

weasyprint/css/validation/expanders.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -830,17 +830,22 @@ def expand_grid(tokens, name):
830830
templates = {'row': [], 'column': []}
831831
iterable = zip(split_tokens, templates.items())
832832
for tokens, (track, track_templates) in iterable:
833+
auto_flow_token = False
833834
for token in tokens:
834835
if get_keyword(token) == 'dense':
835836
if dense or (auto_track and auto_track != track):
836837
raise InvalidValues
837838
dense = token
839+
auto_track = track
838840
elif get_keyword(token) == 'auto-flow':
839-
if auto_track:
841+
if auto_flow_token or (auto_track and auto_track != track):
840842
raise InvalidValues
843+
auto_flow_token = True
841844
auto_track = track
842-
else:
845+
elif token == tokens[-1]:
843846
track_templates.append(token)
847+
else:
848+
raise InvalidValues
844849
if not auto_track:
845850
raise InvalidValues
846851
non_auto_track = 'row' if auto_track == 'column' else 'column'

0 commit comments

Comments
 (0)