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
9
12
32
42
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
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
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
68 "CSSStyleDeclaration parse"
69
70 tests = {
71
72 u'TOP:0': u'top: 0',
73 u'top:0': u'top: 0',
74
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
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
96 ur'$top: 0': None,
97 ur'$: 0': u''
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
125
126 - def test_cssText(self):
127 "CSSStyleDeclaration.cssText"
128
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;'
138 s.cssText = test
139 self.assertEqual(exp, s.cssText)
140
141
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
152
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
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
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
219
233
235 "CSSStyleDeclaration.getProperties()"
236 s = cssutils.css.CSSStyleDeclaration(cssText=
237 u'/*1*/y:0;x:a !important;y:1; \\x:b;')
238 tests = {
239
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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
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
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
337 "CSSStyleDeclaration.removeProperty()"
338 s = cssutils.css.CSSStyleDeclaration()
339 css = ur'\x:0 !important; x:1; \x:2; x:3'
340
341
342 s.cssText = css
343 self.assertEqual(u'0', s.removeProperty('x'))
344 self.assertEqual(u'', s.cssText)
345
346
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
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
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
400 "CSSStyleDeclaration.length"
401 s = cssutils.css.CSSStyleDeclaration()
402
403
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
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
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
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
463 s.setProperty('UNKNOWN', 'red')
464
465 self.assertRaises(AttributeError, s.__getattribute__, 'UNKNOWN')
466 self.assertRaises(AttributeError, s.__delattr__, 'UNKNOWN')
467
468 self.assertEqual('red', s.getPropertyValue('UNKNOWN'))
469 self.assertEqual(
470 '''left: 1px;\nunknown: red''', s.cssText)
471
473 "CSSStyleDeclaration.__repr__(), .__str__()"
474 s = cssutils.css.CSSStyleDeclaration(cssText='a:1;b:2')
475
476 self.assert_("2" in str(s))
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