Debian Codegraph @

src/tinycss/page3.py

raw
1 # coding: utf8
2 """
3 tinycss.page3
4 ------------------
5
6 Support for CSS 3 Paged Media syntax:
7 http://dev.w3.org/csswg/css3-page/
8
9 Adds support for named page selectors and margin rules.
10
11 :copyright: (c) 2012 by Simon Sapin.
12 :license: BSD, see LICENSE for more details.
13 """
14
15
16 from .css21 import CSS21Parser, ParseError
17
18
19 class MarginRule:
20 """A parsed at-rule for margin box.
21
22 .. attribute:: at_keyword
23
24 One of the 16 following strings:
25
26 * ``@top-left-corner``
27 * ``@top-left``
28 * ``@top-center``
29 * ``@top-right``
30 * ``@top-right-corner``
31 * ``@bottom-left-corner``
32 * ``@bottom-left``
33 * ``@bottom-center``
34 * ``@bottom-right``
35 * ``@bottom-right-corner``
36 * ``@left-top``
37 * ``@left-middle``
38 * ``@left-bottom``
39 * ``@right-top``
40 * ``@right-middle``
41 * ``@right-bottom``
42
43 .. attribute:: declarations
44
45 A list of :class:`~.css21.Declaration` objects.
46
47 .. attribute:: line
48
49 Source line where this was read.
50
51 .. attribute:: column
52
53 Source column where this was read.
54
55 """
56
57 __slots__ = 'at_keyword', 'declarations', 'line', 'column'
58
59 def __init__(self, at_keyword, declarations, line, column):
60 self.at_keyword = at_keyword
61 self.declarations = declarations
62 self.line = line
63 self.column = column
64
65
66 class CSSPage3Parser(CSS21Parser):
67 """Extend :class:`~.css21.CSS21Parser` for `CSS 3 Paged Media`_ syntax.
68
69 .. _CSS 3 Paged Media: http://dev.w3.org/csswg/css3-page/
70
71 Compared to CSS 2.1, the ``at_rules`` and ``selector`` attributes of
72 :class:`~.css21.PageRule` objects are modified:
73
74 * ``at_rules`` is not always empty, it is a list of :class:`MarginRule`
75 objects.
76
77 * ``selector``, instead of a single string, is a tuple of the page name
78 and the pseudo class. Each of these may be a ``None`` or a string.
79
80 +--------------------------+------------------------+
81 | CSS | Parsed selectors |
82 +==========================+========================+
83 | .. code-block:: css | .. code-block:: python |
84 | | |
85 | @page {} | (None, None) |
86 | @page :first {} | (None, 'first') |
87 | @page chapter {} | ('chapter', None) |
88 | @page table:right {} | ('table', 'right') |
89 +--------------------------+------------------------+
90
91 """
92
93 PAGE_MARGIN_AT_KEYWORDS = (
94 '@top-left-corner',
95 '@top-left',
96 '@top-center',
97 '@top-right',
98 '@top-right-corner',
99 '@bottom-left-corner',
100 '@bottom-left',
101 '@bottom-center',
102 '@bottom-right',
103 '@bottom-right-corner',
104 '@left-top',
105 '@left-middle',
106 '@left-bottom',
107 '@right-top',
108 '@right-middle',
109 '@right-bottom',
110 )
111
112 def __init__(self):
113 super(CSSPage3Parser, self).__init__()
114 for x in self.PAGE_MARGIN_AT_KEYWORDS:
115 self.at_parsers[x] = self.parse_page_margin_rule
116
117 def parse_page_margin_rule(self, rule, previous_rules, errors, context):
118 if context != '@page':
119 raise ParseError(rule,
120 '%s rule not allowed in %s' % (rule.at_keyword, context))
121 if rule.head:
122 raise ParseError(rule.head[0],
123 'unexpected %s token in %s rule header'
124 % (rule.head[0].type, rule.at_keyword))
125 declarations, body_errors = self.parse_declaration_list(rule.body)
126 errors.extend(body_errors)
127 return MarginRule(rule.at_keyword, declarations,
128 rule.line, rule.column)
129
130 def parse_page_selector(self, tokens):
131 """Parse an @page selector.
132
133 :param tokens:
134 The ``tokens`` attribute of an unparsed :class:`AtRule`.
135 :returns:
136 A page selector. For CSS 2.1, this is 'first', 'left', 'right'
137 or None. 'blank' is added by GCPM.
138 :raises:
139 :class`~parsing.ParseError` on invalid selectors
140
141 """
142 if not tokens:
143 return (None, None), (0, 0, 0)
144 if tokens[0].type == 'IDENT':
145 name = tokens.pop(0).value
146 while tokens and tokens[0].type == 'S':
147 tokens.pop(0)
148 if not tokens:
149 return (name, None), (1, 0, 0)
150 name_specificity = (1,)
151 else:
152 name = None
153 name_specificity = (0,)
154 if (len(tokens) == 2 and tokens[0].type == ':'
155 and tokens[1].type == 'IDENT'):
156 pseudo_class = tokens[1].value
157 specificity = {
158 'first': (1, 0), 'blank': (1, 0),
159 'left': (0, 1), 'right': (0, 1),
160 }.get(pseudo_class)
161 if specificity:
162 return (name, pseudo_class), (name_specificity + specificity)
163 raise ParseError(tokens[0], 'invalid @page selector')