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

Source Code for Module cssutils.css.property

  1  """Property is a single CSS property in a CSSStyleDeclaration 
  2   
  3  Internal use only, may be removed in the future! 
  4  """ 
  5  __all__ = [] 
  6  __docformat__ = 'restructuredtext' 
  7  __author__ = '$LastChangedBy: doerwalter $' 
  8  __date__ = '$LastChangedDate: 2007-08-02 22:58:23 +0200 (Do, 02 Aug 2007) $' 
  9  __version__ = '0.9.2b2, $LastChangedRevision: 160 $' 
 10   
 11  import xml.dom 
 12   
 13  import cssutils 
 14  import cssproperties 
 15  from cssvalue import CSSValue 
 16   
17 -class _Property(cssutils.util.Base):
18 """ 19 (cssutils) a CSS property in a StyleDeclaration of a CSSStyleRule 20 21 Properties 22 ========== 23 name 24 of the property 25 normalname 26 normalized name of the property, e.g. "color" when name is "c\olor" 27 cssValue 28 the relevant CSSValue instance for this property 29 priority 30 of the property (currently only "!important" or None) 31 seqs 32 combination of a list for seq of name, a CSSValue object, and 33 a list for seq of priority (empty or [!important] currently) 34 valid 35 if this Property is valid 36 37 DEPRECATED: value 38 the string value of the property, use cssValue.cssText instead! 39 40 Format 41 ====== 42 :: 43 44 property = name 45 : IDENT S* 46 ; 47 48 expr = value 49 : term [ operator term ]* 50 ; 51 term 52 : unary_operator? 53 [ NUMBER S* | PERCENTAGE S* | LENGTH S* | EMS S* | EXS S* | ANGLE S* | 54 TIME S* | FREQ S* | function ] 55 | STRING S* | IDENT S* | URI S* | hexcolor 56 ; 57 function 58 : FUNCTION S* expr ')' S* 59 ; 60 /* 61 * There is a constraint on the color that it must 62 * have either 3 or 6 hex-digits (i.e., [0-9a-fA-F]) 63 * after the "#"; e.g., "#000" is OK, but "#abcd" is not. 64 */ 65 hexcolor 66 : HASH S* 67 ; 68 69 prio 70 : IMPORTANT_SYM S* 71 ; 72 73 """
74 - def __init__(self, name, value, priority=None):
75 """ 76 inits property 77 """ 78 super(_Property, self).__init__() 79 80 self.seqs = [[], None, []] 81 self.valid = True 82 self.name = name 83 self.cssValue = value 84 self.priority = priority
85 86
87 - def __repr__(self):
88 return '<Property> %s: %s %s' % ( 89 self.name, self.cssValue._value, self.priority)
90 91
92 - def __invalidToken(self, tokens, x):
93 """ 94 raises SyntaxErr if an INVALID token in tokens 95 96 x 97 name, value or priority, used for error message 98 99 returns True if INVALID found, else False 100 """ 101 for t in tokens: 102 if t.type == self._ttypes.INVALID: 103 self._log.error(u'Property: Invalid token found in %s.' % x, t) 104 return True 105 return False
106 107
108 - def _getName(self):
109 try: 110 return self._name 111 except AttributeError: 112 return u''
113
114 - def _setName(self, name):
115 """ 116 Format 117 ====== 118 property = name 119 : IDENT S* 120 ; 121 122 DOMException on setting 123 124 - SYNTAX_ERR: (self) 125 Raised if the specified name has a syntax error and is 126 unparsable. 127 """ 128 tokens = self._tokenize(name) 129 if self.__invalidToken(tokens, 'name'): 130 return 131 newname = newnormalname = None 132 newseq = [] 133 t = None # used later 134 for i in range(0, len(tokens)): 135 t = tokens[i] 136 if self._ttypes.S == t.type: # ignore 137 pass 138 139 elif self._ttypes.COMMENT == t.type: # just add 140 newseq.append(cssutils.css.CSSComment(t)) 141 142 elif self._ttypes.IDENT == t.type and not newname: 143 newname = t.value.lower() 144 newnormalname = t.normalvalue 145 newseq.append(newname) 146 147 else: 148 self._log.error(u'Property: Syntax error in name.', t) 149 return 150 151 if newname: 152 self._name = newname 153 self.normalname = newnormalname 154 self.seqs[0] = newseq 155 156 # validate 157 if newname not in cssproperties.cssvalues: 158 self._log.info(u'Property: No CSS2 Property: "%s".' % 159 newname, t, neverraise=True) 160 161 else: 162 self._log.error(u'Property: No name found: "%s".' % name, t)
163 164 name = property(_getName, _setName, 165 doc="(cssutils) Name of this property") 166 167
168 - def _getCSSValue(self):
169 return self.seqs[1]
170
171 - def _setCSSValue(self, cssText):
172 """ 173 see css.CSSValue 174 175 DOMException on setting? 176 177 - SYNTAX_ERR: (self) 178 Raised if the specified CSS string value has a syntax error 179 (according to the attached property) or is unparsable. 180 - TODO: INVALID_MODIFICATION_ERR: 181 Raised if the specified CSS string value represents a different 182 type of values than the values allowed by the CSS property. 183 """ 184 cssvalue = CSSValue(cssText=cssText) 185 v = cssvalue._value 186 187 if v: 188 self.seqs[1] = cssvalue 189 190 # validate if known 191 if self.name in cssproperties.cssvalues and \ 192 not cssproperties.cssvalues[self.name](v): 193 194 linetoken = cssvalue._linetoken 195 self._log.warn( 196 u'Property: Invalid value for CSS2 property %s: %s' % 197 (self.name, v), linetoken, neverraise=True)
198 199 cssValue = property(_getCSSValue, _setCSSValue, 200 doc="(cssutils) CSSValue object of this property") 201 202
203 - def _getPriority(self):
204 try: 205 return self._priority 206 except AttributeError: 207 return u''
208
209 - def _setPriority(self, priority):
210 """ 211 priority 212 currently "!important" to set an important priority 213 or None or the empty string to set no priority only 214 215 Format 216 ====== 217 :: 218 219 prio 220 : IMPORTANT_SYM S* 221 ; 222 "!"{w}"important" {return IMPORTANT_SYM;} 223 224 DOMException on setting 225 226 - SYNTAX_ERR: (self) 227 Raised if the specified priority has a syntax error and is 228 unparsable. 229 In this case a priority not equal to None, "" or "!{w}important". 230 """ 231 if priority is None or priority == u'': 232 self._priority = u'' 233 self.seqs[2] = [] 234 else: 235 tokens = self._tokenize(priority) 236 if self.__invalidToken(tokens, 'priority'): 237 return 238 239 newpriority = None 240 for t in tokens: 241 if t.type in (self._ttypes.S, self._ttypes.COMMENT): # ignored 242 pass 243 elif self._ttypes.IMPORTANT_SYM == t.type and not newpriority: 244 newpriority = t.value.lower() 245 else: 246 self._log.error(u'Property: Unknown priority.', t) 247 return 248 249 if newpriority: 250 self._priority = newpriority 251 self.seqs[2] = ['!important'] 252 else: 253 self._log.error(u'Property: Unknown priority: "%s".' % priority)
254 255 priority = property(_getPriority, _setPriority, 256 doc="(cssutils) Priority of this property") 257 258 259 # DEPRECATED
260 - def _getValue(self):
261 if self.cssValue: return self.cssValue._value 262 else: return u''
263 - def _setValue(self, value):
264 self.cssValue.cssText = value
265 value = property(_getValue, _setValue, 266 doc="DEPRECATED string value of property")
267 268 269 if __name__ == '__main__': 270 p = _Property(u'color', 'red', '! important') 271 print p.name, p.value, p.priority 272