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

Source Code for Module cssutils.css.cssstylerule

  1  """CSSStyleRule implements DOM Level 2 CSS CSSStyleRule. 
  2  """ 
  3  __all__ = ['CSSStyleRule'] 
  4  __docformat__ = 'restructuredtext' 
  5  __author__ = '$LastChangedBy: cthedot $' 
  6  __date__ = '$LastChangedDate: 2008-01-15 21:56:30 +0100 (Di, 15 Jan 2008) $' 
  7  __version__ = '$LastChangedRevision: 860 $' 
  8   
  9  import xml.dom 
 10  import cssrule 
 11  import cssutils 
 12  from selectorlist import SelectorList 
 13  from cssstyledeclaration import CSSStyleDeclaration 
 14   
15 -class CSSStyleRule(cssrule.CSSRule):
16 """ 17 The CSSStyleRule object represents a ruleset specified (if any) in a CSS 18 style sheet. It provides access to a declaration block as well as to the 19 associated group of selectors. 20 21 Properties 22 ========== 23 selectorText: of type DOMString 24 The textual representation of the selector for the rule set. The 25 implementation may have stripped out insignificant whitespace while 26 parsing the selector. 27 style: of type CSSStyleDeclaration, (DOM) 28 The declaration-block of this rule set. 29 30 inherited properties: 31 - cssText 32 - parentRule 33 - parentStyleSheet 34 - type: STYLE_RULE 35 36 cssutils only 37 ------------- 38 selectorList: of type SelectorList (cssutils only) 39 A list of all Selector elements for the rule set. 40 41 Format 42 ====== 43 ruleset:: 44 45 : selector [ COMMA S* selector ]* 46 LBRACE S* declaration [ ';' S* declaration ]* '}' S* 47 ; 48 """ 49 type = cssrule.CSSRule.STYLE_RULE 50
51 - def __init__(self, selectorText=None, style=None, readonly=False):
52 """ 53 if readonly allows setting of properties in constructor only 54 55 selectorText 56 type string 57 style 58 CSSStyleDeclaration for this CSSStyleRule 59 """ 60 super(CSSStyleRule, self).__init__() 61 62 if selectorText: 63 self.selectorText = selectorText 64 self.seq.append(self.selectorText) 65 else: 66 self._selectorList = SelectorList() 67 self._selectorList.parentRule = self 68 if style: 69 self.style = style 70 self.seq.append(self.style) 71 else: 72 self._style = CSSStyleDeclaration(parentRule=self) 73 74 self._readonly = readonly
75
76 - def _getCssText(self):
77 """ 78 returns serialized property cssText 79 """ 80 return cssutils.ser.do_CSSStyleRule(self)
81
82 - def _setCssText(self, cssText):
83 """ 84 DOMException on setting 85 86 - SYNTAX_ERR: (self, StyleDeclaration, etc) 87 Raised if the specified CSS string value has a syntax error and 88 is unparsable. 89 - INVALID_MODIFICATION_ERR: (self) 90 Raised if the specified CSS string value represents a different 91 type of rule than the current one. 92 - HIERARCHY_REQUEST_ERR: (CSSStylesheet) 93 Raised if the rule cannot be inserted at this point in the 94 style sheet. 95 - NO_MODIFICATION_ALLOWED_ERR: (CSSRule) 96 Raised if the rule is readonly. 97 """ 98 super(CSSStyleRule, self)._setCssText(cssText) 99 100 tokenizer = self._tokenize2(cssText) 101 selectortokens = self._tokensupto2(tokenizer, blockstartonly=True) 102 styletokens = self._tokensupto2(tokenizer, blockendonly=True) 103 104 trail = self._nexttoken(tokenizer) 105 if trail: 106 self._log.error(u'CSSStyleRule: Trailing content: %s', 107 token=trail, 108 error=xml.dom.SyntaxErr) 109 110 if not selectortokens or self._tokenvalue( 111 selectortokens[0]).startswith(u'@'): 112 self._log.error(u'CSSStyleRule: No content or no style rule.', 113 error=xml.dom.InvalidModificationErr) 114 115 else: 116 valid = True 117 118 bracetoken = selectortokens.pop() 119 if self._tokenvalue(bracetoken) != u'{': 120 valid = False 121 self._log.error( 122 u'CSSStyleRule: No start { of style declaration found: %r' % 123 self._valuestr(cssText), bracetoken) 124 elif not selectortokens: 125 valid = False 126 self._log.error(u'CSSStyleRule: No selector found: %r.' % 127 self._valuestr(cssText), bracetoken) 128 129 newselectorlist = SelectorList(selectorText=selectortokens) 130 131 newstyle = CSSStyleDeclaration() 132 if not styletokens: 133 valid = False 134 self._log.error( 135 u'CSSStyleRule: No style declaration or "}" found: %r' % 136 self._valuestr(cssText)) 137 else: 138 braceorEOFtoken = styletokens.pop() 139 val, typ = self._tokenvalue(braceorEOFtoken), self._type(braceorEOFtoken) 140 if val != u'}' and typ != 'EOF': 141 valid = False 142 self._log.error( 143 u'CSSStyleRule: No "}" after style declaration found: %r' % 144 self._valuestr(cssText)) 145 else: 146 if 'EOF' == typ: 147 # add again as style needs it 148 styletokens.append(braceorEOFtoken) 149 newstyle.cssText = styletokens 150 151 if valid: 152 self.valid = True 153 self.selectorList = newselectorlist 154 self.style = newstyle
155 156 cssText = property(_getCssText, _setCssText, 157 doc="(DOM) The parsable textual representation of the rule.") 158
159 - def _setSelectorList(self, selectorList):
160 """ 161 (cssutils) 162 set the SelectorList of this rule 163 164 selectorList 165 instance of SelectorList 166 167 DOMException on setting 168 169 - NO_MODIFICATION_ALLOWED_ERR: 170 Raised if this rule is readonly. 171 """ 172 self._checkReadonly() 173 self._selectorList = selectorList 174 self._selectorList.parentRule = self
175
176 - def _getSelectorList(self):
177 """ 178 (cssutils) 179 returns the SelectorList of this rule 180 see selectorText for a textual representation 181 """ 182 return self._selectorList
183 184 selectorList = property(_getSelectorList, _setSelectorList, 185 doc="The SelectorList of this rule.") 186
187 - def _getSelectorText(self):
188 """ 189 wrapper for cssutils SelectorList object 190 """ 191 return self._selectorList.selectorText
192
193 - def _setSelectorText(self, selectorText):
194 """ 195 wrapper for cssutils SelectorList object 196 197 selector 198 of type string, might also be a comma separated list of 199 selectors 200 201 DOMException on setting 202 203 - SYNTAX_ERR: (SelectorList, Selector) 204 Raised if the specified CSS string value has a syntax error 205 and is unparsable. 206 - NO_MODIFICATION_ALLOWED_ERR: (self) 207 Raised if this rule is readonly. 208 """ 209 self._checkReadonly() 210 self._selectorList = SelectorList(selectorText) 211 self._selectorList.parentRule = self
212 213 selectorText = property(_getSelectorText, _setSelectorText, 214 doc="""(DOM) The textual representation of the selector for the 215 rule set.""") 216
217 - def _getStyle(self):
218 return self._style
219
220 - def _setStyle(self, style):
221 """ 222 style 223 StyleDeclaration or string 224 """ 225 self._checkReadonly() 226 if isinstance(style, basestring): 227 self._style = CSSStyleDeclaration(parentRule=self, cssText=style) 228 else: 229 self._style = style 230 style.parentRule = self
231 232 style = property(_getStyle, _setStyle, 233 doc="(DOM) The declaration-block of this rule set.") 234
235 - def __repr__(self):
236 return "cssutils.css.%s(selectorText=%r, style=%r)" % ( 237 self.__class__.__name__, self.selectorText, self.style.cssText)
238
239 - def __str__(self):
240 return "<cssutils.css.%s object selector=%r style=%r at 0x%x>" % ( 241 self.__class__.__name__, self.selectorText, self.style.cssText, 242 id(self))
243