1 """Testcases for cssutils.css.cssstyledelaration.CSSStyleDeclaration."""
2 __author__ = '$LastChangedBy: cthedot $'
3 __date__ = '$LastChangedDate: 2008-02-03 15:16:50 +0100 (So, 03 Feb 2008) $'
4 __version__ = '$LastChangedRevision: 981 $'
5
6 import xml.dom
7 import basetest
8 import cssutils
9
11
14
34
44
46 "CSSStyleDeclaration.__iter__ and .item"
47 s = cssutils.css.CSSStyleDeclaration()
48 s.cssText = ur'''
49 color: red; c\olor: blue; CO\lor: green;
50 left: 1px !important; left: 0;
51 border: 0;
52 '''
53
54 ps = []
55 for p in s:
56 ps.append((p.literalname, p.value, p.priority))
57 self.assertEqual(len(ps), 3)
58 self.assertEqual(ps[0], (ur'co\lor', 'green', ''))
59 self.assertEqual(ps[1], (ur'left', '1px', 'important'))
60 self.assertEqual(ps[2], (ur'border', '0', ''))
61
62
63 self.assertEqual(s.length, 3)
64 self.assertEqual(s.item(0), u'color')
65 self.assertEqual(s.item(1), u'left')
66 self.assertEqual(s.item(2), u'border')
67 self.assertEqual(s.item(10), u'')
68
70 "CSSStyleDeclaration parse"
71
72 tests = {
73
74 u'TOP:0': u'top: 0',
75 u'top:0': u'top: 0',
76
77 u'c\\olor: red; color:green': u'color: green',
78 u'color:g\\reen': u'color: g\\reen',
79
80 u'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:': u'color: green',
84 u'color:red; color:; color:green': u'color: green',
85 u'color:green; color{;color:maroon}': u'color: green',
86 u'color:red; color{;color:maroon}; color:green': u'color: green',
87
88 ur'''color: red;
89 voice-family: "\"}\"";
90 voice-family:inherit;
91 color: green;''': 'voice-family: inherit;\ncolor: green',
92 ur'''col\or: blue;
93 font-family: 'Courier New Times
94 color: red;
95 color: green;''': u'color: green',
96
97
98 ur'$top: 0': None,
99 ur'$: 0': u''
100 }
101 cssutils.ser.prefs.keepAllProperties = False
102 for test, exp in tests.items():
103 sh = cssutils.parseString('a { %s }' % test)
104 if exp is None:
105 exp = u'%s' % test
106 elif exp != u'':
107 exp = u'%s' % exp
108 self.assertEqual(exp, sh.cssRules[0].style.cssText)
109
110 cssutils.ser.prefs.useDefaults()
111
127
128 - def test_cssText(self):
129 "CSSStyleDeclaration.cssText"
130
131 s = cssutils.css.CSSStyleDeclaration()
132 tests = {
133 u'': u'',
134 u' ': u'',
135 u' \t \n ': u'',
136 u'/*x*/': u'/*x*/'
137 }
138 for test, exp in tests.items():
139 s.cssText = 'left: 0;'
140 s.cssText = test
141 self.assertEqual(exp, s.cssText)
142
143
144 s = cssutils.css.CSSStyleDeclaration()
145 tests = {
146 u'left: 0': u'left: 0',
147 u'left:0': u'left: 0',
148 u' left : 0 ': u'left: 0',
149 u'left: 0;': u'left: 0',
150 u'left: 0 !important ': u'left: 0 !important',
151 u'left:0!important': u'left: 0 !important',
152 u'left: 0; top: 1': u'left: 0;\ntop: 1',
153 u'/*1*/left: 0;/*2*/ top: 1/*3*/':
154 u'/*1*/\nleft: 0;\n/*2*/\ntop: 1/*3*/',
155 u'left:0; top:1;': u'left: 0;\ntop: 1',
156 u'/*1*/left: 0;/*2*/ top: 1;/*3*/':
157 u'/*1*/\nleft: 0;\n/*2*/\ntop: 1;\n/*3*/',
158 }
159 for test, exp in tests.items():
160 s.cssText = test
161 self.assertEqual(exp, s.cssText)
162
163
164 tests = {
165 u'top': xml.dom.SyntaxErr,
166 u'top:': xml.dom.SyntaxErr,
167 u'top : ': xml.dom.SyntaxErr,
168 u'top:!important': xml.dom.SyntaxErr,
169 u'top:!important;': xml.dom.SyntaxErr,
170 u'top:;': xml.dom.SyntaxErr,
171 u'top 0': xml.dom.SyntaxErr,
172 u'top 0;': xml.dom.SyntaxErr,
173
174 u':': xml.dom.SyntaxErr,
175 u':0': xml.dom.SyntaxErr,
176 u':0;': xml.dom.SyntaxErr,
177 u':0!important': xml.dom.SyntaxErr,
178 u':;': xml.dom.SyntaxErr,
179 u': ;': xml.dom.SyntaxErr,
180 u':!important;': xml.dom.SyntaxErr,
181 u': !important;': xml.dom.SyntaxErr,
182
183 u'0': xml.dom.SyntaxErr,
184 u'0!important': xml.dom.SyntaxErr,
185 u'0!important;': xml.dom.SyntaxErr,
186 u'0;': xml.dom.SyntaxErr,
187
188 u'!important': xml.dom.SyntaxErr,
189 u'!important;': xml.dom.SyntaxErr,
190
191 u';': xml.dom.SyntaxErr,
192 }
193 self.do_raise_r(tests)
194
195 - def test_getCssText(self):
196 "CSSStyleDeclaration.getCssText(separator)"
197 s = cssutils.css.CSSStyleDeclaration(cssText=u'a:1;b:2')
198 self.assertEqual(u'a: 1;\nb: 2', s.getCssText())
199 self.assertEqual(u'a: 1;b: 2', s.getCssText(separator=u''))
200 self.assertEqual(u'a: 1;/*x*/b: 2', s.getCssText(separator=u'/*x*/'))
201
213
227
229 "CSSStyleDeclaration.getProperties()"
230 s = cssutils.css.CSSStyleDeclaration(cssText=
231 u'/*1*/y:0;x:a !important;y:1; \\x:b;')
232 tests = {
233
234 (None, False): [(u'y', u'1', u''),
235 (u'x', u'a', u'important')],
236 (None, True): [(u'y', u'0', u''),
237 (u'x', u'a', u'important'),
238 (u'y', u'1', u''),
239 (u'\\x', u'b', u'')
240 ],
241 ('x', False): [(u'x', u'a', u'important')],
242 ('\\x', False): [(u'x', u'a', u'important')],
243 ('x', True): [(u'x', u'a', u'important'),
244 (u'\\x', u'b', u'')],
245 ('\\x', True): [(u'x', u'a', u'important'),
246 (u'\\x', u'b', u'')],
247 }
248 for test in tests:
249 name, all = test
250 expected = tests[test]
251 actual = s.getProperties(name, all)
252 self.assertEqual(len(expected), len(actual))
253 for i, ex in enumerate(expected):
254 a = actual[i]
255 self.assertEqual(ex, (a.literalname, a.value, a.priority))
256
257
258 s = cssutils.css.CSSStyleDeclaration(cssText=
259 u'a:0;b:1;a:1')
260 self.assertEqual(u'ba', u''.join([p.name for p in s]))
261
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
288 "CSSStyleDeclaration.getPropertyValue()"
289 s = cssutils.css.CSSStyleDeclaration()
290 self.assertEqual(u'', s.getPropertyValue('unset'))
291
292 s.setProperty(u'left', '0')
293 self.assertEqual(u'0', s.getPropertyValue('left'))
294
295 s.setProperty(u'border', '1px solid green')
296 self.assertEqual(u'1px solid green', s.getPropertyValue('border'))
297
298 s = cssutils.css.CSSStyleDeclaration(cssText='color: red;c\\olor: green')
299 self.assertEqual(u'green', s.getPropertyValue('color'))
300 self.assertEqual(u'green', s.getPropertyValue('c\\olor'))
301 self.assertEqual(u'red', s.getPropertyValue('color', False))
302 self.assertEqual(u'green', s.getPropertyValue('c\\olor', False))
303
304 tests = {
305 ur'color: red; color: green': 'green',
306 ur'c\olor: red; c\olor: green': 'green',
307 ur'color: red; c\olor: green': 'green',
308 ur'color: red !important; color: green !important': 'green',
309 ur'color: green !important; color: red': 'green',
310 }
311 for test in tests:
312 s = cssutils.css.CSSStyleDeclaration(cssText=test)
313 self.assertEqual(tests[test], s.getPropertyValue('color'))
314
316 "CSSStyleDeclaration.getPropertyPriority()"
317 s = cssutils.css.CSSStyleDeclaration()
318 self.assertEqual(u'', s.getPropertyPriority('unset'))
319
320 s.setProperty(u'left', u'0', u'!important')
321 self.assertEqual(u'important', s.getPropertyPriority('left'))
322
323 s = cssutils.css.CSSStyleDeclaration(cssText=
324 'x: 1 !important;\\x: 2;x: 3 !important;\\x: 4')
325 self.assertEqual(u'important', s.getPropertyPriority('x'))
326 self.assertEqual(u'important', s.getPropertyPriority('\\x'))
327 self.assertEqual(u'important', s.getPropertyPriority('x', True))
328 self.assertEqual(u'', s.getPropertyPriority('\\x', False))
329
331 "CSSStyleDeclaration.removeProperty()"
332 s = cssutils.css.CSSStyleDeclaration()
333 css = ur'\x:0 !important; x:1; \x:2; x:3'
334
335
336 s.cssText = css
337 self.assertEqual(u'0', s.removeProperty('x'))
338 self.assertEqual(u'', s.cssText)
339
340
341 s.cssText = css
342 self.assertEqual(u'3', s.removeProperty('x', normalize=False))
343 self.assertEqual(ur'\x: 0 !important;\x: 2', s.getCssText(separator=u''))
344 self.assertEqual(u'0', s.removeProperty(r'\x', normalize=False))
345 self.assertEqual(u'', s.cssText)
346
347 s.cssText = css
348 self.assertEqual(u'0', s.removeProperty(r'\x', normalize=False))
349 self.assertEqual(ur'x: 1;x: 3', s.getCssText(separator=u''))
350 self.assertEqual(u'3', s.removeProperty('x', normalize=False))
351 self.assertEqual(u'', s.cssText)
352
354 "CSSStyleDeclaration.setProperty()"
355 s = cssutils.css.CSSStyleDeclaration()
356 s.setProperty('top', '0', '!important')
357 self.assertEqual('0', s.getPropertyValue('top'))
358 self.assertEqual('important', s.getPropertyPriority('top'))
359 s.setProperty('top', '1px')
360 self.assertEqual('1px', s.getPropertyValue('top'))
361 self.assertEqual('', s.getPropertyPriority('top'))
362
363 s.setProperty('top', '2px')
364 self.assertEqual('2px', s.getPropertyValue('top'))
365
366 s.setProperty('\\top', '3px')
367 self.assertEqual('3px', s.getPropertyValue('top'))
368
369 s.setProperty('\\top', '4px', normalize=False)
370 self.assertEqual('4px', s.getPropertyValue('top'))
371 self.assertEqual('4px', s.getPropertyValue('\\top', False))
372 self.assertEqual('3px', s.getPropertyValue('top', False))
373
374
375 s.setProperty('TOP', '0', '!IMPORTANT')
376 self.assertEqual('0', s.getPropertyValue('top'))
377 self.assertEqual('important', s.getPropertyPriority('top'))
378
379 tests = {
380 (u'left', u'0px', u''): u'left: 0px',
381 (u'left', u'0px', u'important'): u'left: 0px !important',
382 (u'LEFT', u'0px', u'important'): u'left: 0px !important',
383 (u'left', u'0px', u'important'): u'left: 0px !important',
384 }
385 for test, exp in tests.items():
386 s = cssutils.css.CSSStyleDeclaration()
387 n, v, p = test
388 s.setProperty(n, v, p)
389 self.assertEqual(exp, s.cssText)
390 self.assertEqual(v, s.getPropertyValue(n))
391 self.assertEqual(p, s.getPropertyPriority(n))
392
394 "CSSStyleDeclaration.length"
395 s = cssutils.css.CSSStyleDeclaration()
396
397
398 s.cssText = u'left: 0'
399 self.assertEqual(1, s.length)
400 self.assertEqual(1, len(s.seq))
401 s.cssText = u'/*1*/left/*x*/:/*x*/0/*x*/;/*2*/ top: 1;/*3*/'
402 self.assertEqual(2, s.length)
403 self.assertEqual(5, len(s.seq))
404
405
406 s = cssutils.css.CSSStyleDeclaration()
407 s.setProperty('top', '0', '!important')
408 self.assertEqual(1, s.length)
409 s.setProperty('top', '1px')
410 self.assertEqual(1, s.length)
411 s.setProperty('left', '1px')
412
414 "CSSStyleDeclaration.XXX(name)"
415 s = cssutils.css.CSSStyleDeclaration()
416 s.setProperty('top', '1px', '!important')
417
418 self.assertEqual('1px', s.getPropertyValue('top'))
419 self.assertEqual('1px', s.getPropertyValue('TOP'))
420 self.assertEqual('1px', s.getPropertyValue('T\op'))
421
422 self.assertEqual('important', s.getPropertyPriority('top'))
423 self.assertEqual('important', s.getPropertyPriority('TOP'))
424 self.assertEqual('important', s.getPropertyPriority('T\op'))
425
426 s.setProperty('top', '2px', '!important')
427 self.assertEqual('2px', s.removeProperty('top'))
428 s.setProperty('top', '2px', '!important')
429 self.assertEqual('2px', s.removeProperty('TOP'))
430 s.setProperty('top', '2px', '!important')
431 self.assertEqual('2px', s.removeProperty('T\op'))
432
434 "CSSStyleDeclaration.$css2property get set del"
435 s = cssutils.css.CSSStyleDeclaration(
436 cssText='left: 1px;color: red; font-style: italic')
437
438 s.color = 'green'
439 s.fontStyle = 'normal'
440 self.assertEqual('green', s.color)
441 self.assertEqual('normal', s.fontStyle)
442 self.assertEqual('green', s.getPropertyValue('color'))
443 self.assertEqual('normal', s.getPropertyValue('font-style'))
444 self.assertEqual(
445 u'''left: 1px;\ncolor: green;\nfont-style: normal''',
446 s.cssText)
447
448 del s.color
449 self.assertEqual(
450 u'''left: 1px;\nfont-style: normal''',
451 s.cssText)
452 del s.fontStyle
453 self.assertEqual(u'left: 1px', s.cssText)
454
455 self.assertRaises(AttributeError, s.__setattr__, 'UNKNOWN', 'red')
456
457 s.setProperty('UNKNOWN', 'red')
458
459 self.assertRaises(AttributeError, s.__getattribute__, 'UNKNOWN')
460 self.assertRaises(AttributeError, s.__delattr__, 'UNKNOWN')
461
462 self.assertEqual('red', s.getPropertyValue('UNKNOWN'))
463 self.assertEqual(
464 '''left: 1px;\nunknown: red''', s.cssText)
465
467 "CSSStyleDeclaration.__repr__(), .__str__()"
468 s = cssutils.css.CSSStyleDeclaration(cssText='a:1;b:2')
469
470 self.assert_("2" in str(s))
471
472 s2 = eval(repr(s))
473 self.assert_(isinstance(s2, s.__class__))
474
475
476 if __name__ == '__main__':
477 import unittest
478 unittest.main()
479