1 """Testcases for cssutils.css.cssstyledelaration.CSSStyleDeclaration."""
2 __author__ = '$LastChangedBy: cthedot $'
3 __date__ = '$LastChangedDate: 2008-01-13 00:05:26 +0100 (So, 13 Jan 2008) $'
4 __version__ = '$LastChangedRevision: 836 $'
5
6 import xml.dom
7 import basetest
8 import cssutils
9
11
14
29
31 "CSSStyleDeclaration parse"
32
33 tests = {
34
35 u'TOP:0': u'top: 0',
36 u'top:0': u'top: 0',
37
38 u'c\\olor: red; color:green': u'color: green',
39 u'color:g\\reen': u'color: g\\reen',
40
41 u'color:green': u'color: green',
42 u'color:green; color': u'color: green',
43 u'color:red; color; color:green': u'color: green',
44 u'color:green; color:': u'color: green',
45 u'color:red; color:; color:green': u'color: green',
46 u'color:green; color{;color:maroon}': u'color: green',
47
48
49
50
51 ur'''color: red;
52 voice-family: "\"}\"";
53 voice-family:inherit;
54 color: green;''': 'voice-family: inherit;\ncolor: green',
55 ur'''col\or: blue;
56 font-family: 'Courier New Times
57 color: red;
58 color: green;''': u'color: green',
59
60
61 ur'$top: 0': None,
62 ur'$: 0': u''
63 }
64 cssutils.ser.prefs.keepAllProperties = False
65 for test, exp in tests.items():
66 sh = cssutils.parseString('a { %s }' % test)
67 if exp is None:
68 exp = u'%s' % test
69 elif exp != u'':
70 exp = u'%s' % exp
71 self.assertEqual(exp, sh.cssRules[0].style.cssText)
72
73 cssutils.ser.prefs.useDefaults()
74
75 - def test_cssText(self):
76 "CSSStyleDeclaration.cssText"
77
78 s = cssutils.css.CSSStyleDeclaration()
79 tests = {
80 u'': u'',
81 u' ': u'',
82 u' \t \n ': u'',
83 u'/*x*/': u'/*x*/'
84 }
85 for test, exp in tests.items():
86 s.cssText = 'left: 0;'
87 s.cssText = test
88 self.assertEqual(exp, s.cssText)
89
90
91 s = cssutils.css.CSSStyleDeclaration()
92 tests = {
93 u'left: 0': u'left: 0',
94 u'left:0': u'left: 0',
95 u' left : 0 ': u'left: 0',
96 u'left: 0;': u'left: 0',
97 u'left: 0 !important ': u'left: 0 !important',
98 u'left:0!important': u'left: 0 !important',
99 u'left: 0; top: 1': u'left: 0;\ntop: 1',
100 u'/*1*/left: 0;/*2*/ top: 1/*3*/':
101 u'/*1*/\nleft: 0;\n/*2*/\ntop: 1/*3*/',
102 u'left:0; top:1;': u'left: 0;\ntop: 1',
103 u'/*1*/left: 0;/*2*/ top: 1;/*3*/':
104 u'/*1*/\nleft: 0;\n/*2*/\ntop: 1;\n/*3*/',
105 }
106 for test, exp in tests.items():
107 s.cssText = test
108 self.assertEqual(exp, s.cssText)
109
110
111 tests = {
112 u'top': xml.dom.SyntaxErr,
113 u'top:': xml.dom.SyntaxErr,
114 u'top : ': xml.dom.SyntaxErr,
115 u'top:!important': xml.dom.SyntaxErr,
116 u'top:!important;': xml.dom.SyntaxErr,
117 u'top:;': xml.dom.SyntaxErr,
118 u'top 0': xml.dom.SyntaxErr,
119 u'top 0;': xml.dom.SyntaxErr,
120
121 u':': xml.dom.SyntaxErr,
122 u':0': xml.dom.SyntaxErr,
123 u':0;': xml.dom.SyntaxErr,
124 u':0!important': xml.dom.SyntaxErr,
125 u':;': xml.dom.SyntaxErr,
126 u': ;': xml.dom.SyntaxErr,
127 u':!important;': xml.dom.SyntaxErr,
128 u': !important;': xml.dom.SyntaxErr,
129
130 u'0': xml.dom.SyntaxErr,
131 u'0!important': xml.dom.SyntaxErr,
132 u'0!important;': xml.dom.SyntaxErr,
133 u'0;': xml.dom.SyntaxErr,
134
135 u'!important': xml.dom.SyntaxErr,
136 u'!important;': xml.dom.SyntaxErr,
137
138 u';': xml.dom.SyntaxErr,
139 }
140 self.do_raise_r(tests)
141
142 - def test_getCssText(self):
143 "CSSStyleDeclaration.getCssText(separator)"
144 s = cssutils.css.CSSStyleDeclaration(cssText=u'a:1;b:2')
145 self.assertEqual(u'a: 1;\nb: 2', s.getCssText())
146 self.assertEqual(u'a: 1;b: 2', s.getCssText(separator=u''))
147 self.assertEqual(u'a: 1;/*x*/b: 2', s.getCssText(separator=u'/*x*/'))
148
160
162 "CSSStyleDeclaration.__iter__ and .item"
163 s = cssutils.css.CSSStyleDeclaration()
164 s.cssText = ur'''
165 color: red; c\olor: blue; CO\lor: green;
166 left: 1px !important; left: 0;
167 border: 0;
168 '''
169
170 ps = []
171 for p in s:
172 ps.append((p.literalname, p.value, p.priority))
173 self.assertEqual(len(ps), 3)
174 self.assertEqual(ps[0], (ur'co\lor', 'green', ''))
175 self.assertEqual(ps[1], (ur'left', '1px', '!important'))
176 self.assertEqual(ps[2], (ur'border', '0', ''))
177
178
179 self.assertEqual(s.length, 3)
180 self.assertEqual(s.item(0), u'color')
181 self.assertEqual(s.item(1), u'left')
182 self.assertEqual(s.item(2), u'border')
183 self.assertEqual(s.item(10), u'')
184
198
200 "CSSStyleDeclaration.getProperties()"
201 s = cssutils.css.CSSStyleDeclaration(cssText=
202 u'y:0;x:a !important;y:1; \\x:b;')
203 tests = {
204
205 (None, False): [(u'y', u'1', u''),
206 (u'x', u'a', u'!important')],
207 (None, True): [(u'y', u'0', u''),
208 (u'x', u'a', u'!important'),
209 (u'y', u'1', u''),
210 (u'\\x', u'b', u'')
211 ],
212 ('x', False): [(u'x', u'a', u'!important')],
213 ('\\x', False): [(u'x', u'a', u'!important')],
214 ('x', True): [(u'x', u'a', u'!important'),
215 (u'\\x', u'b', u'')],
216 ('\\x', True): [(u'x', u'a', u'!important'),
217 (u'\\x', u'b', u'')],
218 }
219 for test in tests:
220 name, all = test
221 expected = tests[test]
222 actual = s.getProperties(name, all)
223 self.assertEqual(len(expected), len(actual))
224 for i, ex in enumerate(expected):
225 a = actual[i]
226 self.assertEqual(ex, (a.literalname, a.value, a.priority))
227
228
229 s = cssutils.css.CSSStyleDeclaration(cssText=
230 u'a:0;b:1;a:1')
231 self.assertEqual(u'ba', u''.join([p.name for p in s]))
232
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
259 "CSSStyleDeclaration.getPropertyValue()"
260 s = cssutils.css.CSSStyleDeclaration()
261 self.assertEqual(u'', s.getPropertyValue('unset'))
262
263 s.setProperty(u'left', '0')
264 self.assertEqual(u'0', s.getPropertyValue('left'))
265
266 s.setProperty(u'border', '1px solid green')
267 self.assertEqual(u'1px solid green', s.getPropertyValue('border'))
268
269 s = cssutils.css.CSSStyleDeclaration(cssText='color: red;c\\olor: green')
270 self.assertEqual(u'green', s.getPropertyValue('color'))
271 self.assertEqual(u'green', s.getPropertyValue('c\\olor'))
272 self.assertEqual(u'red', s.getPropertyValue('color', False))
273 self.assertEqual(u'green', s.getPropertyValue('c\\olor', False))
274
275 tests = {
276 ur'color: red; color: green': 'green',
277 ur'c\olor: red; c\olor: green': 'green',
278 ur'color: red; c\olor: green': 'green',
279 ur'color: red !important; color: green !important': 'green',
280 ur'color: green !important; color: red': 'green',
281 }
282 for test in tests:
283 s = cssutils.css.CSSStyleDeclaration(cssText=test)
284 self.assertEqual(tests[test], s.getPropertyValue('color'))
285
287 "CSSStyleDeclaration.getPropertyPriority()"
288 s = cssutils.css.CSSStyleDeclaration()
289 self.assertEqual(u'', s.getPropertyPriority('unset'))
290
291 s.setProperty(u'left', u'0', u'!important')
292 self.assertEqual(u'!important', s.getPropertyPriority('left'))
293
294 s = cssutils.css.CSSStyleDeclaration(cssText=
295 'x: 1 !important;\\x: 2;x: 3 !important;\\x: 4')
296 self.assertEqual(u'!important', s.getPropertyPriority('x'))
297 self.assertEqual(u'!important', s.getPropertyPriority('\\x'))
298 self.assertEqual(u'!important', s.getPropertyPriority('x', True))
299 self.assertEqual(u'', s.getPropertyPriority('\\x', False))
300
302 "CSSStyleDeclaration.removeProperty()"
303 s = cssutils.css.CSSStyleDeclaration()
304 css = ur'\x:0 !important; x:1; \x:2; x:3'
305
306
307 s.cssText = css
308 self.assertEqual(u'0', s.removeProperty('x'))
309 self.assertEqual(u'', s.cssText)
310
311
312 s.cssText = css
313 self.assertEqual(u'3', s.removeProperty('x', normalize=False))
314 self.assertEqual(ur'\x: 0 !important;\x: 2', s.getCssText(separator=u''))
315 self.assertEqual(u'0', s.removeProperty(r'\x', normalize=False))
316 self.assertEqual(u'', s.cssText)
317
318 s.cssText = css
319 self.assertEqual(u'0', s.removeProperty(r'\x', normalize=False))
320 self.assertEqual(ur'x: 1;x: 3', s.getCssText(separator=u''))
321 self.assertEqual(u'3', s.removeProperty('x', normalize=False))
322 self.assertEqual(u'', s.cssText)
323
325 "CSSStyleDeclaration.setProperty()"
326 s = cssutils.css.CSSStyleDeclaration()
327 s.setProperty('top', '0', '!important')
328 self.assertEqual('0', s.getPropertyValue('top'))
329 self.assertEqual('!important', s.getPropertyPriority('top'))
330 s.setProperty('top', '1px')
331 self.assertEqual('1px', s.getPropertyValue('top'))
332 self.assertEqual('', s.getPropertyPriority('top'))
333
334 s.setProperty('top', '2px')
335 self.assertEqual('2px', s.getPropertyValue('top'))
336
337 s.setProperty('\\top', '3px')
338 self.assertEqual('3px', s.getPropertyValue('top'))
339
340 s.setProperty('\\top', '4px', normalize=False)
341 self.assertEqual('4px', s.getPropertyValue('top'))
342 self.assertEqual('4px', s.getPropertyValue('\\top', False))
343 self.assertEqual('3px', s.getPropertyValue('top', False))
344
345
346 s.setProperty('TOP', '0', '!IMPORTANT')
347 self.assertEqual('0', s.getPropertyValue('top'))
348 self.assertEqual('!important', s.getPropertyPriority('top'))
349
350 tests = {
351 (u'left', u'0px', u''): u'left: 0px',
352 (u'left', u'0px', u'!important'): u'left: 0px !important',
353 (u'LEFT', u'0px', u'!important'): u'left: 0px !important',
354 (u'left', u'0px', u'!important'): u'left: 0px !important',
355 }
356 for test, exp in tests.items():
357 s = cssutils.css.CSSStyleDeclaration()
358 n, v, p = test
359 s.setProperty(n, v, p)
360 self.assertEqual(exp, s.cssText)
361 self.assertEqual(v, s.getPropertyValue(n))
362 self.assertEqual(p, s.getPropertyPriority(n))
363
365 "CSSStyleDeclaration.length"
366 s = cssutils.css.CSSStyleDeclaration()
367
368
369 s.cssText = u'left: 0'
370 self.assertEqual(1, s.length)
371 self.assertEqual(1, len(s.seq))
372 s.cssText = u'/*1*/left/*x*/:/*x*/0/*x*/;/*2*/ top: 1;/*3*/'
373 self.assertEqual(2, s.length)
374 self.assertEqual(5, len(s.seq))
375
376
377 s = cssutils.css.CSSStyleDeclaration()
378 s.setProperty('top', '0', '!important')
379 self.assertEqual(1, s.length)
380 s.setProperty('top', '1px')
381 self.assertEqual(1, s.length)
382 s.setProperty('left', '1px')
383
385 "CSSStyleDeclaration.XXX(name)"
386 s = cssutils.css.CSSStyleDeclaration()
387 s.setProperty('top', '1px', '!important')
388
389 self.assertEqual('1px', s.getPropertyValue('top'))
390 self.assertEqual('1px', s.getPropertyValue('TOP'))
391 self.assertEqual('1px', s.getPropertyValue('T\op'))
392
393 self.assertEqual('!important', s.getPropertyPriority('top'))
394 self.assertEqual('!important', s.getPropertyPriority('TOP'))
395 self.assertEqual('!important', s.getPropertyPriority('T\op'))
396
397 s.setProperty('top', '2px', '!important')
398 self.assertEqual('2px', s.removeProperty('top'))
399 s.setProperty('top', '2px', '!important')
400 self.assertEqual('2px', s.removeProperty('TOP'))
401 s.setProperty('top', '2px', '!important')
402 self.assertEqual('2px', s.removeProperty('T\op'))
403
405 "CSSStyleDeclaration.$css2property get set del"
406 s = cssutils.css.CSSStyleDeclaration(
407 cssText='left: 1px;color: red; font-style: italic')
408
409 s.color = 'green'
410 s.fontStyle = 'normal'
411 self.assertEqual('green', s.color)
412 self.assertEqual('normal', s.fontStyle)
413 self.assertEqual('green', s.getPropertyValue('color'))
414 self.assertEqual('normal', s.getPropertyValue('font-style'))
415 self.assertEqual(
416 u'''left: 1px;\ncolor: green;\nfont-style: normal''',
417 s.cssText)
418
419 del s.color
420 self.assertEqual(
421 u'''left: 1px;\nfont-style: normal''',
422 s.cssText)
423 del s.fontStyle
424 self.assertEqual(u'left: 1px', s.cssText)
425
426 self.assertRaises(AttributeError, s.__setattr__, 'UNKNOWN', 'red')
427
428 s.setProperty('UNKNOWN', 'red')
429
430 self.assertRaises(AttributeError, s.__getattribute__, 'UNKNOWN')
431 self.assertRaises(AttributeError, s.__delattr__, 'UNKNOWN')
432
433 self.assertEqual('red', s.getPropertyValue('UNKNOWN'))
434 self.assertEqual(
435 '''left: 1px;\nunknown: red''', s.cssText)
436
438 "CSSStyleDeclaration.__repr__(), .__str__()"
439 s = cssutils.css.CSSStyleDeclaration(cssText='a:1;b:2')
440
441 self.assert_("2" in str(s))
442
443 s2 = eval(repr(s))
444 self.assert_(isinstance(s2, s.__class__))
445
446
447 if __name__ == '__main__':
448 import unittest
449 unittest.main()
450