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