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