-
Notifications
You must be signed in to change notification settings - Fork 0
/
cssdecl.py
349 lines (288 loc) · 10.6 KB
/
cssdecl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
"""Python CSS declaration computer
"""
import re
import warnings
from collections import defaultdict
try:
import tinycss2
import tinycss2.color3
except ImportError:
# currently needed for setup.cfg to get version :(
pass
__version__ = '0.1.3+dev'
__all__ = ['CSSWarning', 'CSS22Resolver']
class CSSWarning(UserWarning):
"""Warning for when this CSS syntax cannot currently be parsed"""
pass
def _clean_tokens(tokens):
cleaned = []
for tok in tokens:
if tok.type == 'comment' or tok.type == 'whitespace':
pass
elif tok.type == 'error':
# TODO: indicate error context (requires
warnings.warn('Error parsing CSS: %r' % tok.message,
CSSWarning)
else:
cleaned.append(tok)
return cleaned
def match_color_token(token):
return tinycss2.color3.parse_color(token) is not None
def match_size_token(token):
return (token.type == 'dimension' or token.type == 'percentage'
or (token.type == 'ident' and token.lower_value in {
'medium', 'thin', 'thick', 'smaller', 'larger', 'xx-small',
'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'}))
class IdentMatch:
def __init__(self, values):
assert not isinstance(values, str)
self.values = set(values)
def __call__(self, token):
return token.type == 'ident' and token.lower_value in self.values
match_inherit_initial = IdentMatch(['inherit', 'initial'])
def match_tokens(tokens, matchers, remainder):
cleaned = _clean_tokens(tokens)
if len(cleaned) == 1 and match_inherit_initial(cleaned[0]):
out = {remainder: cleaned}
for k in matchers:
out[k] = cleaned
return out
out = defaultdict(list)
for tok in cleaned:
for k, matcher in matchers.items():
if matcher(tok):
out[k].append(tok)
break
else:
out[remainder].append(tok)
out.default_factory = None
return out
class _BaseCSSResolver(object):
"""Base class for parsing and resolving CSS to atomic properties
"""
def __init__(self, initial=None):
self.initial = initial or {}
def resolve_string(self, declarations_str, inherited=None):
"""Resolve the given declarations to atomic properties
Parameters
----------
declarations_str : str
A list of CSS declarations
inherited : dict, optional
Atomic properties indicating the inherited style context in which
declarations_str is to be resolved. ``inherited`` should already
be resolved, i.e. valid output of this method.
Returns
-------
props : dict
Atomic CSS 2.2 properties mapped to their values as strings
Examples
--------
>>> resolver = CSS22Resolver()
>>> inherited = {'font-family': 'serif', 'font-weight': 'bold'}
>>> out = resolver.resolve_string('''
... border-color: BLUE RED;
... font-size: 1em;
... font-size: 2em;
... font-weight: normal;
... font-weight: inherit;
... ''', inherited)
>>> sorted(out.items()) # doctest: +NORMALIZE_WHITESPACE
[('border-bottom-color', 'blue'),
('border-left-color', 'red'),
('border-right-color', 'red'),
('border-top-color', 'blue'),
('font-family', 'serif'),
('font-size', '24pt'),
('font-weight', 'bold')]
"""
props = dict(self._atomize(self._parse(declarations_str)))
if inherited is None:
inherited = {}
# 1. resolve inherited, initial
for prop, val in inherited.items():
if prop not in props:
props[prop] = val
for prop, val in list(props.items()):
if val == 'inherit':
val = inherited.get(prop, 'initial')
if val == 'initial':
val = self.initial.get(prop)
if val is None:
# we do not define a complete initial stylesheet
del props[prop]
else:
props[prop] = val
# 2. resolve relative font size
if props.get('font-size'):
if 'font-size' in inherited:
em_pt = inherited['font-size']
assert em_pt[-2:] == 'pt'
em_pt = float(em_pt[:-2])
else:
em_pt = None
props['font-size'] = self._size_to_pt(
props['font-size'], em_pt, conversions=self.FONT_SIZE_RATIOS)
font_size = float(props['font-size'][:-2])
else:
font_size = None
# 3. TODO: resolve other font-relative units
for side in self.SIDES:
prop = 'border-%s-width' % side
if prop in props:
props[prop] = self._size_to_pt(
props[prop], em_pt=font_size,
conversions=self.BORDER_WIDTH_RATIOS)
for prop in ['margin-%s' % side, 'padding-%s' % side]:
if prop in props:
# TODO: support %
props[prop] = self._size_to_pt(
props[prop], em_pt=font_size,
conversions=self.MARGIN_RATIOS)
return props
UNIT_RATIOS = {
'rem': ('pt', 12),
'ex': ('em', .5),
# 'ch':
'px': ('pt', .75),
'pc': ('pt', 12),
'in': ('pt', 72),
'cm': ('in', 1 / 2.54),
'mm': ('in', 1 / 25.4),
'q': ('mm', .25),
'!!default': ('em', 0),
}
FONT_SIZE_RATIOS = UNIT_RATIOS.copy()
FONT_SIZE_RATIOS.update({
'%': ('em', .01),
'xx-small': ('rem', .5),
'x-small': ('rem', .625),
'small': ('rem', .8),
'medium': ('rem', 1),
'large': ('rem', 1.125),
'x-large': ('rem', 1.5),
'xx-large': ('rem', 2),
'smaller': ('em', 1 / 1.2),
'larger': ('em', 1.2),
'!!default': ('em', 1),
})
MARGIN_RATIOS = UNIT_RATIOS.copy()
MARGIN_RATIOS.update({
'none': ('pt', 0),
})
BORDER_WIDTH_RATIOS = UNIT_RATIOS.copy()
BORDER_WIDTH_RATIOS.update({
'none': ('pt', 0),
'thick': ('px', 4),
'medium': ('px', 2),
'thin': ('px', 1),
# Default: medium only if solid
})
def _size_to_pt(self, in_val, em_pt=None, conversions=UNIT_RATIOS):
def _error():
warnings.warn('Unhandled size: %r' % in_val, CSSWarning)
return self._size_to_pt('1!!default', conversions=conversions)
try:
val, unit = re.match(r'^(\S*?)([a-zA-Z%!].*)', in_val).groups()
except AttributeError:
return _error()
if val == '':
# hack for 'large' etc.
val = 1
else:
try:
val = float(val)
except ValueError:
return _error()
while unit != 'pt':
if unit == 'em':
if em_pt is None:
unit = 'rem'
else:
val *= em_pt
unit = 'pt'
continue
try:
unit, mul = conversions[unit]
except KeyError:
return _error()
val *= mul
val = round(val, 5)
if int(val) == val:
size_fmt = '%d'
else:
size_fmt = '%f'
return (size_fmt + 'pt') % val
def _atomize(self, declarations):
for prop, value in declarations:
attr = 'expand_' + prop.replace('-', '_')
try:
expand = getattr(self, attr)
except AttributeError:
yield prop, value
else:
for prop, value in expand(prop, value):
yield prop, value
def _parse(self, declarations_str):
"""Generates (prop, value) pairs from declarations
In a future version may generate parsed tokens from tinycss/tinycss2
"""
decls = tinycss2.parse_declaration_list(declarations_str,
skip_comments=True)
decls = _clean_tokens(decls)
for decl in decls:
value_str = tinycss2.serialize(decl.value).strip().lower()
yield decl.lower_name, value_str
class _CommonExpansions(object):
SIDE_SHORTHANDS = {
1: [0, 0, 0, 0],
2: [0, 1, 0, 1],
3: [0, 1, 2, 1],
4: [0, 1, 2, 3],
}
SIDES = ('top', 'right', 'bottom', 'left')
def _side_expander(prop_fmt):
def expand(self, prop, value):
tokens = _clean_tokens(tinycss2.parse_component_value_list(value))
try:
mapping = self.SIDE_SHORTHANDS[len(tokens)]
except KeyError:
warnings.warn('Could not expand "%s: %s"' % (prop, value),
CSSWarning)
return
for key, idx in zip(self.SIDES, mapping):
yield prop_fmt % key, tinycss2.serialize(tokens[idx:idx + 1])
return expand
expand_border_color = _side_expander('border-%s-color')
expand_border_style = _side_expander('border-%s-style')
expand_border_width = _side_expander('border-%s-width')
expand_margin = _side_expander('margin-%s')
expand_padding = _side_expander('padding-%s')
def expand_border(self, prop, value, side=None):
if side is None:
sides = ['top', 'right', 'bottom', 'left']
else:
sides = [side]
# TODO: clean tokens?
value = tinycss2.parse_component_value_list(value)
matched = match_tokens(value,
{'width': match_size_token,
'color': match_color_token},
remainder='style')
for side in sides:
for k, v in matched.items():
if v:
v = _clean_tokens(v)
yield 'border-%s-%s' % (side, k), tinycss2.serialize(v)
def _border_side_expander(side):
# XXX: functools.partial cannot be bound!
def expand_border_side(self, prop, value):
return self.expand_border(prop, value, side)
return expand_border_side
expand_border_top = _border_side_expander('top')
expand_border_right = _border_side_expander('right')
expand_border_bottom = _border_side_expander('bottom')
expand_border_left = _border_side_expander('left')
class CSS22Resolver(_BaseCSSResolver, _CommonExpansions):
"""Parses and resolves CSS to atomic CSS 2.2 properties
"""