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

Source Code for Module cssutils.css.cssimportrule

  1  """CSSImportRule implements DOM Level 2 CSS CSSImportRule. 
  2   
  3  TODO: 
  4      - stylesheet: read and parse linked stylesheet 
  5  """ 
  6  __all__ = ['CSSImportRule'] 
  7  __docformat__ = 'restructuredtext' 
  8  __author__ = '$LastChangedBy: doerwalter $' 
  9  __date__ = '$LastChangedDate: 2007-08-02 22:58:23 +0200 (Do, 02 Aug 2007) $' 
 10  __version__ = '0.9.2a1, $LastChangedRevision: 160 $' 
 11   
 12  import xml.dom 
 13   
 14  import cssrule 
 15  import cssutils 
 16   
 17   
18 -class CSSImportRule(cssrule.CSSRule):
19 """ 20 Represents an @import rule within a CSS style sheet. The @import rule 21 is used to import style rules from other style sheets. 22 23 Properties 24 ========== 25 cssText: of type DOMString 26 The parsable textual representation of this rule 27 href: of type DOMString, (DOM readonly, cssutils also writable) 28 The location of the style sheet to be imported. The attribute will 29 not contain the url(...) specifier around the URI. 30 media: of type stylesheets::MediaList (DOM readonly) 31 A list of media types for this rule of type MediaList. 32 stylesheet: of type CSSStyleSheet (DOM readonly) 33 The style sheet referred to by this rule. The value of this 34 attribute is None if the style sheet has not yet been loaded or if 35 it will not be loaded (e.g. if the stylesheet is for a media type 36 not supported by the user agent). 37 38 Currently always None 39 40 cssutils only 41 ------------- 42 atkeyword: 43 the literal keyword used 44 hreftype: 'uri' (serializer default) or 'string' 45 The original usage of href, not really relevant as it may be 46 configured in the serializer too 47 48 Inherits properties from CSSRule 49 50 Format 51 ====== 52 import 53 : IMPORT_SYM S* 54 [STRING|URI] S* [ medium [ COMMA S* medium]* ]? ';' S* 55 ; 56 """ 57 type = cssrule.CSSRule.IMPORT_RULE 58
59 - def __init__(self, href=None, mediaText=u'all', hreftype=None, 60 readonly=False):
61 """ 62 if readonly allows setting of properties in constructor only 63 64 Do not use as positional but as keyword attributes only! 65 66 href 67 location of the style sheet to be imported. 68 mediaText 69 A list of media types for which this style sheet may be used 70 as a string 71 hreftype 72 'uri' (default) or 'string' 73 """ 74 super(CSSImportRule, self).__init__() 75 76 self.atkeyword = u'@import' 77 self.href = href 78 self.hreftype = hreftype 79 self._media = cssutils.stylesheets.MediaList( 80 mediaText, readonly=readonly) 81 if not self.media.valid: 82 self._media = cssutils.stylesheets.MediaList() 83 self.seq = [self.href, self.media] 84 85 # TODO: load stylesheet from href automatically? 86 self._styleSheet = None 87 88 self._readonly = readonly
89 90
91 - def _getHref(self):
92 """ returns href as a string """ 93 return self._href
94
95 - def _setHref(self, href):
96 """ 97 TODO: 98 parse properly 99 100 DOMException on setting 101 102 - SYNTAX_ERR: (not checked here) 103 Raised if the specified CSS string value has a syntax error and 104 is unparsable. 105 - NO_MODIFICATION_ALLOWED_ERR: (CSSRule) 106 Raised if this rule is readonly. 107 """ 108 self._checkReadonly() 109 110 # update seq 111 for i, x in enumerate(self.seq): 112 if x == self._href: 113 self.seq[i] = href 114 break 115 else: 116 self.seq = [href] 117 # set new href 118 self._href = href
119 120 href = property(_getHref, _setHref, 121 doc="Location of the style sheet to be imported.") 122 123
124 - def _getMedia(self):
125 "returns MediaList" 126 return self._media
127 128 media = property(_getMedia, 129 doc=u"(DOM readonly) A list of media types for this rule of type\ 130 MediaList") 131 132
133 - def _getStyleSheet(self):
134 """ 135 returns a CSSStyleSheet or None 136 """ 137 return self._styleSheet
138 139 styleSheet = property(_getStyleSheet, 140 doc="(readonly) The style sheet referred to by this rule.") 141 142
143 - def _getCssText(self):
144 """ 145 returns serialized property cssText 146 """ 147 return cssutils.ser.do_CSSImportRule(self)
148
149 - def _setCssText(self, cssText):
150 """ 151 DOMException on setting 152 153 - HIERARCHY_REQUEST_ERR: (CSSStylesheet) 154 Raised if the rule cannot be inserted at this point in the 155 style sheet. 156 - INVALID_MODIFICATION_ERR: (self) 157 Raised if the specified CSS string value represents a different 158 type of rule than the current one. 159 - NO_MODIFICATION_ALLOWED_ERR: (CSSRule) 160 Raised if the rule is readonly. 161 - SYNTAX_ERR: (self) 162 Raised if the specified CSS string value has a syntax error and 163 is unparsable. 164 """ 165 super(CSSImportRule, self)._setCssText(cssText) 166 valid = True 167 168 tokens = self._tokenize(cssText) 169 170 # check if right type 171 if not tokens or tokens and tokens[0].type != self._ttypes.IMPORT_SYM: 172 self._log.error(u'CSSImportRule: No CSSImportRule found: %s' % 173 self._valuestr(cssText), 174 error=xml.dom.InvalidModificationErr) 175 return 176 else: 177 newatkeyword = tokens[0].value 178 179 newseq = [] 180 newhref = None 181 newhreftype = None 182 newmedia = cssutils.stylesheets.MediaList() 183 184 mediatokens = [] 185 expected = 'href' # href medialist end 186 for i in range(1, len(tokens)): 187 t = tokens[i] 188 189 if self._ttypes.EOF == t.type: 190 expected = 'EOF' 191 192 elif self._ttypes.S == t.type: # ignore 193 pass 194 195 elif self._ttypes.COMMENT == t.type: 196 if 'href' == expected: # before href 197 newseq.append(cssutils.css.CSSComment(t)) 198 else: # after href 199 mediatokens.append(t) 200 201 elif 'href' == expected and \ 202 t.type in (self._ttypes.URI, self._ttypes.STRING): 203 if t.type == self._ttypes.URI: 204 newhref = t.value[4:-1].strip() # url(href) 205 newhreftype = 'uri' 206 else: 207 newhref = t.value[1:-1].strip() # "href" or 'href' 208 newhreftype = 'string' 209 newseq.append(newhref) 210 expected = 'medialist' 211 212 elif self._ttypes.SEMICOLON == t.type: 213 if 'medialist' != expected: # normal end 214 valid = False 215 self._log.error( 216 u'CSSImportRule: Syntax Error, no href found.', t) 217 expected = None 218 break 219 220 elif 'medialist' == expected: 221 mediatokens.append(t) 222 223 else: 224 valid = False 225 self._log.error(u'CSSImportRule: Syntax Error.', t) 226 227 if expected and expected != 'EOF': 228 valid = False 229 self._log.error(u'CSSImportRule: Syntax Error, no ";" found: %s' % 230 self._valuestr(cssText)) 231 return 232 233 if mediatokens: 234 newmedia.mediaText = mediatokens 235 if not newmedia.valid: 236 valid = False 237 newseq.append(newmedia) 238 239 if valid: 240 self.atkeyword = newatkeyword 241 self.href = newhref 242 self.hreftype = newhreftype 243 self._media = newmedia 244 self.seq = newseq
245 246 cssText = property(fget=_getCssText, fset=_setCssText, 247 doc="(DOM attribute) The parsable textual representation.")
248 249 250 if __name__ == '__main__': 251 c = CSSImportRule(href='"1.css"', mediaText='handheld, all, tv') 252 print c.seq#, c.media.seq 253 print c.cssText 254 c.cssText = '@import' 255