1
2 """Tests for parsing which does not raise Exceptions normally"""
3 __version__ = '$Id: test_parse.py 1116 2008-03-05 13:52:23Z cthedot $'
4
5 import xml.dom
6 import basetest
7 import cssutils
8
10
12 tests = {
13
14 (u'@namespace "a";', 'BOGUS'): u'@namespace "a";',
15 ('@namespace "a";', None): u'@namespace "a";',
16 ('@namespace "a";', 'ascii'): u'@namespace "a";',
17
18 ('@namespace "b";', 'ascii'): '@namespace "b";',
19
20 ('@namespace "\xc3\xa4";', None): '@namespace "\xc3\xa4";',
21 ('@namespace "\xc3\xa4";', 'utf-8'): '@namespace "\xc3\xa4";'
22 }
23 for test in tests:
24 css, encoding = test
25 sheet = cssutils.parseString(css, encoding=encoding)
26 self.assertEqual(tests[test], sheet.cssText)
27
29 "cssutils encodings"
30 css1 = ur'''@charset "utf-8";
31 /* ä */'''
32 s = cssutils.parseString(css1)
33 css2 = unicode(s.cssText, 'utf-8')
34 self.assertEqual(css1, css2)
35
36 s = cssutils.parseString(css2)
37 s.cssRules[0].encoding='ascii'
38 css3 = ur'''@charset "ascii";
39 /* \E4 */'''
40 self.assertEqual(css3, unicode(s.cssText, 'utf-8'))
41
43 "cssutils escapes"
44 css = ur'\43\x { \43\x: \43\x !import\41nt }'
45 sheet = cssutils.parseString(css)
46 self.assertEqual(sheet.cssText, ur'''C\x {
47 c\x: C\x !important
48 }''')
49
50 css = ur'\ x{\ x :\ x ;y:1} '
51 sheet = cssutils.parseString(css)
52 self.assertEqual(sheet.cssText, ur'''\ x {
53 \ x: \ x;
54 y: 1
55 }''')
56
58 "cssutils.parseString(INVALID_STRING)"
59 validfromhere = '@namespace "x";'
60 csss = (
61 u'''@charset "ascii
62 ;''' + validfromhere,
63 u'''@charset 'ascii
64 ;''' + validfromhere,
65 u'''@namespace "y
66 ;''' + validfromhere,
67 u'''@import "y
68 ;''' + validfromhere,
69 u'''@import url('a
70 );''' + validfromhere,
71 u'''@unknown "y
72 ;''' + validfromhere)
73 for css in csss:
74 s = cssutils.parseString(css)
75 self.assertEqual(validfromhere, s.cssText)
76
77 css = u'''a { font-family: "Courier
78 ; }'''
79 s = cssutils.parseString(css)
80 self.assertEqual(u'', s.cssText)
81
100
102 "cssutils.parseString nesting"
103
104 tests = {
105 '@1; div{color:green}': u'div {\n color: green\n }',
106 '@1 []; div{color:green}': u'div {\n color: green\n }',
107 '@1 [{}]; div { color:green; }': u'div {\n color: green\n }',
108 '@media all { @ } div{color:green}':
109 u'div {\n color: green\n }',
110
111 '@1 { [ } div{color:green}': u'',
112
113 '@1 { [ } ] div{color:red}div{color:green}': u'div {\n color: green\n }',
114 }
115 for css, exp in tests.items():
116 self.assertEqual(exp, cssutils.parseString(css).cssText)
117
119 "cssutils.parseString(special_case)"
120 tests = {
121 u'''
122 a[title="a not s\
123 o very long title"] {/*...*/}''': u'''a[title="a not so very long title"] {
124 /*...*/
125 }'''
126 }
127 for css in tests:
128 exp = tests[css]
129 if exp == None:
130 exp = css
131 s = cssutils.parseString(css)
132 self.assertEqual(exp, s.cssText)
133
152
154 "cssutils.parseString(href, media)"
155 s = cssutils.parseString("a{}", href="file:foo.css", media="screen, projection, tv")
156 self.assertEqual(s.href, "file:foo.css")
157 self.assertEqual(s.media.mediaText, "screen, projection, tv")
158
159 s = cssutils.parseString("a{}", href="file:foo.css", media=["screen", "projection", "tv"])
160 self.assertEqual(s.media.mediaText, "screen, projection, tv")
161
165
166
167 if __name__ == '__main__':
168 import unittest
169 unittest.main()
170