forked from jaraco/cssutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcssmediarule.py
More file actions
342 lines (293 loc) · 11.7 KB
/
cssmediarule.py
File metadata and controls
342 lines (293 loc) · 11.7 KB
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
"""CSSMediaRule implements DOM Level 2 CSS CSSMediaRule."""
__all__ = ['CSSMediaRule']
import xml.dom
import cssutils
from . import cssrule
class CSSMediaRule(cssrule.CSSRuleRules):
"""
Objects implementing the CSSMediaRule interface can be identified by the
MEDIA_RULE constant. On these objects the type attribute must return the
value of that constant.
Format::
: MEDIA_SYM S* medium [ COMMA S* medium ]*
STRING? # the name
LBRACE S* ruleset* '}' S*;
``cssRules``
All Rules in this media rule, a :class:`~cssutils.css.CSSRuleList`.
"""
def __init__(
self,
mediaText='all',
name=None,
parentRule=None,
parentStyleSheet=None,
readonly=False,
):
"""constructor"""
super().__init__(parentRule=parentRule, parentStyleSheet=parentStyleSheet)
self._atkeyword = '@media'
# 1. media
if mediaText:
self.media = mediaText
else:
self.media = cssutils.stylesheets.MediaList()
self.name = name
self._readonly = readonly
def __repr__(self):
return f"cssutils.css.{self.__class__.__name__}(mediaText={self.media.mediaText!r})"
def __str__(self):
return f"<cssutils.css.{self.__class__.__name__} object mediaText={self.media.mediaText!r} at 0x{id(self):x}>"
def _getCssText(self):
"""Return serialized property cssText."""
return cssutils.ser.do_CSSMediaRule(self)
def _setCssText(self, cssText): # noqa: C901
"""
:param cssText:
a parseable string or a tuple of (cssText, dict-of-namespaces)
:Exceptions:
- :exc:`~xml.dom.NamespaceErr`:
Raised if a specified selector uses an unknown namespace
prefix.
- :exc:`~xml.dom.SyntaxErr`:
Raised if the specified CSS string value has a syntax error and
is unparsable.
- :exc:`~xml.dom.InvalidModificationErr`:
Raised if the specified CSS string value represents a different
type of rule than the current one.
- :exc:`~xml.dom.HierarchyRequestErr`:
Raised if the rule cannot be inserted at this point in the
style sheet.
- :exc:`~xml.dom.NoModificationAllowedErr`:
Raised if the rule is readonly.
"""
# media "name"? { cssRules }
super()._setCssText(cssText)
# might be (cssText, namespaces)
cssText, namespaces = self._splitNamespacesOff(cssText)
tokenizer = self._tokenize2(cssText)
attoken = self._nexttoken(tokenizer, None)
if self._type(attoken) != self._prods.MEDIA_SYM:
self._log.error(
'CSSMediaRule: No CSSMediaRule found: %s' % self._valuestr(cssText),
error=xml.dom.InvalidModificationErr,
)
else:
# save if parse goes wrong
oldMedia = self._media
oldCssRules = self._cssRules
ok = True
# media
mediatokens, end = self._tokensupto2(
tokenizer, mediaqueryendonly=True, separateEnd=True
)
if '{' == self._tokenvalue(end) or self._prods.STRING == self._type(end):
self.media = cssutils.stylesheets.MediaList(parentRule=self)
# TODO: remove special case
self.media.mediaText = mediatokens
ok = ok and self.media.wellformed
else:
ok = False
# name (optional)
name = None
nameseq = self._tempSeq()
if self._prods.STRING == self._type(end):
name = self._stringtokenvalue(end)
# TODO: for now comments are lost after name
nametokens, end = self._tokensupto2(
tokenizer, blockstartonly=True, separateEnd=True
)
wellformed, expected = self._parse(None, nameseq, nametokens, {})
if not wellformed:
ok = False
self._log.error(
'CSSMediaRule: Syntax Error: %s' % self._valuestr(cssText)
)
# check for {
if '{' != self._tokenvalue(end):
self._log.error(
'CSSMediaRule: No "{" found: %s' % self._valuestr(cssText)
)
return
# cssRules
cssrulestokens, braceOrEOF = self._tokensupto2(
tokenizer, mediaendonly=True, separateEnd=True
)
nonetoken = self._nexttoken(tokenizer, None)
if 'EOF' == self._type(braceOrEOF):
# HACK!!!
# TODO: Not complete, add EOF to rule and } to @media
cssrulestokens.append(braceOrEOF)
braceOrEOF = ('CHAR', '}', 0, 0)
self._log.debug(
'CSSMediaRule: Incomplete, adding "}".',
token=braceOrEOF,
neverraise=True,
)
if '}' != self._tokenvalue(braceOrEOF):
self._log.error('CSSMediaRule: No "}" found.', token=braceOrEOF)
elif nonetoken:
self._log.error(
'CSSMediaRule: Trailing content found.', token=nonetoken
)
else:
# for closures: must be a mutable
new = {'wellformed': True}
def COMMENT(expected, seq, token, tokenizer=None):
self.insertRule(
cssutils.css.CSSComment(
[token],
parentRule=self,
parentStyleSheet=self.parentStyleSheet,
)
)
return expected
def ruleset(expected, seq, token, tokenizer):
rule = cssutils.css.CSSStyleRule(
parentRule=self, parentStyleSheet=self.parentStyleSheet
)
rule.cssText = self._tokensupto2(tokenizer, token)
if rule.wellformed:
self.insertRule(rule)
return expected
def atrule(expected, seq, token, tokenizer):
# TODO: get complete rule!
tokens = self._tokensupto2(tokenizer, token)
atval = self._tokenvalue(token)
factories = {
'@page': cssutils.css.CSSPageRule,
'@media': CSSMediaRule,
}
if atval in (
'@charset ',
'@font-face',
'@import',
'@namespace',
'@variables',
):
self._log.error(
'CSSMediaRule: This rule is not '
'allowed in CSSMediaRule - ignored: '
'%s.' % self._valuestr(tokens),
token=token,
error=xml.dom.HierarchyRequestErr,
)
elif atval in factories:
rule = factories[atval](
parentRule=self, parentStyleSheet=self.parentStyleSheet
)
rule.cssText = tokens
if rule.wellformed:
self.insertRule(rule)
else:
rule = cssutils.css.CSSUnknownRule(
tokens,
parentRule=self,
parentStyleSheet=self.parentStyleSheet,
)
if rule.wellformed:
self.insertRule(rule)
return expected
# save for possible reset
oldCssRules = self.cssRules
self.cssRules = cssutils.css.CSSRuleList()
seq = [] # not used really
tokenizer = iter(cssrulestokens)
wellformed, expected = self._parse(
braceOrEOF,
seq,
tokenizer,
{
'COMMENT': COMMENT,
'CHARSET_SYM': atrule,
'FONT_FACE_SYM': atrule,
'IMPORT_SYM': atrule,
'NAMESPACE_SYM': atrule,
'PAGE_SYM': atrule,
'MEDIA_SYM': atrule,
'ATKEYWORD': atrule,
},
default=ruleset,
new=new,
)
ok = ok and wellformed
if ok:
self.name = name
self._setSeq(nameseq)
else:
self._media = oldMedia
self._cssRules = oldCssRules
cssText = property(
_getCssText,
_setCssText,
doc="(DOM) The parsable textual representation of this " "rule.",
)
@property
def name(self):
"""An optional name for this media rule."""
return self._name
@name.setter
def name(self, name):
if isinstance(name, str) or name is None:
# "" or ''
if not name:
name = None
self._name = name
else:
self._log.error('CSSImportRule: Not a valid name: %s' % name)
def _setName(self, name):
self.name = name
def _setMedia(self, media):
"""
:param media:
a :class:`~cssutils.stylesheets.MediaList` or string
"""
self._checkReadonly()
if isinstance(media, str):
self._media = cssutils.stylesheets.MediaList(
mediaText=media, parentRule=self
)
else:
media._parentRule = self
self._media = media
# NOT IN @media seq at all?!
# # update seq
# for i, item in enumerate(self.seq):
# if item.type == 'media':
# self._seq[i] = (self._media, 'media', None, None)
# break
# else:
# # insert after @media if not in seq at all
# self.seq.insert(0,
# self._media, 'media', None, None)
media = property(
lambda self: self._media,
_setMedia,
doc="(DOM) A list of media types for this rule "
"of type :class:`~cssutils.stylesheets.MediaList`.",
)
def insertRule(self, rule, index=None):
"""Implements base ``insertRule``."""
rule, index = self._prepareInsertRule(rule, index)
if rule is False or rule is True:
# done or error
return
# check hierarchy
if (
isinstance(rule, cssutils.css.CSSCharsetRule)
or isinstance(rule, cssutils.css.CSSFontFaceRule)
or isinstance(rule, cssutils.css.CSSImportRule)
or isinstance(rule, cssutils.css.CSSNamespaceRule)
or isinstance(rule, cssutils.css.MarginRule)
):
self._log.error(
'%s: This type of rule is not allowed here: %s'
% (self.__class__.__name__, rule.cssText),
error=xml.dom.HierarchyRequestErr,
)
return
return self._finishInsertRule(rule, index)
type = property(
lambda self: self.MEDIA_RULE,
doc="The type of this rule, as defined by a CSSRule " "type constant.",
)
wellformed = property(lambda self: self.media.wellformed)