1 """Testcases for cssutils.css.cssstyledelaration.CSSStyleDeclaration."""
2 __author__ = '$LastChangedBy: cthedot $'
3 __date__ = '$LastChangedDate: 2007-08-26 15:35:29 +0200 (So, 26 Aug 2007) $'
4 __version__ = '$LastChangedRevision: 282 $'
5
6 import xml.dom
7 import basetest
8 import cssutils
9
10
12
15
30
32 "CSSStyleDeclaration parseString()"
33
34
35 tests = {
36
37 u'TOP:0': u'top: 0',
38 u'top:0': u'top: 0',
39
40 u'c\\olor: red; color:green': u'color: green',
41 u'color:g\\reen': u'color: g\\reen',
42
43 u'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:': u'color: green',
47 u'color:red; color:; color:green': u'color: green',
48 u'color:green; color{;color:maroon}': u'color: green',
49 u'color:red; color{;color:maroon}; color:green':
50 u'color: green',
51
52 ur'''color: red;
53 voice-family: "\"}\"";
54 voice-family:inherit;
55 color: green;''': 'color: green;\nvoice-family: inherit'
56 }
57 for test, exp in tests.items():
58 sh = cssutils.parseString('a { %s }' % test)
59 if exp is None:
60 exp = u'%s' % test
61 elif exp != u'':
62 exp = u'%s' % exp
63 self.assertEqual(exp, sh.cssRules[0].style.cssText)
64
65 - def test_cssText(self):
66 "CSSStyleDeclaration.cssText"
67
68 s = cssutils.css.CSSStyleDeclaration()
69 tests = {
70 u'': u'',
71 u' ': u'',
72 u' \t \n ': u'',
73 u'/*x*/': u'/*x*/'
74 }
75 for test, exp in tests.items():
76 s.cssText = 'left: 0;'
77 s.cssText = test
78 self.assertEqual(exp, s.cssText)
79
80
81 s = cssutils.css.CSSStyleDeclaration()
82 tests = {
83 u'left: 0': u'left: 0',
84 u'left:0': u'left: 0',
85 u' left : 0 ': u'left: 0',
86 u'left: 0;': u'left: 0',
87 u'left: 0 !important ': u'left: 0 !important',
88 u'left:0!important': u'left: 0 !important',
89 u'left: 0; top: 1': u'left: 0;\ntop: 1',
90 }
91 for test, exp in tests.items():
92 s.cssText = test
93 self.assertEqual(exp, s.cssText)
94
95
96 tests = {
97 u'top': xml.dom.SyntaxErr,
98 u'top:': xml.dom.SyntaxErr,
99 u'top : ': xml.dom.SyntaxErr,
100 u'top:!important': xml.dom.SyntaxErr,
101 u'top:!important;': xml.dom.SyntaxErr,
102 u'top:;': xml.dom.SyntaxErr,
103 u'top 0': xml.dom.SyntaxErr,
104 u'top 0;': xml.dom.SyntaxErr,
105
106 u':': xml.dom.SyntaxErr,
107 u':0': xml.dom.SyntaxErr,
108 u':0;': xml.dom.SyntaxErr,
109 u':0!important': xml.dom.SyntaxErr,
110 u':;': xml.dom.SyntaxErr,
111 u':0;': xml.dom.SyntaxErr,
112 u':0!important;': xml.dom.SyntaxErr,
113
114 u'0': xml.dom.SyntaxErr,
115 u'0!important': xml.dom.SyntaxErr,
116 u'0!important;': xml.dom.SyntaxErr,
117
118 u'!important': xml.dom.SyntaxErr,
119 u'!important;': xml.dom.SyntaxErr,
120
121 u';': xml.dom.SyntaxErr,
122 }
123 self.do_raise_r(tests)
124
125 - def test_cssText(self):
126 "CSSStyleDeclaration.getCssText(separator)"
127 s = cssutils.css.CSSStyleDeclaration(cssText=u'a:1;b:2')
128 self.assertEqual(u'a: 1;\nb: 2', s.getCssText())
129 self.assertEqual(u'a: 1;b: 2', s.getCssText(separator=u''))
130 self.assertEqual(u'a: 1;/*x*/b: 2', s.getCssText(separator=u'/*x*/'))
131
133 "CSSStyleDeclaration.length"
134 s = cssutils.css.CSSStyleDeclaration()
135
136
137 s.cssText = u'left: 0'
138 self.assertEqual(1, s.length)
139 self.assertEqual(1, len(s.seq))
140 s.cssText = u'/*1*/left/*x*/:/*x*/0/*x*/;/*2*/ top: 1;/*3*/'
141 self.assertEqual(2, s.length)
142 self.assertEqual(5, len(s.seq))
143
144
145 s = cssutils.css.CSSStyleDeclaration()
146 s.setProperty('top', '0', '!important')
147 self.assertEqual(1, s.length)
148 s.setProperty('top', '1')
149 self.assertEqual(1, s.length)
150 s.setProperty('left', '1')
151
163
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
196
207
209 "CSSStyleDeclaration.getSameNamePropertyList()"
210 s = cssutils.css.CSSStyleDeclaration(cssText='color: red')
211 pl = s.getSameNamePropertyList('color')
212 self.assertEqual('color', pl.name)
213 self.assertEqual(1, len(pl))
214 self.assertEqual(0, pl._currentIndex())
215
216 s.setProperty('C\olor', 'green', '!important', overwrite=False)
217 self.assertEqual('color', pl.name)
218 self.assertEqual(2, len(pl))
219 self.assertEqual(1, pl._currentIndex())
220
221
222
223
224 s.setProperty('COLOR', 'blue', overwrite=False)
225 self.assertEqual('color', pl.name)
226 self.assertEqual(3, len(pl))
227 self.assertEqual(1, pl._currentIndex())
228
229
230
231
233 "CSSStyleDeclaration.item()"
234 _props = ('left', 'top', 'right')
235 s = cssutils.css.CSSStyleDeclaration(cssText=
236 '\left:0;TOP:1;right:3')
237 for i in range(0,3):
238 self.assertEqual(_props[i], s.item(i))
239 self.assertEqual(_props[-1-i], s.item(-1-i))
240 self.assertEqual(u'', s.item(3))
241 self.assertEqual(u'', s.item(-4))
242
250
252 "CSSStyleDeclaration.setProperty(overwrite=True)"
253 s = cssutils.css.CSSStyleDeclaration()
254 s.cssText = 'color: red; top: 1px'
255 self.assertEqual(2, s.length)
256 pl = s.getSameNamePropertyList('color')
257 self.assertEqual(1, len(pl))
258
259
260 s.setProperty('color', 'green', '!important')
261 self.assertEqual(2, s.length)
262 self.assertEqual('color', s.item(0))
263 self.assertEqual('top', s.item(1))
264 self.assertEqual('green', s.getPropertyValue('color'))
265 pl = s.getSameNamePropertyList('color')
266 self.assertEqual(1, len(pl))
267
268
269 s.setProperty('color', 'blue', overwrite=False)
270 self.assertEqual(2, s.length)
271 self.assertEqual('color', s.item(0))
272 self.assertEqual('top', s.item(1))
273
274 self.assertEqual('green', s.getPropertyValue('color'))
275 pl = s.getSameNamePropertyList('color')
276 self.assertEqual(2, len(pl))
277
278
279 s.setProperty('color', 'red', overwrite=True)
280 self.assertEqual(2, s.length)
281 self.assertEqual('color', s.item(0))
282 self.assertEqual('top', s.item(1))
283 self.assertEqual('red', s.getPropertyValue('color'))
284 pl = s.getSameNamePropertyList('color')
285 self.assertEqual(1, len(pl))
286
288 "CSSStyleDeclaration.setProperty()"
289 s = cssutils.css.CSSStyleDeclaration()
290 s.setProperty('top', '0', '!important')
291 self.assertEqual('0', s.getPropertyValue('top'))
292 self.assertEqual('!important', s.getPropertyPriority('top'))
293 s.setProperty('top', '1')
294 self.assertEqual('1', s.getPropertyValue('top'))
295 self.assertEqual('', s.getPropertyPriority('top'))
296
297
298 s.setProperty('TOP', '0', '!IMPORTANT')
299 self.assertEqual('0', s.getPropertyValue('top'))
300 self.assertEqual('!important', s.getPropertyPriority('top'))
301 self.assertEqual('0', s.getPropertyValue('top'))
302 self.assertEqual('!important', s.getPropertyPriority('top'))
303
304 tests = {
305 (u'left', u'0px', u''): u'left: 0px',
306 (u'left', u'0px', u'!important'): u'left: 0px !important',
307 (u'LEFT', u'0px', u'!important'): u'left: 0px !important',
308 (u'left', u'0px', u'!important'): u'left: 0px !important',
309 }
310 for test, exp in tests.items():
311 s = cssutils.css.CSSStyleDeclaration()
312 n, v, p = test
313 s.setProperty(n, v, p)
314 self.assertEqual(exp, s.cssText)
315 self.assertEqual(v, s.getPropertyValue(n))
316 self.assertEqual(p, s.getPropertyPriority(n))
317
319 "CSSStyleDeclaration.XXX(name)"
320 s = cssutils.css.CSSStyleDeclaration()
321 s.setProperty('top', '1px', '!important')
322
323 self.assertEqual('1px', s.getPropertyValue('top'))
324 self.assertEqual('1px', s.getPropertyValue('TOP'))
325 self.assertEqual('1px', s.getPropertyValue('T\op'))
326
327
328
329
330
331 self.assertEqual('!important', s.getPropertyPriority('top'))
332 self.assertEqual('!important', s.getPropertyPriority('TOP'))
333 self.assertEqual('!important', s.getPropertyPriority('T\op'))
334
335 s.setProperty('top', '2px', '!important')
336 self.assertEqual('2px', s.removeProperty('top'))
337 s.setProperty('top', '2px', '!important')
338 self.assertEqual('2px', s.removeProperty('TOP'))
339 s.setProperty('top', '2px', '!important')
340 self.assertEqual('2px', s.removeProperty('T\op'))
341
343 "CSSStyleDeclaration.$css2property get set del"
344 s = cssutils.css.CSSStyleDeclaration(
345 cssText='left: 1px;color: red; font-style: italic')
346
347 s.color = 'green'
348 s.fontStyle = 'normal'
349 self.assertEqual('green', s.color)
350 self.assertEqual('normal', s.fontStyle)
351 self.assertEqual('green', s.getPropertyValue('color'))
352 self.assertEqual('normal', s.getPropertyValue('font-style'))
353 self.assertEqual(
354 u'''left: 1px;\ncolor: green;\nfont-style: normal''',
355 s.cssText)
356
357 del s.color
358 self.assertEqual(
359 u'''left: 1px;\nfont-style: normal''',
360 s.cssText)
361 del s.fontStyle
362 self.assertEqual(u'left: 1px', s.cssText)
363
364 self.assertRaises(AttributeError, s.__setattr__, 'UNKNOWN', 'red')
365
366 s.setProperty('UNKNOWN', 'red')
367
368 self.assertRaises(AttributeError, s.__getattribute__, 'UNKNOWN')
369 self.assertRaises(AttributeError, s.__delattr__, 'UNKNOWN')
370
371 self.assertEqual('red', s.getPropertyValue('UNKNOWN'))
372 self.assertEqual(
373 '''left: 1px;\nunknown: red''', s.cssText)
374
376 "CSSStyleDeclaration.__repr__(), .__str__()"
377 s = cssutils.css.CSSStyleDeclaration(cssText='a:1;b:2')
378
379 self.assert_("2" in str(s))
380
381 s2 = eval(repr(s))
382 self.assert_(isinstance(s2, s.__class__))
383
384
398
399
400 if __name__ == '__main__':
401 import unittest
402 unittest.main()
403