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
11
14
29
38
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
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
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
64 "CSSStyleDeclaration parse"
65
66 tests = {
67
68 u'TOP:0': u'top: 0',
69 u'top:0': u'top: 0',
70
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
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
92 ur'$top: 0': None,
93 ur'$: 0': u''
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
121
122 - def test_cssText(self):
123 "CSSStyleDeclaration.cssText"
124
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;'
134 s.cssText = test
135 self.assertEqual(exp, s.cssText)
136
137
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
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
207
221
223 "CSSStyleDeclaration.getProperties()"
224 s = cssutils.css.CSSStyleDeclaration(cssText=
225 u'/*1*/y:0;x:a !important;y:1; \\x:b;')
226 tests = {
227
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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
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
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
325 "CSSStyleDeclaration.removeProperty()"
326 s = cssutils.css.CSSStyleDeclaration()
327 css = ur'\x:0 !important; x:1; \x:2; x:3'
328
329
330 s.cssText = css
331 self.assertEqual(u'0', s.removeProperty('x'))
332 self.assertEqual(u'', s.cssText)
333
334
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
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
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
388 "CSSStyleDeclaration.length"
389 s = cssutils.css.CSSStyleDeclaration()
390
391
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
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
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
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
451 s.setProperty('UNKNOWN', 'red')
452
453 self.assertRaises(AttributeError, s.__getattribute__, 'UNKNOWN')
454 self.assertRaises(AttributeError, s.__delattr__, 'UNKNOWN')
455
456 self.assertEqual('red', s.getPropertyValue('UNKNOWN'))
457 self.assertEqual(
458 '''left: 1px;\nunknown: red''', s.cssText)
459
461 "CSSStyleDeclaration.__repr__(), .__str__()"
462 s = cssutils.css.CSSStyleDeclaration(cssText='a:1;b:2')
463
464 self.assert_("2" in str(s))
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