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: 2007-10-18 19:36:22 +0200 (Do, 18 Okt 2007) $' 
  7  __version__ = '$LastChangedRevision: 496 $' 
  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 if style: 68 self.style = style 69 self.seq.append(self.style) 70 else: 71 self._style = CSSStyleDeclaration(parentRule=self) 72 73 self._readonly = readonly
74
75 - def _getCssText(self):
76 """ 77 returns serialized property cssText 78 """ 79 return cssutils.ser.do_CSSStyleRule(self)
80
81 - def _setCssText(self, cssText):
82 """ 83 DOMException on setting 84 85 - SYNTAX_ERR: (self, StyleDeclaration, etc) 86 Raised if the specified CSS string value has a syntax error and 87 is unparsable. 88 - INVALID_MODIFICATION_ERR: (self) 89 Raised if the specified CSS string value represents a different 90 type of rule than the current one. 91 - HIERARCHY_REQUEST_ERR: (CSSStylesheet) 92 Raised if the rule cannot be inserted at this point in the 93 style sheet. 94 - NO_MODIFICATION_ALLOWED_ERR: (CSSRule) 95 Raised if the rule is readonly. 96 """ 97 super(CSSStyleRule, self)._setCssText(cssText) 98 99 tokenizer = self._tokenize2(cssText) 100 selectortokens = self._tokensupto2(tokenizer, blockstartonly=True) 101 styletokens = self._tokensupto2(tokenizer, blockendonly=True) 102 103 if not selectortokens or self._tokenvalue( 104 selectortokens[0]).startswith(u'@'): 105 self._log.error(u'CSSStyleRule: No content or no style rule.', 106 error=xml.dom.InvalidModificationErr) 107 108 else: 109 valid = True 110 111 bracetoken = selectortokens.pop() 112 if self._tokenvalue(bracetoken) != u'{': 113 valid = False 114 self._log.error( 115 u'CSSStyleRule: No start { of style declaration found: %r' % 116 self._valuestr(cssText), bracetoken) 117 elif not selectortokens: 118 valid = False 119 self._log.error(u'CSSStyleRule: No selector found: %r.' % 120 self._valuestr(cssText), bracetoken) 121 122 newselectorlist = SelectorList(selectorText=selectortokens) 123 124 newstyle = CSSStyleDeclaration() 125 if not styletokens: 126 valid = False 127 self._log.error( 128 u'CSSStyleRule: No style declaration or "}" found: %r' % 129 self._valuestr(cssText)) 130 else: 131 braceorEOFtoken = styletokens.pop() 132 val, typ = self._tokenvalue(braceorEOFtoken), self._type(braceorEOFtoken) 133 if val != u'}' and typ != 'EOF': 134 valid = False 135 self._log.error( 136 u'CSSStyleRule: No "}" after style declaration found: %r' % 137 self._valuestr(cssText)) 138 else: 139 if 'EOF' == typ: 140 # add again as style needs it 141 styletokens.append(braceorEOFtoken) 142 newstyle.cssText = styletokens 143 144 if valid: 145 self.valid = True 146 self.selectorList = newselectorlist 147 self.style = newstyle
148 149 cssText = property(_getCssText, _setCssText, 150 doc="(DOM) The parsable textual representation of the rule.") 151
152 - def _setSelectorList(self, selectorList):
153 """ 154 (cssutils) 155 set the SelectorList of this rule 156 157 selectorList 158 instance of SelectorList 159 160 DOMException on setting 161 162 - NO_MODIFICATION_ALLOWED_ERR: 163 Raised if this rule is readonly. 164 """ 165 self._checkReadonly() 166 self._selectorList = selectorList
167
168 - def _getSelectorList(self):
169 """ 170 (cssutils) 171 returns the SelectorList of this rule 172 see selectorText for a textual representation 173 """ 174 return self._selectorList
175 176 selectorList = property(_getSelectorList, _setSelectorList, 177 doc="The SelectorList of this rule.") 178
179 - def _getSelectorText(self):
180 """ 181 wrapper for cssutils SelectorList object 182 """ 183 return self._selectorList.selectorText
184
185 - def _setSelectorText(self, selectorText):
186 """ 187 wrapper for cssutils SelectorList object 188 189 selector 190 of type string, might also be a comma separated list of 191 selectors 192 193 DOMException on setting 194 195 - SYNTAX_ERR: (SelectorList, Selector) 196 Raised if the specified CSS string value has a syntax error 197 and is unparsable. 198 - NO_MODIFICATION_ALLOWED_ERR: (self) 199 Raised if this rule is readonly. 200 """ 201 self._checkReadonly() 202 self._selectorList = SelectorList(selectorText)
203 204 selectorText = property(_getSelectorText, _setSelectorText, 205 doc="""(DOM) The textual representation of the selector for the 206 rule set.""") 207
208 - def _getStyle(self):
209 return self._style
210
211 - def _setStyle(self, style):
212 """ 213 style 214 StyleDeclaration or string 215 """ 216 self._checkReadonly() 217 if isinstance(style, basestring): 218 self._style = CSSStyleDeclaration(parentRule=self, cssText=style) 219 else: 220 self._style = style 221 style.parentRule = self
222 223 style = property(_getStyle, _setStyle, 224 doc="(DOM) The declaration-block of this rule set.") 225
226 - def __repr__(self):
227 return "cssutils.css.%s(selectorText=%r, style=%r)" % ( 228 self.__class__.__name__, self.selectorText, self.style.cssText)
229
230 - def __str__(self):
231 return "<cssutils.css.%s object selector=%r style=%r at 0x%x>" % ( 232 self.__class__.__name__, self.selectorText, self.style.cssText, 233 id(self))
234