forked from jaraco/cssutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcssrule.py
More file actions
321 lines (270 loc) · 11 KB
/
cssrule.py
File metadata and controls
321 lines (270 loc) · 11 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
"""CSSRule implements DOM Level 2 CSS CSSRule."""
__all__ = ['CSSRule']
import xml.dom
import cssutils.util
class CSSRule(cssutils.util.Base2):
"""Abstract base interface for any type of CSS statement. This includes
both rule sets and at-rules. An implementation is expected to preserve
all rules specified in a CSS style sheet, even if the rule is not
recognized by the parser. Unrecognized rules are represented using the
``CSSUnknownRule`` interface.
"""
"""
CSSRule type constants.
An integer indicating which type of rule this is.
"""
UNKNOWN_RULE = 0
"``cssutils.css.CSSUnknownRule`` (not used in CSSOM anymore)"
STYLE_RULE = 1
"``cssutils.css.CSSStyleRule``"
CHARSET_RULE = 2
"``cssutils.css.CSSCharsetRule`` (not used in CSSOM anymore)"
IMPORT_RULE = 3
"``cssutils.css.CSSImportRule``"
MEDIA_RULE = 4
"``cssutils.css.CSSMediaRule``"
FONT_FACE_RULE = 5
"``cssutils.css.CSSFontFaceRule``"
PAGE_RULE = 6
"``cssutils.css.CSSPageRule``"
NAMESPACE_RULE = 10
"""``cssutils.css.CSSNamespaceRule``,
Value has changed in 0.9.7a3 due to a change in the CSSOM spec."""
COMMENT = 1001 # was -1, cssutils only
"""``cssutils.css.CSSComment`` - not in the offical spec,
Value has changed in 0.9.7a3"""
VARIABLES_RULE = 1008
"""``cssutils.css.CSSVariablesRule`` - experimental rule
not in the offical spec"""
MARGIN_RULE = 1006
"""``cssutils.css.MarginRule`` - experimental rule
not in the offical spec"""
_typestrings = {
UNKNOWN_RULE: 'UNKNOWN_RULE',
STYLE_RULE: 'STYLE_RULE',
CHARSET_RULE: 'CHARSET_RULE',
IMPORT_RULE: 'IMPORT_RULE',
MEDIA_RULE: 'MEDIA_RULE',
FONT_FACE_RULE: 'FONT_FACE_RULE',
PAGE_RULE: 'PAGE_RULE',
NAMESPACE_RULE: 'NAMESPACE_RULE',
COMMENT: 'COMMENT',
VARIABLES_RULE: 'VARIABLES_RULE',
MARGIN_RULE: 'MARGIN_RULE',
}
def __init__(self, parentRule=None, parentStyleSheet=None, readonly=False):
"""Set common attributes for all rules."""
super().__init__()
self._parent = parentRule
self._parentRule = parentRule
self._parentStyleSheet = parentStyleSheet
self._setSeq(self._tempSeq())
# self._atkeyword = None
# must be set after initialization of #inheriting rule is done
self._readonly = False
def _setAtkeyword(self, keyword):
"""Check if new keyword fits the rule it is used for."""
atkeyword = self._normalize(keyword)
if not self.atkeyword or (self.atkeyword == atkeyword):
self._atkeyword = atkeyword
self._keyword = keyword
else:
self._log.error(
f'{self.atkeyword}: Invalid atkeyword for this rule: {keyword!r}',
error=xml.dom.InvalidModificationErr,
)
atkeyword = property(
lambda self: self._atkeyword,
_setAtkeyword,
doc="Normalized keyword of an @rule (e.g. ``@import``).",
)
def _setCssText(self, cssText):
"""
:param cssText:
A parsable DOMString.
:exceptions:
- :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.
"""
self._checkReadonly()
cssText = property(
lambda self: '',
_setCssText,
doc="(DOM) The parsable textual representation of the "
"rule. This reflects the current state of the rule "
"and not its initial value.",
)
@property
def parent(self):
"""The Parent Node of this CSSRule or None."""
return self._parent
parentRule = property(
lambda self: self._parentRule,
doc="If this rule is contained inside another rule "
"(e.g. a style rule inside an @media block), this "
"is the containing rule. If this rule is not nested "
"inside any other rules, this returns None.",
)
def _getParentStyleSheet(self):
# rules contained in other rules (@media) use that rules parent
if self.parentRule:
return self.parentRule._parentStyleSheet
else:
return self._parentStyleSheet
parentStyleSheet = property(
_getParentStyleSheet, doc="The style sheet that contains this rule."
)
type = property(
lambda self: self.UNKNOWN_RULE,
doc="The type of this rule, as defined by a CSSRule " "type constant.",
)
typeString = property(
lambda self: CSSRule._typestrings[self.type],
doc="Descriptive name of this rule's type.",
)
wellformed = property(lambda self: False, doc="If the rule is wellformed.")
class CSSRuleRules(CSSRule):
"""Abstract base interface for rules that contain other rules
like @media or @page. Methods may be overwritten if a rule
has specific stuff to do like checking the order of insertion like
@media does.
"""
def __init__(self, parentRule=None, parentStyleSheet=None):
super().__init__(parentRule=parentRule, parentStyleSheet=parentStyleSheet)
self.cssRules = cssutils.css.CSSRuleList()
def __iter__(self):
"""Generator iterating over these rule's cssRules."""
yield from self._cssRules
def _setCssRules(self, cssRules):
"Set new cssRules and update contained rules refs."
cssRules.append = self.insertRule
cssRules.extend = self.insertRule
cssRules.__delitem__ = self.deleteRule
for rule in cssRules:
rule._parentRule = self
rule._parentStyleSheet = None
self._cssRules = cssRules
cssRules = property(
lambda self: self._cssRules,
_setCssRules,
"All Rules in this style sheet, a " ":class:`~cssutils.css.CSSRuleList`.",
)
def deleteRule(self, index):
"""
Delete the rule at `index` from rules ``cssRules``.
:param index:
The `index` of the rule to be removed from the rules cssRules
list. For an `index` < 0 **no** :exc:`~xml.dom.IndexSizeErr` is
raised but rules for normal Python lists are used. E.g.
``deleteRule(-1)`` removes the last rule in cssRules.
`index` may also be a CSSRule object which will then be removed.
:Exceptions:
- :exc:`~xml.dom.IndexSizeErr`:
Raised if the specified index does not correspond to a rule in
the media rule list.
- :exc:`~xml.dom.NoModificationAllowedErr`:
Raised if this media rule is readonly.
"""
self._checkReadonly()
if isinstance(index, CSSRule):
for i, r in enumerate(self.cssRules):
if index == r:
index = i
break
else:
raise xml.dom.IndexSizeErr(
"%s: Not a rule in "
"this rule'a cssRules list: %s" % (self.__class__.__name__, index)
)
try:
# detach
self._cssRules[index]._parentRule = None
del self._cssRules[index]
except IndexError as err:
raise xml.dom.IndexSizeErr(
'%s: %s is not a valid index '
'in the rulelist of length %i'
% (self.__class__.__name__, index, self._cssRules.length)
) from err
def _prepareInsertRule(self, rule, index=None):
"return checked `index` and optional parsed `rule`"
self._checkReadonly()
# check index
if index is None:
index = len(self._cssRules)
elif index < 0 or index > self._cssRules.length:
raise xml.dom.IndexSizeErr(
'%s: Invalid index %s for '
'CSSRuleList with a length of %s.'
% (self.__class__.__name__, index, self._cssRules.length)
)
# check and optionally parse rule
if isinstance(rule, str):
tempsheet = cssutils.css.CSSStyleSheet()
tempsheet.cssText = rule
if len(tempsheet.cssRules) != 1 or (
tempsheet.cssRules
and not isinstance(tempsheet.cssRules[0], cssutils.css.CSSRule)
):
self._log.error(f'{self.__class__.__name__}: Invalid Rule: {rule}')
return False, False
rule = tempsheet.cssRules[0]
elif isinstance(rule, cssutils.css.CSSRuleList):
# insert all rules
for i, r in enumerate(rule):
self.insertRule(r, index + i)
return True, True
elif not isinstance(rule, cssutils.css.CSSRule):
self._log.error(f'{rule}: Not a CSSRule: {self.__class__.__name__}')
return False, False
return rule, index
def _finishInsertRule(self, rule, index):
"add `rule` at `index`"
rule._parentRule = self
rule._parentStyleSheet = None
self._cssRules.insert(index, rule)
return index
def add(self, rule):
"""Add `rule` to page rule. Same as ``insertRule(rule)``."""
return self.insertRule(rule)
def insertRule(self, rule, index=None):
"""
Insert `rule` into the rules ``cssRules``.
:param rule:
the parsable text representing the `rule` to be inserted. For rule
sets this contains both the selector and the style declaration.
For at-rules, this specifies both the at-identifier and the rule
content.
cssutils also allows rule to be a valid
:class:`~cssutils.css.CSSRule` object.
:param index:
before the `index` the specified `rule` will be inserted.
If the specified `index` is equal to the length of the rules
rule collection, the rule will be added to the end of the rule.
If index is not given or None rule will be appended to rule
list.
:returns:
the index of the newly inserted rule.
:exceptions:
- :exc:`~xml.dom.HierarchyRequestErr`:
Raised if the `rule` cannot be inserted at the specified `index`,
e.g., if an @import rule is inserted after a standard rule set
or other at-rule.
- :exc:`~xml.dom.IndexSizeErr`:
Raised if the specified `index` is not a valid insertion point.
- :exc:`~xml.dom.NoModificationAllowedErr`:
Raised if this rule is readonly.
- :exc:`~xml.dom.SyntaxErr`:
Raised if the specified `rule` has a syntax error and is
unparsable.
"""
return self._prepareInsertRule(rule, index)