1 """Testcases for cssutils.css.cssstyledelaration.CSSStyleDeclaration."""
2 __author__ = '$LastChangedBy: cthedot $'
3 __date__ = '$LastChangedDate: 2007-10-27 21:33:00 +0200 (Sa, 27 Okt 2007) $'
4 __version__ = '$LastChangedRevision: 579 $'
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 cssutils.ser.prefs.keepAllProperties = False
61 for test, exp in tests.items():
62 sh = cssutils.parseString('a { %s }' % test)
63 if exp is None:
64 exp = u'%s' % test
65 elif exp != u'':
66 exp = u'%s' % exp
67 self.assertEqual(exp, sh.cssRules[0].style.cssText)
68
69 cssutils.ser.prefs.useDefaults()
70
71 - def test_cssText(self):
72 "CSSStyleDeclaration.cssText"
73
74 s = cssutils.css.CSSStyleDeclaration()
75 tests = {
76 u'': u'',
77 u' ': u'',
78 u' \t \n ': u'',
79 u'/*x*/': u'/*x*/'
80 }
81 for test, exp in tests.items():
82 s.cssText = 'left: 0;'
83 s.cssText = test
84 self.assertEqual(exp, s.cssText)
85
86
87 s = cssutils.css.CSSStyleDeclaration()
88 tests = {
89 u'left: 0': u'left: 0',
90 u'left:0': u'left: 0',
91 u' left : 0 ': u'left: 0',
92 u'left: 0;': u'left: 0',
93 u'left: 0 !important ': u'left: 0 !important',
94 u'left:0!important': u'left: 0 !important',
95 u'left: 0; top: 1': u'left: 0;\ntop: 1',
96 u'/*1*/left: 0;/*2*/ top: 1/*3*/':
97 u'/*1*/\nleft: 0;\n/*2*/\ntop: 1/*3*/',
98 u'left:0; top:1;': u'left: 0;\ntop: 1',
99 u'/*1*/left: 0;/*2*/ top: 1;/*3*/':
100 u'/*1*/\nleft: 0;\n/*2*/\ntop: 1;\n/*3*/',
101 }
102 for test, exp in tests.items():
103 s.cssText = test
104 self.assertEqual(exp, s.cssText)
105
106
107 tests = {
108 u'top': xml.dom.SyntaxErr,
109 u'top:': xml.dom.SyntaxErr,
110 u'top : ': xml.dom.SyntaxErr,
111 u'top:!important': xml.dom.SyntaxErr,
112 u'top:!important;': xml.dom.SyntaxErr,
113 u'top:;': xml.dom.SyntaxErr,
114 u'top 0': xml.dom.SyntaxErr,
115 u'top 0;': xml.dom.SyntaxErr,
116
117 u':': xml.dom.SyntaxErr,
118 u':0': xml.dom.SyntaxErr,
119 u':0;': xml.dom.SyntaxErr,
120 u':0!important': xml.dom.SyntaxErr,
121 u':;': xml.dom.SyntaxErr,
122 u': ;': xml.dom.SyntaxErr,
123 u':!important;': xml.dom.SyntaxErr,
124 u': !important;': xml.dom.SyntaxErr,
125
126 u'0': xml.dom.SyntaxErr,
127 u'0!important': xml.dom.SyntaxErr,
128 u'0!important;': xml.dom.SyntaxErr,
129 u'0;': xml.dom.SyntaxErr,
130
131 u'!important': xml.dom.SyntaxErr,
132 u'!important;': xml.dom.SyntaxErr,
133
134 u';': xml.dom.SyntaxErr,
135 }
136 self.do_raise_r(tests)
137
138 - def test_getCssText(self):
139 "CSSStyleDeclaration.getCssText(separator)"
140 s = cssutils.css.CSSStyleDeclaration(cssText=u'a:1;b:2')
141 self.assertEqual(u'a: 1;\nb: 2', s.getCssText())
142 self.assertEqual(u'a: 1;b: 2', s.getCssText(separator=u''))
143 self.assertEqual(u'a: 1;/*x*/b: 2', s.getCssText(separator=u'/*x*/'))
144
156
158 "CSSStyleDeclaration.getProperties()"
159 s = cssutils.css.CSSStyleDeclaration(cssText=u'x:a; \\x:b; y:1')
160
161 tests = {
162
163 (None, False): [(u'\\x', u'b', u''),
164 (u'y', u'1', u'')],
165 (None, True): [(u'x', u'a', u''),
166 (u'\\x', u'b', u''),
167 (u'y', u'1', u'')],
168 ('x', False): [(u'\\x', u'b', u'')],
169 ('x', True): [(u'x', u'a', u''),
170 (u'\\x', u'b', u'')],
171 ('\\x', False): [(u'\\x', u'b', u'')],
172 ('\\x', True): [(u'x', u'a', u''),
173 (u'\\x', u'b', u'')],
174 }
175 for test in tests:
176 name, all = test
177 expected = tests[test]
178 actual = s.getProperties(name, all)
179 self.assertEqual(len(expected), len(actual))
180 for i, ex in enumerate(expected):
181 a = actual[i]
182 self.assertEqual(ex, (a.name, a.value, a.priority))
183
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
210 "CSSStyleDeclaration.getPropertyValue()"
211 s = cssutils.css.CSSStyleDeclaration()
212 self.assertEqual(u'', s.getPropertyValue('unset'))
213
214 s.setProperty(u'left', '0')
215 self.assertEqual(u'0', s.getPropertyValue('left'))
216
217 s.setProperty(u'border', '1px solid green')
218 self.assertEqual(u'1px solid green', s.getPropertyValue('border'))
219
220 s = cssutils.css.CSSStyleDeclaration(cssText='color: red;c\\olor: green')
221 self.assertEqual(u'green', s.getPropertyValue('color'))
222 self.assertEqual(u'green', s.getPropertyValue('c\\olor'))
223 self.assertEqual(u'red', s.getPropertyValue('color', False))
224 self.assertEqual(u'green', s.getPropertyValue('c\\olor', False))
225
227 "CSSStyleDeclaration.getPropertyPriority()"
228 s = cssutils.css.CSSStyleDeclaration()
229 self.assertEqual(u'', s.getPropertyPriority('unset'))
230
231 s.setProperty(u'left', u'0', u'!important')
232 self.assertEqual(u'!important', s.getPropertyPriority('left'))
233
234 s = cssutils.css.CSSStyleDeclaration(cssText=
235 'x: 1 !important;\\x: 2;x: 3 !important;\\x: 4')
236 self.assertEqual(u'', s.getPropertyPriority('x'))
237 self.assertEqual(u'', s.getPropertyPriority('\\x'))
238 self.assertEqual(u'!important', s.getPropertyPriority('x', False))
239 self.assertEqual(u'', s.getPropertyPriority('\\x', False))
240
242 "CSSStyleDeclaration.removeProperty()"
243 s = cssutils.css.CSSStyleDeclaration(cssText='top: 0 !important')
244 self.assertEqual('0', s.removeProperty('top'))
245 self.assertEqual(0, s.length)
246 self.assertEqual('', s.removeProperty('top'))
247 self.assertEqual(0, s.length)
248
249
250 s.cssText = 'x: 1 !important;\\x: 2;x: 3 !important;\\x: 4'
251 self.assertEqual(4, len(s.getProperties(all=True)))
252
253 self.assertEqual('', s.removeProperty('y', False))
254 self.assertEqual('', s.removeProperty('y', True))
255
256 self.assertEqual('', s.removeProperty('X', False))
257
258 self.assertEqual('4', s.removeProperty('X', True))
259
260 self.assertEqual('2', s.removeProperty('\\x', False))
261 self.assertEqual('3', s.removeProperty('x', False))
262 self.assertEqual(1, len(s.getProperties(all=True)))
263
264
265 s.cssText = 'x:1;\\x:2;x:3;\\x:4'
266 self.assertEqual('3', s.removeProperty('x', normalize=False))
267 self.assertEqual(3, len(s.getProperties(all=True)))
268
269
270
271
273 "CSSStyleDeclaration.setProperty()"
274 s = cssutils.css.CSSStyleDeclaration()
275 s.setProperty('top', '0', '!important')
276 self.assertEqual('0', s.getPropertyValue('top'))
277 self.assertEqual('!important', s.getPropertyPriority('top'))
278 s.setProperty('top', '1px')
279 self.assertEqual('1px', s.getPropertyValue('top'))
280 self.assertEqual('', s.getPropertyPriority('top'))
281
282 s.setProperty('top', '2px')
283 self.assertEqual('2px', s.getPropertyValue('top'))
284
285 s.setProperty('\\top', '3px')
286 self.assertEqual('3px', s.getPropertyValue('top'))
287
288 s.setProperty('\\top', '4px', normalize=False)
289 self.assertEqual('4px', s.getPropertyValue('top'))
290 self.assertEqual('4px', s.getPropertyValue('\\top', False))
291 self.assertEqual('3px', s.getPropertyValue('top', False))
292
293
294
295 s.setProperty('TOP', '0', '!IMPORTANT')
296 self.assertEqual('0', s.getPropertyValue('top'))
297 self.assertEqual('!IMPORTANT', s.getPropertyPriority('top'))
298 self.assertEqual('0', s.getPropertyValue('top'))
299 self.assertEqual('!IMPORTANT', s.getPropertyPriority('top'))
300
301 tests = {
302 (u'left', u'0px', u''): u'left: 0px',
303 (u'left', u'0px', u'!important'): u'left: 0px !important',
304 (u'LEFT', u'0px', u'!important'): u'left: 0px !important',
305 (u'left', u'0px', u'!important'): u'left: 0px !important',
306 }
307 for test, exp in tests.items():
308 s = cssutils.css.CSSStyleDeclaration()
309 n, v, p = test
310 s.setProperty(n, v, p)
311 self.assertEqual(exp, s.cssText)
312 self.assertEqual(v, s.getPropertyValue(n))
313 self.assertEqual(p, s.getPropertyPriority(n))
314
316 "CSSStyleDeclaration.item()"
317 _props = ('left', 'top', 'right')
318 s = cssutils.css.CSSStyleDeclaration(cssText=
319 '\left:0;TOP:1;right:3')
320 for i in range(0,3):
321 self.assertEqual(_props[i], s.item(i))
322 self.assertEqual(_props[-1-i], s.item(-1-i))
323 self.assertEqual(u'', s.item(3))
324 self.assertEqual(u'', s.item(-4))
325
327 "CSSStyleDeclaration.length"
328 s = cssutils.css.CSSStyleDeclaration()
329
330
331 s.cssText = u'left: 0'
332 self.assertEqual(1, s.length)
333 self.assertEqual(1, len(s.seq))
334 s.cssText = u'/*1*/left/*x*/:/*x*/0/*x*/;/*2*/ top: 1;/*3*/'
335 self.assertEqual(2, s.length)
336 self.assertEqual(5, len(s.seq))
337
338
339 s = cssutils.css.CSSStyleDeclaration()
340 s.setProperty('top', '0', '!important')
341 self.assertEqual(1, s.length)
342 s.setProperty('top', '1px')
343 self.assertEqual(1, s.length)
344 s.setProperty('left', '1px')
345
347 "CSSStyleDeclaration.XXX(name)"
348 s = cssutils.css.CSSStyleDeclaration()
349 s.setProperty('top', '1px', '!important')
350
351 self.assertEqual('1px', s.getPropertyValue('top'))
352 self.assertEqual('1px', s.getPropertyValue('TOP'))
353 self.assertEqual('1px', s.getPropertyValue('T\op'))
354
355 self.assertEqual('!important', s.getPropertyPriority('top'))
356 self.assertEqual('!important', s.getPropertyPriority('TOP'))
357 self.assertEqual('!important', s.getPropertyPriority('T\op'))
358
359 s.setProperty('top', '2px', '!important')
360 self.assertEqual('2px', s.removeProperty('top'))
361 s.setProperty('top', '2px', '!important')
362 self.assertEqual('2px', s.removeProperty('TOP'))
363 s.setProperty('top', '2px', '!important')
364 self.assertEqual('2px', s.removeProperty('T\op'))
365
367 "CSSStyleDeclaration.$css2property get set del"
368 s = cssutils.css.CSSStyleDeclaration(
369 cssText='left: 1px;color: red; font-style: italic')
370
371 s.color = 'green'
372 s.fontStyle = 'normal'
373 self.assertEqual('green', s.color)
374 self.assertEqual('normal', s.fontStyle)
375 self.assertEqual('green', s.getPropertyValue('color'))
376 self.assertEqual('normal', s.getPropertyValue('font-style'))
377 self.assertEqual(
378 u'''left: 1px;\ncolor: green;\nfont-style: normal''',
379 s.cssText)
380
381 del s.color
382 self.assertEqual(
383 u'''left: 1px;\nfont-style: normal''',
384 s.cssText)
385 del s.fontStyle
386 self.assertEqual(u'left: 1px', s.cssText)
387
388 self.assertRaises(AttributeError, s.__setattr__, 'UNKNOWN', 'red')
389
390 s.setProperty('UNKNOWN', 'red')
391
392 self.assertRaises(AttributeError, s.__getattribute__, 'UNKNOWN')
393 self.assertRaises(AttributeError, s.__delattr__, 'UNKNOWN')
394
395 self.assertEqual('red', s.getPropertyValue('UNKNOWN'))
396 self.assertEqual(
397 '''left: 1px;\nunknown: red''', s.cssText)
398
400 "CSSStyleDeclaration.__repr__(), .__str__()"
401 s = cssutils.css.CSSStyleDeclaration(cssText='a:1;b:2')
402
403 self.assert_("2" in str(s))
404
405 s2 = eval(repr(s))
406 self.assert_(isinstance(s2, s.__class__))
407
408
409 if __name__ == '__main__':
410 import unittest
411 unittest.main()
412