Package cssutils :: Package tests :: Module test_cssstyledeclaration
[hide private]
[frames] | no frames]

Source Code for Module cssutils.tests.test_cssstyledeclaration

  1  """Testcases for cssutils.css.cssstyledelaration.CSSStyleDeclaration.""" 
  2  __author__ = '$LastChangedBy: doerwalter $' 
  3  __date__ = '$LastChangedDate: 2007-08-02 22:58:23 +0200 (Do, 02 Aug 2007) $' 
  4  __version__ = '0.9.2a2, $LastChangedRevision: 160 $' 
  5   
  6   
  7  import xml.dom 
  8   
  9  import basetest 
 10   
 11  import cssutils 
 12   
 13   
14 -class CSSStyleDeclarationTestCase(basetest.BaseTestCase):
15
16 - def setUp(self):
18
19 - def test_init(self):
20 "CSSStyleDeclaration.__init__()" 21 s = cssutils.css.CSSStyleDeclaration() 22 self.assertEqual(u'', s.cssText) 23 self.assertEqual(0, s.length) 24 self.assertEqual(None, s.parentRule) 25 26 s = cssutils.css.CSSStyleDeclaration(cssText='left: 0') 27 self.assertEqual(u'\n left: 0\n ', s.cssText) 28 self.assertEqual('0', s.getPropertyValue('left')) 29 30 sheet = cssutils.css.CSSStyleRule() 31 s = cssutils.css.CSSStyleDeclaration(sheet) 32 self.assertEqual(sheet, s.parentRule)
33 34
35 - def test_parseString(self):
36 "CSSStyleDeclaration parseString()" 37 # error but parse 38 39 tests = { 40 # property names are caseinsensitive 41 u'TOP:0': u'top: 0', 42 u'top:0': u'top: 0', 43 # simple escape 44 u'c\\olor: red; color:green': u'color: green', 45 u'color:g\\reen': u'color: g\\reen', 46 47 u'color:green': u'color: green', 48 u'color:green; color': u'color: green', 49 u'color:red; color; color:green': u'color: green', 50 u'color:green; color:': u'color: green', 51 u'color:red; color:; color:green': u'color: green', 52 u'color:green; color{;color:maroon}': u'color: green', 53 u'color:red; color{;color:maroon}; color:green': 54 u'color: green', 55 # tantek hack 56 ur'''color: red; 57 voice-family: "\"}\""; 58 voice-family:inherit; 59 color: green;''': 'color: green;\n voice-family: inherit' 60 } 61 for test, exp in tests.items(): 62 sh = cssutils.parseString('a { %s }' % test) 63 if exp is None: 64 exp = u'\n %s\n ' % test 65 elif exp != u'': 66 exp = u'\n %s\n ' % exp 67 self.assertEqual(exp, sh.cssRules[0].style.cssText)
68 69
70 - def test_cssText(self):
71 "CSSStyleDeclaration.cssText" 72 # empty 73 s = cssutils.css.CSSStyleDeclaration() 74 tests = { 75 u'': u'', 76 u' ': u'', 77 u' \t \n ': u'', 78 u'/*x*/': u'\n /*x*/\n ' 79 } 80 for test, exp in tests.items(): 81 s.cssText = 'left: 0;' # dummy to reset s 82 s.cssText = test 83 self.assertEqual(exp, s.cssText) 84 85 # normal 86 s = cssutils.css.CSSStyleDeclaration() 87 tests = { 88 u'left: 0': u'left: 0', 89 u'left:0': u'left: 0', 90 u' left : 0 ': u'left: 0', 91 u'left: 0;': u'left: 0', 92 u'left: 0 !important ': u'left: 0 !important', 93 u'left:0!important': u'left: 0 !important', 94 u'left: 0; top: 1': u'left: 0;\n top: 1', 95 } 96 for test, exp in tests.items(): 97 exp = u'\n %s\n ' % exp 98 s.cssText = test 99 self.assertEqual(exp, s.cssText) 100 101 # exception 102 tests = { 103 u'top': xml.dom.SyntaxErr, 104 u'top:': xml.dom.SyntaxErr, 105 u'top : ': xml.dom.SyntaxErr, 106 u'top:!important': xml.dom.SyntaxErr, 107 u'top:!important;': xml.dom.SyntaxErr, 108 u'top:;': xml.dom.SyntaxErr, 109 u'top 0': xml.dom.SyntaxErr, 110 u'top 0;': xml.dom.SyntaxErr, 111 112 u':': xml.dom.SyntaxErr, 113 u':0': xml.dom.SyntaxErr, 114 u':0;': xml.dom.SyntaxErr, 115 u':0!important': xml.dom.SyntaxErr, 116 u':;': xml.dom.SyntaxErr, 117 u':0;': xml.dom.SyntaxErr, 118 u':0!important;': xml.dom.SyntaxErr, 119 120 u'0': xml.dom.SyntaxErr, 121 u'0!important': xml.dom.SyntaxErr, 122 u'0!important;': xml.dom.SyntaxErr, 123 124 u'!important': xml.dom.SyntaxErr, 125 u'!important;': xml.dom.SyntaxErr, 126 127 u';': xml.dom.SyntaxErr, 128 } 129 self.do_raise_r(tests)
130 131
132 - def test_length(self):
133 "CSSStyleDeclaration.length" 134 s = cssutils.css.CSSStyleDeclaration() 135 136 # cssText 137 s.cssText = u'left: 0' 138 self.assertEqual(1, s.length) 139 self.assertEqual(1, len(s.seq)) 140 s.cssText = u'/*1*/left/*x*/:/*x*/0/*x*/;/*2*/ top: 1;/*3*/' 141 self.assertEqual(2, s.length) 142 self.assertEqual(5, len(s.seq)) 143 144 # set 145 s = cssutils.css.CSSStyleDeclaration() 146 s.setProperty('top', '0', '!important') 147 self.assertEqual(1, s.length) 148 s.setProperty('top', '1') 149 self.assertEqual(1, s.length) 150 s.setProperty('left', '1')
151 152
153 - def test_parentRule(self):
154 "CSSStyleDeclaration.parentRule" 155 s = cssutils.css.CSSStyleDeclaration() 156 sheet = cssutils.css.CSSStyleRule() 157 s.parentRule = sheet 158 self.assertEqual(sheet, s.parentRule) 159 160 sheet = cssutils.parseString(u'a{x:1}') 161 s = sheet.cssRules[0] 162 d = s.style 163 self.assertEqual(s, d.parentRule)
164 165
166 - def test_getPropertyCSSValue(self):
167 "CSSStyleDeclaration.getPropertyCSSValue()" 168 s = cssutils.css.CSSStyleDeclaration() 169 170 # not set 171 self.assertEqual(None, s.getPropertyCSSValue('color')) 172 173 # shorthand 174 SHORTHAND = [ 175 u'background', 176 u'border', 177 u'border-left', u'border-right', 178 u'border-top', u'border-bottom', 179 u'border-color', u'border-style', u'border-width', 180 u'cue', 181 u'font', 182 u'list-style', 183 u'margin', 184 u'outline', 185 u'padding', 186 u'pause'] 187 for short in SHORTHAND: 188 s.setProperty(short, u'inherit') 189 self.assertEqual(None, s.getPropertyCSSValue(short))
190 191
192 - def test_getPropertyPriority(self):
193 "CSSStyleDeclaration.getPropertyPriority()" 194 s = cssutils.css.CSSStyleDeclaration() 195 self.assertEqual(u'', s.getPropertyPriority('unset')) 196 197 s.setProperty(u'left', u'0', u'!important') 198 self.assertEqual(u'!important', s.getPropertyPriority('left'))
199 200
201 - def test_getPropertyValue(self):
202 "CSSStyleDeclaration.getPropertyValue()" 203 s = cssutils.css.CSSStyleDeclaration() 204 self.assertEqual(u'', s.getPropertyValue('unset')) 205 206 s.setProperty(u'left', '0') 207 self.assertEqual(u'0', s.getPropertyValue('left')) 208 209 s.setProperty(u'border', '1px solid green') 210 self.assertEqual(u'1px solid green', s.getPropertyValue('border'))
211 212
214 "CSSStyleDeclaration.getSameNamePropertyList()" 215 s = cssutils.css.CSSStyleDeclaration(cssText='color: red') 216 pl = s.getSameNamePropertyList('color') 217 self.assertEqual('color', pl.name) 218 self.assertEqual(1, len(pl)) 219 self.assertEqual(0, pl._currentIndex()) 220 221 s.setProperty('C\olor', 'green', '!important', overwrite=False) 222 self.assertEqual('color', pl.name) 223 self.assertEqual(2, len(pl)) 224 self.assertEqual(1, pl._currentIndex()) 225 ## self.assertEqual( 226 ## '[<Property> color: red , <Property> c\\olor: green !important]', 227 ## str(pl)) 228 229 s.setProperty('COLOR', 'blue', overwrite=False) 230 self.assertEqual('color', pl.name) 231 self.assertEqual(3, len(pl)) 232 self.assertEqual(1, pl._currentIndex())
233 ## self.assertEqual( 234 ## '[<Property> color: red , <Property> c\\olor: green !important, <Property> color: blue ]', 235 ## str(pl)) 236 237
238 - def test_item(self):
239 "CSSStyleDeclaration.item()" 240 _props = ('left', 'top', 'right') 241 s = cssutils.css.CSSStyleDeclaration(cssText= 242 '\left:0;TOP:1;right:3') 243 for i in range(0,3): 244 self.assertEqual(_props[i], s.item(i)) 245 self.assertEqual(_props[-1-i], s.item(-1-i)) 246 self.assertEqual(u'', s.item(3)) 247 self.assertEqual(u'', s.item(-4))
248 249
250 - def test_removeProperty(self):
251 "CSSStyleDeclaration.removeProperty()" 252 s = cssutils.css.CSSStyleDeclaration(cssText='top: 0 !important') 253 self.assertEqual('0', s.removeProperty('top')) 254 self.assertEqual(0, s.length) 255 self.assertEqual('', s.removeProperty('top')) 256 self.assertEqual(0, s.length)
257 258
260 "CSSStyleDeclaration.setProperty(overwrite=True)" 261 s = cssutils.css.CSSStyleDeclaration() 262 s.cssText = 'color: red; top: 1px' 263 self.assertEqual(2, s.length) 264 pl = s.getSameNamePropertyList('color') 265 self.assertEqual(1, len(pl)) 266 267 # overwrite (default) 268 s.setProperty('color', 'green', '!important') 269 self.assertEqual(2, s.length) 270 self.assertEqual('color', s.item(0)) 271 self.assertEqual('top', s.item(1)) 272 self.assertEqual('green', s.getPropertyValue('color')) 273 pl = s.getSameNamePropertyList('color') 274 self.assertEqual(1, len(pl)) 275 276 # append value 277 s.setProperty('color', 'blue', overwrite=False) 278 self.assertEqual(2, s.length) 279 self.assertEqual('color', s.item(0)) 280 self.assertEqual('top', s.item(1)) 281 # new value is not important 282 self.assertEqual('green', s.getPropertyValue('color')) 283 pl = s.getSameNamePropertyList('color') 284 self.assertEqual(2, len(pl)) 285 286 # overwrite 287 s.setProperty('color', 'red', overwrite=True) 288 self.assertEqual(2, s.length) 289 self.assertEqual('color', s.item(0)) 290 self.assertEqual('top', s.item(1)) 291 self.assertEqual('red', s.getPropertyValue('color')) 292 pl = s.getSameNamePropertyList('color') 293 self.assertEqual(1, len(pl))
294 295
296 - def test_setProperty(self):
297 "CSSStyleDeclaration.setProperty()" 298 s = cssutils.css.CSSStyleDeclaration() 299 s.setProperty('top', '0', '!important') 300 self.assertEqual('0', s.getPropertyValue('top')) 301 self.assertEqual('!important', s.getPropertyPriority('top')) 302 s.setProperty('top', '1') 303 self.assertEqual('1', s.getPropertyValue('top')) 304 self.assertEqual('', s.getPropertyPriority('top')) 305 306 # case insensitive 307 s.setProperty('TOP', '0', '!IMPORTANT') 308 self.assertEqual('0', s.getPropertyValue('top')) 309 self.assertEqual('!important', s.getPropertyPriority('top')) 310 self.assertEqual('0', s.getPropertyValue('top')) 311 self.assertEqual('!important', s.getPropertyPriority('top')) 312 313 tests = { 314 (u'left', u'0px', u''): u'left: 0px', 315 (u'left', u'0px', u'!important'): u'left: 0px !important', 316 (u'LEFT', u'0px', u'!important'): u'left: 0px !important', 317 (u'left', u'0px', u'!important'): u'left: 0px !important', 318 } 319 for test, exp in tests.items(): 320 s = cssutils.css.CSSStyleDeclaration() 321 n, v, p = test 322 exp = u'\n %s\n ' % exp 323 s.setProperty(n, v, p) 324 self.assertEqual(exp, s.cssText) 325 self.assertEqual(v, s.getPropertyValue(n)) 326 self.assertEqual(p, s.getPropertyPriority(n))
327 328
329 - def test_nameParameter(self):
330 "CSSStyleDeclaration.XXX(name)" 331 s = cssutils.css.CSSStyleDeclaration() 332 s.setProperty('top', '1px', '!important') 333 334 self.assertEqual('1px', s.getPropertyValue('top')) 335 self.assertEqual('1px', s.getPropertyValue('TOP')) 336 self.assertEqual('1px', s.getPropertyValue('T\op')) 337 338 ## self.assertEqual('1px', s.getPropertyCSSValue('top')) 339 ## self.assertEqual('1px', s.getPropertyCSSValue('TOP')) 340 ## self.assertEqual('1px', s.getPropertyCSSValue('T\op')) 341 342 self.assertEqual('!important', s.getPropertyPriority('top')) 343 self.assertEqual('!important', s.getPropertyPriority('TOP')) 344 self.assertEqual('!important', s.getPropertyPriority('T\op')) 345 346 s.setProperty('top', '2px', '!important') 347 self.assertEqual('2px', s.removeProperty('top')) 348 s.setProperty('top', '2px', '!important') 349 self.assertEqual('2px', s.removeProperty('TOP')) 350 s.setProperty('top', '2px', '!important') 351 self.assertEqual('2px', s.removeProperty('T\op'))
352 353
354 - def test_css2properties(self):
355 "CSSStyleDeclaration.$css2property get set del" 356 s = cssutils.css.CSSStyleDeclaration( 357 cssText='left: 1px;color: red; font-style: italic') 358 359 s.color = 'green' 360 s.fontStyle = 'normal' 361 self.assertEqual('green', s.color) 362 self.assertEqual('normal', s.fontStyle) 363 self.assertEqual('green', s.getPropertyValue('color')) 364 self.assertEqual('normal', s.getPropertyValue('font-style')) 365 self.assertEqual( 366 '''\n left: 1px;\n color: green;\n font-style: normal\n ''', 367 s.cssText) 368 369 del s.color 370 self.assertEqual( 371 '''\n left: 1px;\n font-style: normal\n ''', 372 s.cssText) 373 del s.fontStyle 374 self.assertEqual( 375 '''\n left: 1px\n ''', s.cssText) 376 377 self.assertRaises(AttributeError, s.__setattr__, 'UNKNOWN', 'red') 378 # unknown properties must be set with setProperty! 379 s.setProperty('UNKNOWN', 'red') 380 # but are still not usable as property! 381 self.assertRaises(AttributeError, s.__getattribute__, 'UNKNOWN') 382 self.assertRaises(AttributeError, s.__delattr__, 'UNKNOWN') 383 # but are kept 384 self.assertEqual('red', s.getPropertyValue('UNKNOWN')) 385 self.assertEqual( 386 '''\n left: 1px;\n unknown: red\n ''', s.cssText)
387 388 389 if __name__ == '__main__': 390 import unittest 391 unittest.main() 392