Package cssutils :: Package css :: Module cssmediarule
[hide private]
[frames] | no frames]

Source Code for Module cssutils.css.cssmediarule

  1  """CSSMediaRule implements DOM Level 2 CSS CSSMediaRule. 
  2  """ 
  3  __all__ = ['CSSMediaRule'] 
  4  __docformat__ = 'restructuredtext' 
  5  __author__ = '$LastChangedBy: doerwalter $' 
  6  __date__ = '$LastChangedDate: 2007-08-02 22:58:23 +0200 (Do, 02 Aug 2007) $' 
  7  __version__ = '0.9.2a5 $LastChangedRevision: 160 $' 
  8   
  9  import xml.dom 
 10   
 11  import cssrule 
 12  import cssutils 
 13   
14 -class CSSMediaRule(cssrule.CSSRule):
15 """ 16 represents an @media rule in a CSS style sheet. A @media rule can be 17 used to delimit style rules for specific media types. 18 19 Properties 20 ========== 21 cssRules: A css::CSSRuleList of all CSS rules contained within the 22 media block. 23 cssText: of type DOMString 24 The parsable textual representation of this rule 25 media: of type stylesheets::MediaList, (DOM readonly) 26 A list of media types for this rule of type MediaList. 27 28 cssutils only 29 ------------- 30 atkeyword: 31 the literal keyword used 32 33 Inherits properties from CSSRule 34 35 Format 36 ====== 37 media 38 : MEDIA_SYM S* medium [ COMMA S* medium ]* LBRACE S* ruleset* '}' S*; 39 """ 40 # CONSTANT 41 type = cssrule.CSSRule.MEDIA_RULE 42
43 - def __init__(self, readonly=False):
44 """ 45 constructor 46 """ 47 super(CSSMediaRule, self).__init__() 48 49 self.atkeyword = u'@media' 50 self._media = cssutils.stylesheets.MediaList() 51 self._rules = cssutils.css.cssrulelist.CSSRuleList() 52 53 self._readonly = readonly
54 55
56 - def _getMedia(self):
57 "returns MediaList" 58 return self._media
59 60 media = property(_getMedia, 61 doc=u"(DOM readonly) A list of media types for this rule of type\ 62 MediaList") 63 64
65 - def _getCssRules(self):
66 return self._rules
67 68 cssRules = property(_getCssRules, 69 doc="(DOM readonly) A css::CSSRuleList of all CSS rules contained\ 70 within the media block.") 71 72
73 - def deleteRule(self, index):
74 """ 75 index 76 within the media block's rule collection of the rule to remove. 77 78 Used to delete a rule from the media block. 79 80 DOMExceptions 81 82 - INDEX_SIZE_ERR: (self) 83 Raised if the specified index does not correspond to a rule in 84 the media rule list. 85 - NO_MODIFICATION_ALLOWED_ERR: (self) 86 Raised if this media rule is readonly. 87 """ 88 self._checkReadonly() 89 90 try: 91 self._rules[index].parentRule = None # detach 92 del self._rules[index] # remove from @media 93 except IndexError: 94 raise xml.dom.IndexSizeErr( 95 u'CSSMediaRule: %s is not a valid index in the rulelist of length %i' % ( 96 index, self.cssRules.length))
97 98
99 - def insertRule(self, rule, index=None):
100 """ 101 rule 102 The parsable text representing the rule. For rule sets this 103 contains both the selector and the style declaration. For 104 at-rules, this specifies both the at-identifier and the rule 105 content. 106 107 cssutils also allows rule to be a valid **CSSRule** object 108 109 index 110 within the media block's rule collection of the rule before 111 which to insert the specified rule. If the specified index is 112 equal to the length of the media blocks's rule collection, the 113 rule will be added to the end of the media block. 114 If index is not given or None rule will be appended to rule 115 list. 116 117 Used to insert a new rule into the media block. 118 119 DOMException on setting 120 121 - HIERARCHY_REQUEST_ERR: 122 (no use case yet as no @charset or @import allowed)) 123 Raised if the rule cannot be inserted at the specified index, 124 e.g., if an @import rule is inserted after a standard rule set 125 or other at-rule. 126 - INDEX_SIZE_ERR: (self) 127 Raised if the specified index is not a valid insertion point. 128 - NO_MODIFICATION_ALLOWED_ERR: (self) 129 Raised if this media rule is readonly. 130 - SYNTAX_ERR: (CSSStyleRule) 131 Raised if the specified rule has a syntax error and is 132 unparsable. 133 134 returns the index within the media block's rule collection of the 135 newly inserted rule. 136 137 """ 138 self._checkReadonly() 139 140 # check position 141 if index is None: 142 index = len(self.cssRules) 143 elif index < 0 or index > self.cssRules.length: 144 raise xml.dom.IndexSizeErr( 145 u'CSSMediaRule: Invalid index %s for CSSRuleList with a length of %s.' % ( 146 index, self.cssRules.length)) 147 148 # parse 149 if isinstance(rule, basestring): 150 tempsheet = CSSStyleSheet() 151 tempsheet.cssText = rule 152 if len(tempsheet.cssRules) != 1 or (tempsheet.cssRules and 153 not isinstance(tempsheet.cssRules[0], cssutils.css.CSSRule)): 154 self._log.error(u'CSSMediaRule: Invalid Rule: %s' % rule) 155 return 156 rule = tempsheet.cssRules[0] 157 elif not isinstance(rule, cssutils.css.CSSRule): 158 self._log.error(u'CSSMediaRule: Not a CSSRule: %s' % rule) 159 return 160 161 # CHECK HIERARCHY 162 # @charset @import @media -> TODO: @page @namespace 163 if isinstance(rule, cssutils.css.CSSCharsetRule) or \ 164 isinstance(rule, cssutils.css.CSSImportRule) or \ 165 isinstance(rule, CSSMediaRule): 166 self._log.error(u'CSSMediaRule: This type of rule is not allowed here: %s' % 167 rule.cssText, 168 error=xml.dom.HierarchyRequestErr) 169 return 170 171 self.cssRules.insert(index, rule) 172 rule.parentRule = self 173 return index
174 175
176 - def _getCssText(self):
177 """ 178 returns serialized property cssText 179 """ 180 return cssutils.ser.do_CSSMediaRule(self)
181
182 - def _setCssText(self, cssText):
183 """ 184 DOMException on setting 185 186 - NO_MODIFICATION_ALLOWED_ERR: (self) 187 Raised if the rule is readonly. 188 - INVALID_MODIFICATION_ERR: (self) 189 Raised if the specified CSS string value represents a different 190 type of rule than the current one. 191 - HIERARCHY_REQUEST_ERR: (self) 192 Raised if the rule cannot be inserted at this point in the 193 style sheet. 194 - SYNTAX_ERR: (self) 195 Raised if the specified CSS string value has a syntax error and 196 is unparsable. 197 """ 198 super(CSSMediaRule, self)._setCssText(cssText) 199 tokens = self._tokenize(cssText) 200 valid = True 201 202 # check if right token 203 if not tokens or tokens and tokens[0].type != self._ttypes.MEDIA_SYM: 204 self._log.error(u'CSSMediaRule: No CSSMediaRule found: %s' % 205 self._valuestr(cssText), 206 error=xml.dom.InvalidModificationErr) 207 return 208 else: 209 newatkeyword = tokens[0].value 210 211 newmedia = cssutils.stylesheets.MediaList() 212 mediatokens, endi = self._tokensupto(tokens[1:], 213 blockstartonly=True) 214 215 # checks if media ends with rules start and if at least 216 # one media found, medialist default to all which is wrong here! 217 if mediatokens and mediatokens[-1].value == u'{' and \ 218 self._ttypes.IDENT in [_t.type for _t in mediatokens]: 219 newmedia.mediaText = mediatokens[:-1] 220 else: 221 self._log.error(xml.dom.SyntaxErr( 222 u'CSSMediaRule: Syntax error in MediaList: %s' % 223 self._valuestr(cssText))) 224 return 225 226 newrules = cssutils.css.CSSRuleList() 227 i, imax = endi + 2, len(tokens) 228 while i < imax: 229 t = tokens[i] 230 231 if t.type == self._ttypes.EOF: 232 break 233 234 elif self._ttypes.S == t.type: # ignore 235 pass 236 237 elif self._ttypes.COMMENT == t.type: # just add 238 newrules.append(cssutils.css.CSSComment(t)) 239 240 elif u'}' == t.value: # end 241 if i+1 < len(tokens): 242 self._log.error( 243 u'CSSMediaRule: Unexpected tokens found: "%s".' 244 % self._valuestr(tokens[i:]), t) 245 break 246 247 elif self._ttypes.ATKEYWORD == t.type: 248 # @UNKNOWN 249 self._log.info(u'CSSMediaRule: Found unknown @rule.', t, 250 error=None) 251 atruletokens, endi = self._tokensupto(tokens[i:]) 252 i += endi 253 atrule = cssutils.css.CSSUnknownRule() 254 atrule.cssText = atruletokens 255 newrules.append(atrule) 256 257 elif t.type in (self._ttypes.CHARSET_SYM, self._ttypes.IMPORT_SYM, 258 self._ttypes.MEDIA_SYM, self._ttypes.PAGE_SYM): 259 atruletokens, endi = self._tokensupto(tokens[i:]) 260 i += endi + 1 261 self._log.error( 262 u'CSSMediaRule: This rule is not allowed in CSSMediaRule - ignored: %s.' 263 % self._valuestr(atruletokens), t, 264 xml.dom.HierarchyRequestErr) 265 continue 266 267 else: 268 # StyleRule 269 ruletokens, endi = self._tokensupto( 270 tokens[i:], blockendonly=True) 271 i += endi 272 rule = cssutils.css.CSSStyleRule() 273 rule.cssText = ruletokens 274 newrules.append(rule) 275 276 i += 1 277 278 if valid: 279 self.atkeyword = newatkeyword 280 self._media = newmedia 281 self._rules = newrules 282 for r in self._rules: 283 r.parentRule = self
284 285 cssText = property(_getCssText, _setCssText, 286 doc="(DOM attribute) The parsable textual representation.")
287 288 289 if __name__ == '__main__': 290 m = CSSMediaRule() 291 m.cssText = u'@media all {@;}' 292 print m.cssText 293