Skip to content

Commit 76faf0c

Browse files
committed
Allow @page inside @media
1 parent dfe68f4 commit 76faf0c

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

tinycss/css21.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,8 @@ def read_at_rule(self, at_keyword_token, tokens):
482482
return AtRule(at_keyword, head, body,
483483
at_keyword_token.line, at_keyword_token.column)
484484

485+
at_page_allowed_contexts = ['stylesheet']
486+
485487
def parse_at_rule(self, rule, previous_rules, errors, context):
486488
"""Parse an at-rule.
487489
@@ -508,7 +510,7 @@ def parse_at_rule(self, rule, previous_rules, errors, context):
508510
509511
"""
510512
if rule.at_keyword == '@page':
511-
if context != 'stylesheet':
513+
if context not in self.at_page_allowed_contexts:
512514
raise ParseError(rule, '@page rule not allowed in ' + context)
513515
selector, specificity = self.parse_page_selector(rule.head)
514516
if rule.body is None:

tinycss/page3.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ class CSSPage3Parser(CSS21Parser):
107107
'@right-bottom',
108108
]
109109

110+
at_page_allowed_contexts = ['stylesheet', '@media']
111+
110112
def parse_at_rule(self, rule, previous_rules, errors, context):
111113
if rule.at_keyword in self.PAGE_MARGIN_AT_KEYWORDS:
112114
if context != '@page':
@@ -150,6 +152,8 @@ def parse_page_selector(self, head):
150152
if (len(head) == 2 and head[0].type == ':'
151153
and head[1].type == 'IDENT'):
152154
pseudo_class = head[1].value
155+
# :blank is defined in GCPM:
156+
# http://dev.w3.org/csswg/css3-gcpm/#styling-blank-pages
153157
specificity = {
154158
'first': (1, 0), 'blank': (1, 0),
155159
'left': (0, 1), 'right': (0, 1),

tinycss/tests/test_page3.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import pytest
1414

15+
from tinycss.css21 import CSS21Parser
1516
from tinycss.page3 import CSSPage3Parser
1617
from .test_tokenizer import jsonify
1718
from . import assert_errors
@@ -99,3 +100,21 @@ def declarations(rule):
99100
rules = [(margin_rule.at_keyword, declarations(margin_rule))
100101
for margin_rule in rule.at_rules]
101102
assert rules == expected_rules
103+
104+
105+
def test_in_at_media():
106+
css = '@media print { @page { size: A4 } }'
107+
108+
stylesheet = CSS21Parser().parse_stylesheet(css)
109+
assert_errors(stylesheet.errors, ['@page rule not allowed in @media'])
110+
at_media_rule, = stylesheet.rules
111+
assert at_media_rule.at_keyword == '@media'
112+
assert at_media_rule.rules == []
113+
114+
stylesheet = CSSPage3Parser().parse_stylesheet(css)
115+
assert stylesheet.errors == []
116+
at_media_rule, = stylesheet.rules
117+
at_page_rule, = at_media_rule.rules
118+
assert at_media_rule.at_keyword == '@media'
119+
assert at_page_rule.at_keyword == '@page'
120+
assert len(at_page_rule.declarations) == 1

0 commit comments

Comments
 (0)