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: cthedot $' 
  3  __date__ = '$LastChangedDate: 2008-02-03 15:16:50 +0100 (So, 03 Feb 2008) $' 
  4  __version__ = '$LastChangedRevision: 981 $' 
  5   
  6  import xml.dom 
  7  import basetest 
  8  import cssutils 
  9   
10 -class CSSStyleDeclarationTestCase(basetest.BaseTestCase):
11
12 - def setUp(self):
14
15 - def test_init(self):
16 "CSSStyleDeclaration.__init__()" 17 s = cssutils.css.CSSStyleDeclaration() 18 self.assertEqual(u'', s.cssText) 19 self.assertEqual(0, s.length) 20 self.assertEqual(None, s.parentRule) 21 22 s = cssutils.css.CSSStyleDeclaration(cssText='left: 0') 23 self.assertEqual(u'left: 0', s.cssText) 24 self.assertEqual('0', s.getPropertyValue('left')) 25 26 sheet = cssutils.css.CSSStyleRule() 27 s = cssutils.css.CSSStyleDeclaration(parentRule=sheet) 28 self.assertEqual(sheet, s.parentRule) 29 30 # should not be used but ordered paramter test 31 s = cssutils.css.CSSStyleDeclaration('top: 0', sheet) 32 self.assertEqual(u'top: 0', s.cssText) 33 self.assertEqual(sheet, s.parentRule)
34
35 - def test__contains__(self):
36 "CSSStyleDeclaration.__contains__(nameOrProperty)" 37 s = cssutils.css.CSSStyleDeclaration(cssText=r'x: 1;\y: 2') 38 for test in ('x', r'x', 'y', r'y'): 39 self.assert_(test in s) 40 self.assert_(test.upper() in s) 41 self.assert_(cssutils.css.Property(test, '1') in s) 42 self.assert_('z' not in s) 43 self.assert_(cssutils.css.Property('z', '1') not in s)
44
45 - def test__iter__item(self):
46 "CSSStyleDeclaration.__iter__ and .item" 47 s = cssutils.css.CSSStyleDeclaration() 48 s.cssText = ur''' 49 color: red; c\olor: blue; CO\lor: green; 50 left: 1px !important; left: 0; 51 border: 0; 52 ''' 53 # __iter__ 54 ps = [] 55 for p in s: 56 ps.append((p.literalname, p.value, p.priority)) 57 self.assertEqual(len(ps), 3) 58 self.assertEqual(ps[0], (ur'co\lor', 'green', '')) 59 self.assertEqual(ps[1], (ur'left', '1px', 'important')) 60 self.assertEqual(ps[2], (ur'border', '0', '')) 61 62 # item 63 self.assertEqual(s.length, 3) 64 self.assertEqual(s.item(0), u'color') 65 self.assertEqual(s.item(1), u'left') 66 self.assertEqual(s.item(2), u'border') 67 self.assertEqual(s.item(10), u'')
68
69 - def test_parse(self):
70 "CSSStyleDeclaration parse" 71 # error but parse 72 tests = { 73 # property names are caseinsensitive 74 u'TOP:0': u'top: 0', 75 u'top:0': u'top: 0', 76 # simple escape 77 u'c\\olor: red; color:green': u'color: green', 78 u'color:g\\reen': u'color: g\\reen', 79 80 u'color:green': u'color: green', 81 u'color:green; color': u'color: green', 82 u'color:red; color; color:green': u'color: green', 83 u'color:green; color:': u'color: green', 84 u'color:red; color:; color:green': u'color: green', 85 u'color:green; color{;color:maroon}': u'color: green', 86 u'color:red; color{;color:maroon}; color:green': u'color: green', 87 # tantek hack 88 ur'''color: red; 89 voice-family: "\"}\""; 90 voice-family:inherit; 91 color: green;''': 'voice-family: inherit;\ncolor: green', 92 ur'''col\or: blue; 93 font-family: 'Courier New Times 94 color: red; 95 color: green;''': u'color: green', 96 97 # special IE hacks are preserved for now 98 ur'$top: 0': None, 99 ur'$: 0': u'' # really invalid! 100 } 101 cssutils.ser.prefs.keepAllProperties = False 102 for test, exp in tests.items(): 103 sh = cssutils.parseString('a { %s }' % test) 104 if exp is None: 105 exp = u'%s' % test 106 elif exp != u'': 107 exp = u'%s' % exp 108 self.assertEqual(exp, sh.cssRules[0].style.cssText) 109 110 cssutils.ser.prefs.useDefaults()
111
112 - def test_serialize(self):
113 "CSSStyleDeclaration serialize" 114 s = cssutils.css.CSSStyleDeclaration() 115 tests = { 116 u'a:1 !important; a:2;b:1': (u'a: 1 !important;\nb: 1', 117 u'a: 1 !important;\na: 2;\nb: 1') 118 } 119 for test, exp in tests.items(): 120 s.cssText = test 121 cssutils.ser.prefs.keepAllProperties = False 122 self.assertEqual(exp[0], s.cssText) 123 cssutils.ser.prefs.keepAllProperties = True 124 self.assertEqual(exp[1], s.cssText) 125 126 cssutils.ser.prefs.useDefaults()
127
128 - def test_cssText(self):
129 "CSSStyleDeclaration.cssText" 130 # empty 131 s = cssutils.css.CSSStyleDeclaration() 132 tests = { 133 u'': u'', 134 u' ': u'', 135 u' \t \n ': u'', 136 u'/*x*/': u'/*x*/' 137 } 138 for test, exp in tests.items(): 139 s.cssText = 'left: 0;' # dummy to reset s 140 s.cssText = test 141 self.assertEqual(exp, s.cssText) 142 143 # normal 144 s = cssutils.css.CSSStyleDeclaration() 145 tests = { 146 u'left: 0': u'left: 0', 147 u'left:0': u'left: 0', 148 u' left : 0 ': u'left: 0', 149 u'left: 0;': u'left: 0', 150 u'left: 0 !important ': u'left: 0 !important', 151 u'left:0!important': u'left: 0 !important', 152 u'left: 0; top: 1': u'left: 0;\ntop: 1', 153 u'/*1*/left: 0;/*2*/ top: 1/*3*/': 154 u'/*1*/\nleft: 0;\n/*2*/\ntop: 1/*3*/', 155 u'left:0; top:1;': u'left: 0;\ntop: 1', 156 u'/*1*/left: 0;/*2*/ top: 1;/*3*/': 157 u'/*1*/\nleft: 0;\n/*2*/\ntop: 1;\n/*3*/', 158 } 159 for test, exp in tests.items(): 160 s.cssText = test 161 self.assertEqual(exp, s.cssText) 162 163 # exception 164 tests = { 165 u'top': xml.dom.SyntaxErr, 166 u'top:': xml.dom.SyntaxErr, 167 u'top : ': xml.dom.SyntaxErr, 168 u'top:!important': xml.dom.SyntaxErr, 169 u'top:!important;': xml.dom.SyntaxErr, 170 u'top:;': xml.dom.SyntaxErr, 171 u'top 0': xml.dom.SyntaxErr, 172 u'top 0;': xml.dom.SyntaxErr, 173 174 u':': xml.dom.SyntaxErr, 175 u':0': xml.dom.SyntaxErr, 176 u':0;': xml.dom.SyntaxErr, 177 u':0!important': xml.dom.SyntaxErr, 178 u':;': xml.dom.SyntaxErr, 179 u': ;': xml.dom.SyntaxErr, 180 u':!important;': xml.dom.SyntaxErr, 181 u': !important;': xml.dom.SyntaxErr, 182 183 u'0': xml.dom.SyntaxErr, 184 u'0!important': xml.dom.SyntaxErr, 185 u'0!important;': xml.dom.SyntaxErr, 186 u'0;': xml.dom.SyntaxErr, 187 188 u'!important': xml.dom.SyntaxErr, 189 u'!important;': xml.dom.SyntaxErr, 190 191 u';': xml.dom.SyntaxErr, 192 } 193 self.do_raise_r(tests)
194
195 - def test_getCssText(self):
196 "CSSStyleDeclaration.getCssText(separator)" 197 s = cssutils.css.CSSStyleDeclaration(cssText=u'a:1;b:2') 198 self.assertEqual(u'a: 1;\nb: 2', s.getCssText()) 199 self.assertEqual(u'a: 1;b: 2', s.getCssText(separator=u'')) 200 self.assertEqual(u'a: 1;/*x*/b: 2', s.getCssText(separator=u'/*x*/'))
201
202 - def test_parentRule(self):
203 "CSSStyleDeclaration.parentRule" 204 s = cssutils.css.CSSStyleDeclaration() 205 sheet = cssutils.css.CSSStyleRule() 206 s.parentRule = sheet 207 self.assertEqual(sheet, s.parentRule) 208 209 sheet = cssutils.parseString(u'a{x:1}') 210 s = sheet.cssRules[0] 211 d = s.style 212 self.assertEqual(s, d.parentRule)
213
214 - def test_getProperty(self):
215 "CSSStyleDeclaration.getProperty" 216 s = cssutils.css.CSSStyleDeclaration() 217 P = cssutils.css.Property 218 s.cssText = ur''' 219 color: red; c\olor: blue; CO\lor: green; 220 left: 1px !important; left: 0; 221 border: 0; 222 ''' 223 self.assertEqual(s.getProperty('color').cssText, ur'co\lor: green') 224 self.assertEqual(s.getProperty(r'COLO\r').cssText, ur'co\lor: green') 225 self.assertEqual(s.getProperty('left').cssText, ur'left: 1px !important') 226 self.assertEqual(s.getProperty('border').cssText, ur'border: 0')
227
228 - def test_getProperties(self):
229 "CSSStyleDeclaration.getProperties()" 230 s = cssutils.css.CSSStyleDeclaration(cssText= 231 u'/*1*/y:0;x:a !important;y:1; \\x:b;') 232 tests = { 233 # name, all 234 (None, False): [(u'y', u'1', u''), 235 (u'x', u'a', u'important')], 236 (None, True): [(u'y', u'0', u''), 237 (u'x', u'a', u'important'), 238 (u'y', u'1', u''), 239 (u'\\x', u'b', u'') 240 ], 241 ('x', False): [(u'x', u'a', u'important')], 242 ('\\x', False): [(u'x', u'a', u'important')], 243 ('x', True): [(u'x', u'a', u'important'), 244 (u'\\x', u'b', u'')], 245 ('\\x', True): [(u'x', u'a', u'important'), 246 (u'\\x', u'b', u'')], 247 } 248 for test in tests: 249 name, all = test 250 expected = tests[test] 251 actual = s.getProperties(name, all) 252 self.assertEqual(len(expected), len(actual)) 253 for i, ex in enumerate(expected): 254 a = actual[i] 255 self.assertEqual(ex, (a.literalname, a.value, a.priority)) 256 257 # order is be effective properties set 258 s = cssutils.css.CSSStyleDeclaration(cssText= 259 u'a:0;b:1;a:1') 260 self.assertEqual(u'ba', u''.join([p.name for p in s]))
261
262 - def test_getPropertyCSSValue(self):
263 "CSSStyleDeclaration.getPropertyCSSValue()" 264 s = cssutils.css.CSSStyleDeclaration(cssText='color: red;c\\olor: green') 265 self.assertEqual(u'green', s.getPropertyCSSValue('color').cssText) 266 self.assertEqual(u'green', s.getPropertyCSSValue('c\\olor').cssText) 267 self.assertEqual(u'red', s.getPropertyCSSValue('color', False).cssText) 268 self.assertEqual(u'green', s.getPropertyCSSValue('c\\olor', False).cssText)
269 # # shorthand CSSValue should be None 270 # SHORTHAND = [ 271 # u'background', 272 # u'border', 273 # u'border-left', u'border-right', 274 # u'border-top', u'border-bottom', 275 # u'border-color', u'border-style', u'border-width', 276 # u'cue', 277 # u'font', 278 # u'list-style', 279 # u'margin', 280 # u'outline', 281 # u'padding', 282 # u'pause'] 283 # for short in SHORTHAND: 284 # s.setProperty(short, u'inherit') 285 # self.assertEqual(None, s.getPropertyCSSValue(short)) 286
287 - def test_getPropertyValue(self):
288 "CSSStyleDeclaration.getPropertyValue()" 289 s = cssutils.css.CSSStyleDeclaration() 290 self.assertEqual(u'', s.getPropertyValue('unset')) 291 292 s.setProperty(u'left', '0') 293 self.assertEqual(u'0', s.getPropertyValue('left')) 294 295 s.setProperty(u'border', '1px solid green') 296 self.assertEqual(u'1px solid green', s.getPropertyValue('border')) 297 298 s = cssutils.css.CSSStyleDeclaration(cssText='color: red;c\\olor: green') 299 self.assertEqual(u'green', s.getPropertyValue('color')) 300 self.assertEqual(u'green', s.getPropertyValue('c\\olor')) 301 self.assertEqual(u'red', s.getPropertyValue('color', False)) 302 self.assertEqual(u'green', s.getPropertyValue('c\\olor', False)) 303 304 tests = { 305 ur'color: red; color: green': 'green', 306 ur'c\olor: red; c\olor: green': 'green', 307 ur'color: red; c\olor: green': 'green', 308 ur'color: red !important; color: green !important': 'green', 309 ur'color: green !important; color: red': 'green', 310 } 311 for test in tests: 312 s = cssutils.css.CSSStyleDeclaration(cssText=test) 313 self.assertEqual(tests[test], s.getPropertyValue('color'))
314
315 - def test_getPropertyPriority(self):
316 "CSSStyleDeclaration.getPropertyPriority()" 317 s = cssutils.css.CSSStyleDeclaration() 318 self.assertEqual(u'', s.getPropertyPriority('unset')) 319 320 s.setProperty(u'left', u'0', u'!important') 321 self.assertEqual(u'important', s.getPropertyPriority('left')) 322 323 s = cssutils.css.CSSStyleDeclaration(cssText= 324 'x: 1 !important;\\x: 2;x: 3 !important;\\x: 4') 325 self.assertEqual(u'important', s.getPropertyPriority('x')) 326 self.assertEqual(u'important', s.getPropertyPriority('\\x')) 327 self.assertEqual(u'important', s.getPropertyPriority('x', True)) 328 self.assertEqual(u'', s.getPropertyPriority('\\x', False))
329
330 - def test_removeProperty(self):
331 "CSSStyleDeclaration.removeProperty()" 332 s = cssutils.css.CSSStyleDeclaration() 333 css = ur'\x:0 !important; x:1; \x:2; x:3' 334 335 # normalize=True DEFAULT 336 s.cssText = css 337 self.assertEqual(u'0', s.removeProperty('x')) 338 self.assertEqual(u'', s.cssText) 339 340 # normalize=False 341 s.cssText = css 342 self.assertEqual(u'3', s.removeProperty('x', normalize=False)) 343 self.assertEqual(ur'\x: 0 !important;\x: 2', s.getCssText(separator=u'')) 344 self.assertEqual(u'0', s.removeProperty(r'\x', normalize=False)) 345 self.assertEqual(u'', s.cssText) 346 347 s.cssText = css 348 self.assertEqual(u'0', s.removeProperty(r'\x', normalize=False)) 349 self.assertEqual(ur'x: 1;x: 3', s.getCssText(separator=u'')) 350 self.assertEqual(u'3', s.removeProperty('x', normalize=False)) 351 self.assertEqual(u'', s.cssText)
352
353 - def test_setProperty(self):
354 "CSSStyleDeclaration.setProperty()" 355 s = cssutils.css.CSSStyleDeclaration() 356 s.setProperty('top', '0', '!important') 357 self.assertEqual('0', s.getPropertyValue('top')) 358 self.assertEqual('important', s.getPropertyPriority('top')) 359 s.setProperty('top', '1px') 360 self.assertEqual('1px', s.getPropertyValue('top')) 361 self.assertEqual('', s.getPropertyPriority('top')) 362 363 s.setProperty('top', '2px') 364 self.assertEqual('2px', s.getPropertyValue('top')) 365 366 s.setProperty('\\top', '3px') 367 self.assertEqual('3px', s.getPropertyValue('top')) 368 369 s.setProperty('\\top', '4px', normalize=False) 370 self.assertEqual('4px', s.getPropertyValue('top')) 371 self.assertEqual('4px', s.getPropertyValue('\\top', False)) 372 self.assertEqual('3px', s.getPropertyValue('top', False)) 373 374 # case insensitive 375 s.setProperty('TOP', '0', '!IMPORTANT') 376 self.assertEqual('0', s.getPropertyValue('top')) 377 self.assertEqual('important', s.getPropertyPriority('top')) 378 379 tests = { 380 (u'left', u'0px', u''): u'left: 0px', 381 (u'left', u'0px', u'important'): u'left: 0px !important', 382 (u'LEFT', u'0px', u'important'): u'left: 0px !important', 383 (u'left', u'0px', u'important'): u'left: 0px !important', 384 } 385 for test, exp in tests.items(): 386 s = cssutils.css.CSSStyleDeclaration() 387 n, v, p = test 388 s.setProperty(n, v, p) 389 self.assertEqual(exp, s.cssText) 390 self.assertEqual(v, s.getPropertyValue(n)) 391 self.assertEqual(p, s.getPropertyPriority(n))
392
393 - def test_length(self):
394 "CSSStyleDeclaration.length" 395 s = cssutils.css.CSSStyleDeclaration() 396 397 # cssText 398 s.cssText = u'left: 0' 399 self.assertEqual(1, s.length) 400 self.assertEqual(1, len(s.seq)) 401 s.cssText = u'/*1*/left/*x*/:/*x*/0/*x*/;/*2*/ top: 1;/*3*/' 402 self.assertEqual(2, s.length) 403 self.assertEqual(5, len(s.seq)) 404 405 # set 406 s = cssutils.css.CSSStyleDeclaration() 407 s.setProperty('top', '0', '!important') 408 self.assertEqual(1, s.length) 409 s.setProperty('top', '1px') 410 self.assertEqual(1, s.length) 411 s.setProperty('left', '1px')
412
413 - def test_nameParameter(self):
414 "CSSStyleDeclaration.XXX(name)" 415 s = cssutils.css.CSSStyleDeclaration() 416 s.setProperty('top', '1px', '!important') 417 418 self.assertEqual('1px', s.getPropertyValue('top')) 419 self.assertEqual('1px', s.getPropertyValue('TOP')) 420 self.assertEqual('1px', s.getPropertyValue('T\op')) 421 422 self.assertEqual('important', s.getPropertyPriority('top')) 423 self.assertEqual('important', s.getPropertyPriority('TOP')) 424 self.assertEqual('important', s.getPropertyPriority('T\op')) 425 426 s.setProperty('top', '2px', '!important') 427 self.assertEqual('2px', s.removeProperty('top')) 428 s.setProperty('top', '2px', '!important') 429 self.assertEqual('2px', s.removeProperty('TOP')) 430 s.setProperty('top', '2px', '!important') 431 self.assertEqual('2px', s.removeProperty('T\op'))
432
433 - def test_css2properties(self):
434 "CSSStyleDeclaration.$css2property get set del" 435 s = cssutils.css.CSSStyleDeclaration( 436 cssText='left: 1px;color: red; font-style: italic') 437 438 s.color = 'green' 439 s.fontStyle = 'normal' 440 self.assertEqual('green', s.color) 441 self.assertEqual('normal', s.fontStyle) 442 self.assertEqual('green', s.getPropertyValue('color')) 443 self.assertEqual('normal', s.getPropertyValue('font-style')) 444 self.assertEqual( 445 u'''left: 1px;\ncolor: green;\nfont-style: normal''', 446 s.cssText) 447 448 del s.color 449 self.assertEqual( 450 u'''left: 1px;\nfont-style: normal''', 451 s.cssText) 452 del s.fontStyle 453 self.assertEqual(u'left: 1px', s.cssText) 454 455 self.assertRaises(AttributeError, s.__setattr__, 'UNKNOWN', 'red') 456 # unknown properties must be set with setProperty! 457 s.setProperty('UNKNOWN', 'red') 458 # but are still not usable as property! 459 self.assertRaises(AttributeError, s.__getattribute__, 'UNKNOWN') 460 self.assertRaises(AttributeError, s.__delattr__, 'UNKNOWN') 461 # but are kept 462 self.assertEqual('red', s.getPropertyValue('UNKNOWN')) 463 self.assertEqual( 464 '''left: 1px;\nunknown: red''', s.cssText)
465
466 - def test_reprANDstr(self):
467 "CSSStyleDeclaration.__repr__(), .__str__()" 468 s = cssutils.css.CSSStyleDeclaration(cssText='a:1;b:2') 469 470 self.assert_("2" in str(s)) # length 471 472 s2 = eval(repr(s)) 473 self.assert_(isinstance(s2, s.__class__))
474 475 476 if __name__ == '__main__': 477 import unittest 478 unittest.main() 479