1
2 """tests for parsing which does not raise Exceptions normally
3 """
4 __author__ = '$LastChangedBy: cthedot $'
5 __date__ = '$LastChangedDate: 2008-02-11 20:28:23 +0100 (Mo, 11 Feb 2008) $'
6 __version__ = '$LastChangedRevision: 1004 $'
7
8 import xml.dom
9 import basetest
10 import cssutils
11
13
15 "cssutils encodings"
16 css1 = ur'''@charset "utf-8";
17 /* ä */'''
18 s = cssutils.parseString(css1)
19 css2 = unicode(s.cssText, 'utf-8')
20 self.assertEqual(css1, css2)
21
22 s = cssutils.parseString(css2)
23 s.cssRules[0].encoding='ascii'
24 css3 = ur'''@charset "ascii";
25 /* \E4 */'''
26 self.assertEqual(css3, unicode(s.cssText, 'utf-8'))
27
29 "cssutils escapes"
30 css = ur'\43\x { \43\x: \43\x !import\41nt }'
31 sheet = cssutils.parseString(css)
32 self.assertEqual(sheet.cssText, ur'''C\x {
33 c\x: C\x !important
34 }''')
35
36 css = ur'\ x{\ x :\ x ;y:1} '
37 sheet = cssutils.parseString(css)
38 self.assertEqual(sheet.cssText, ur'''\ x {
39 \ x: \ x;
40 y: 1
41 }''')
42
44 "cssutils.parseString(INVALID_STRING)"
45 validfromhere = '@namespace "x";'
46 csss = (
47 u'''@charset "ascii
48 ;''' + validfromhere,
49 u'''@charset 'ascii
50 ;''' + validfromhere,
51 u'''@namespace "y
52 ;''' + validfromhere,
53 u'''@import "y
54 ;''' + validfromhere,
55 u'''@import url('a
56 );''' + validfromhere,
57 u'''@unknown "y
58 ;''' + validfromhere)
59 for css in csss:
60 s = cssutils.parseString(css)
61 self.assertEqual(validfromhere, s.cssText)
62
63 css = u'''a { font-family: "Courier
64 ; }'''
65 s = cssutils.parseString(css)
66 self.assertEqual(u'', s.cssText)
67
69 "cssutils.parseString(INVALID_CSS)"
70 tests = {
71 u'a {color: blue}} a{color: red} a{color: green}':
72 u'''a {
73 color: blue
74 }
75 a {
76 color: green
77 }'''
78 }
79
80 for css in tests:
81 exp = tests[css]
82 if exp == None:
83 exp = css
84 s = cssutils.parseString(css)
85 self.assertEqual(exp, s.cssText)
86
88 "cssutils.parseString nesting"
89
90 tests = {
91 '@1; div{color:green}': u'div {\n color: green\n }',
92 '@1 []; div{color:green}': u'div {\n color: green\n }',
93 '@1 [{}]; div { color:green; }': u'div {\n color: green\n }',
94 '@media all { @ } div{color:green}':
95 u'div {\n color: green\n }',
96
97 '@1 { [ } div{color:green}': u'',
98
99 '@1 { [ } ] div{color:red}div{color:green}': u'div {\n color: green\n }',
100 }
101 for css, exp in tests.items():
102 self.assertEqual(exp, cssutils.parseString(css).cssText)
103
105 "cssutils.parseString(special_case)"
106 tests = {
107 u'''
108 a[title="a not s\
109 o very long title"] {/*...*/}''': u'''a[title="a not so very long title"] {
110 /*...*/
111 }'''
112 }
113 for css in tests:
114 exp = tests[css]
115 if exp == None:
116 exp = css
117 s = cssutils.parseString(css)
118 self.assertEqual(exp, s.cssText)
119
138
140 "cssutils.parseString(href, media)"
141 s = cssutils.parseString("a{}", href="file:foo.css", media="screen, projection, tv")
142 self.assertEqual(s.href, "file:foo.css")
143 self.assertEqual(s.media.mediaText, "screen, projection, tv")
144
145 s = cssutils.parseString("a{}", href="file:foo.css", media=["screen", "projection", "tv"])
146 self.assertEqual(s.media.mediaText, "screen, projection, tv")
147
151
152
153 if __name__ == '__main__':
154 import unittest
155 unittest.main()
156