Skip to content

Commit

Permalink
Fix CSS nesting for nested selectors with comma
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed May 22, 2024
1 parent 0019940 commit 8cdd66f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
2 changes: 2 additions & 0 deletions tests/css/test_nesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
'p { div & { width: 10px } width: 20px }',
'div { & { & { p { & { width: 10px } } } } }',
'@media print { div { p { width: 10px } } }',
'div { em, p { width: 10px } }',
'p { a, div & { width: 10px } }',
))
def test_nesting_block(style):
page, = render_pages('''
Expand Down
43 changes: 25 additions & 18 deletions weasyprint/css/validation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,26 +149,33 @@ def preprocess_declarations(base_url, declarations, prelude=None):
# Nested rule.
if prelude is None:
continue
declaration_prelude = declaration.prelude
if NESTING_SELECTOR in declaration.prelude:
# Replace & selector by parent.
declaration_prelude = []
for token in declaration.prelude:
if token == NESTING_SELECTOR:
declaration_prelude.extend(is_token)
else:
declaration_prelude.append(token)
else:
# No & selector, prepend parent.
is_token = (
LiteralToken(1, 1, ':'),
FunctionBlock(1, 1, 'is', prelude))
declaration_prelude = [
*is_token, WhitespaceToken(1, 1, ' '),
*declaration.prelude]
declaration_prelude = []
token_groups = [[]]
for token in declaration.prelude:
if token == ',':
token_groups.append([])
else:
token_groups[-1].append(token)
for token_group in token_groups:
if NESTING_SELECTOR in token_group:
# Replace & selector by parent.
for token in declaration.prelude:
if token == NESTING_SELECTOR:
declaration_prelude.extend(is_token)
else:
declaration_prelude.append(token)
else:
# No & selector, prepend parent.
is_token = (
LiteralToken(1, 1, ':'),
FunctionBlock(1, 1, 'is', prelude))
declaration_prelude.extend([
*is_token, WhitespaceToken(1, 1, ' '),
*token_group])
declaration_prelude.append(LiteralToken(1, 1, ','))
yield from preprocess_declarations(
base_url, parse_blocks_contents(declaration.content),
declaration_prelude)
declaration_prelude[:-1])

if declaration.type != 'declaration':
continue
Expand Down

0 comments on commit 8cdd66f

Please sign in to comment.