1 """testcases for cssutils.css.CSSRule
2 """
3 __author__ = '$LastChangedBy: cthedot $'
4 __date__ = '$LastChangedDate: 2008-02-03 15:10:34 +0100 (So, 03 Feb 2008) $'
5 __version__ = '$LastChangedRevision: 980 $'
6
7 import xml.dom
8 import basetest
9 import cssutils.css
10
12 """
13 base class for all CSSRule subclass tests
14
15 overwrite setUp with the appriopriate values, will be used in
16 test_init and test_readonly
17 overwrite all tests as you please, use::
18
19 super(CLASSNAME, self).test_TESTNAME(params)
20
21 to use the base class tests too
22 """
37
39 "CSSRule.type and init"
40 self.assertEqual(self.r_type, self.r.type)
41 self.assertEqual(self.r_typeString, self.r.typeString)
42 self.assertEqual(u'', self.r.cssText)
43 self.assertEqual(None, self.r.parentRule)
44 self.assertEqual(None, self.r.parentStyleSheet)
45
47 "CSSRule.parentRule .parentStyleSheet .type"
48 rules = [
49 ('@charset "ascii";', cssutils.css.CSSRule.CHARSET_RULE),
50 ('@import "x";', cssutils.css.CSSRule.IMPORT_RULE),
51 ('@namespace "x";', cssutils.css.CSSRule.NAMESPACE_RULE),
52 ('@font-face { src: url(x) }', cssutils.css.CSSRule.FONT_FACE_RULE),
53 ('''@media all {
54 @x;
55 a { color: red }
56 /* c */
57 }''', cssutils.css.CSSRule.MEDIA_RULE),
58 ('@page :left { color: red }', cssutils.css.CSSRule.PAGE_RULE),
59 ('@unknown;', cssutils.css.CSSRule.UNKNOWN_RULE),
60 ('b { left: 0 }', cssutils.css.CSSRule.STYLE_RULE),
61 ('/*1*/', cssutils.css.CSSRule.COMMENT)
62 ]
63 mrt = [cssutils.css.CSSRule.UNKNOWN_RULE,
64 cssutils.css.CSSRule.STYLE_RULE,
65 cssutils.css.CSSRule.COMMENT]
66 def test(s):
67 for i, rule in enumerate(s):
68 self.assertEqual(rule.parentRule, None)
69 self.assertEqual(rule.parentStyleSheet, s)
70 self.assertEqual(rule.type, rules[i][1])
71 if rule.MEDIA_RULE == rule.type:
72 for j, r in enumerate(rule):
73 self.assertEqual(r.parentRule, rule)
74 self.assertEqual(r.parentStyleSheet, s)
75 self.assertEqual(r.type, mrt[j])
76
77 if i == 0:
78 self.assertEqual('ascii', s.encoding)
79 elif i == 2:
80 self.assertEqual('x', s.namespaces[''])
81
82 cssText = u''.join(r[0] for r in rules)
83
84 s = cssutils.parseString(cssText)
85 test(s)
86
87 s = cssutils.css.CSSStyleSheet()
88 s.cssText = cssText
89 test(s)
90
91 s = cssutils.css.CSSStyleSheet()
92 for css, type_ in rules:
93 s.add(css)
94 test(s)
95
96 s = cssutils.css.CSSStyleSheet()
97 for css, type_ in rules:
98 s.insertRule(css)
99 test(s)
100
101 types = [cssutils.css.CSSCharsetRule,
102 cssutils.css.CSSImportRule,
103 cssutils.css.CSSNamespaceRule,
104 cssutils.css.CSSFontFaceRule,
105 cssutils.css.CSSMediaRule,
106 cssutils.css.CSSPageRule,
107 cssutils.css.CSSUnknownRule,
108 cssutils.css.CSSStyleRule,
109 cssutils.css.CSSComment]
110
111 s = cssutils.css.CSSStyleSheet()
112 for i, (css, type_) in enumerate(rules):
113 rule = types[i]()
114 rule.cssText = css
115 s.add(rule)
116 test(s)
117
118 s = cssutils.css.CSSStyleSheet()
119 for i, (css, type_) in enumerate(rules):
120 rule = types[i]()
121 rule.cssText = css
122 s.insertRule(rule)
123 test(s)
124
139
140 cssText = '@media all { %s }' % u''.join(r[0] for r in rules)
141
142 s = cssutils.parseString(cssText)
143 test(s)
144
145 s = cssutils.css.CSSStyleSheet()
146 s.cssText = cssText
147 test(s)
148
149 def getMediaSheet():
150 s = cssutils.css.CSSStyleSheet()
151 s.cssText = '@media all {}'
152 return s, s.cssRules[0]
153
154 s, mr = getMediaSheet()
155 for css, type_ in rules:
156 mr.add(css)
157 test(s)
158
159 s, mr = getMediaSheet()
160 for css, type_ in rules:
161 mr.insertRule(css)
162 test(s)
163
164 types = [cssutils.css.CSSStyleRule,
165 cssutils.css.CSSComment,
166 cssutils.css.CSSUnknownRule]
167
168 s, mr = getMediaSheet()
169 for i, (css, type_) in enumerate(rules):
170 rule = types[i]()
171 rule.cssText = css
172 mr.add(rule)
173 test(s)
174
175 s, mr = getMediaSheet()
176 for i, (css, type_) in enumerate(rules):
177 rule = types[i]()
178 rule.cssText = css
179 mr.insertRule(rule)
180 test(s)
181
183 "CSSRule readonly"
184 self.assertEqual(True, self.rRO._readonly)
185 self.assertEqual(u'', self.rRO.cssText)
186 self.assertRaises(xml.dom.NoModificationAllowedErr,
187 self.rRO._setCssText, u'x')
188 self.assertEqual(u'', self.rRO.cssText)
189
191 """
192 CSSRule.cssText InvalidModificationErr
193
194 called by subclasses
195
196 startwithspace
197
198 for test starting with this not the test but " test" is tested
199 e.g. " @page {}"
200 exception is the style rule test
201 """
202 tests = (u'',
203 u'/* comment */',
204 u'@charset "utf-8";',
205 u'@font-face {}',
206 u'@import url(x);',
207 u'@media all {}',
208 u'@namespace "x";'
209 u'@page {}',
210 u'@unknown;',
211 u'a style rule {}'
212 )
213 for test in tests:
214 if startwithspace in (u'a style rule', ) and test in (
215 u'/* comment */', u'a style rule {}'):
216 continue
217
218 if test.startswith(startwithspace):
219 test = u' %s' % test
220
221 self.assertRaises(xml.dom.InvalidModificationErr,
222 self.r._setCssText, test)
223
224
225 if __name__ == '__main__':
226 import unittest
227 unittest.main()
228