1
2 """Testcases for cssutils.css.CSSCharsetRule"""
3 __version__ = '$Id: test_cssutils.py 1200 2008-03-23 00:18:51Z cthedot $'
4
5 import os
6 import tempfile
7 import urllib
8 import xml.dom
9 import basetest
10 import cssutils
11 import codecs
12
14
15 exp = u'''@import "import/import2.css";
16 a {
17 background-image: url(test/x.gif)
18 }
19 /* import.css*/'''
20
22 "cssutils.parseString()"
23 s = cssutils.parseString(self.exp,
24 media='handheld, screen',
25 title='from string')
26 self.assert_(isinstance(s, cssutils.css.CSSStyleSheet))
27 self.assertEqual(None, s.href)
28 self.assertEqual(self.exp, s.cssText)
29 self.assertEqual(u'utf-8', s.encoding)
30 self.assertEqual(u'handheld, screen', s.media.mediaText)
31 self.assertEqual(u'from string', s.title)
32 self.assertEqual(self.exp, s.cssText)
33
34 ir = s.cssRules[0]
35 self.assertEqual('import/import2.css', ir.href)
36 irs = ir.styleSheet
37 self.assertEqual(None, irs)
38
39 href = os.path.join(os.path.dirname(__file__),
40 '..', '..', '..', 'sheets', 'import.css')
41 href = 'file:' + urllib.pathname2url(href)
42 s = cssutils.parseString(self.exp,
43 href=href)
44 self.assertEqual(href, s.href)
45
46 ir = s.cssRules[0]
47 self.assertEqual('import/import2.css', ir.href)
48 irs = ir.styleSheet
49 self.assert_(isinstance(irs, cssutils.css.CSSStyleSheet))
50 self.assertEqual(u'''@import "../t2.css";
51 /* sheets/import2.css */''', irs.cssText)
52
54 "cssutils.parse()"
55
56 name = os.path.join(os.path.dirname(__file__),
57 '..', '..', '..', 'sheets', 'import.css')
58 href = 'file:' + urllib.pathname2url(name)
59
60 s = cssutils.parse(name, href=href, media='screen', title='from file')
61 self.assert_(isinstance(s, cssutils.css.CSSStyleSheet))
62 self.assert_(s.href.startswith('file:///'))
63 self.assert_(s.href.endswith('/sheets/import.css'))
64 self.assertEqual(u'utf-8', s.encoding)
65 self.assertEqual(u'screen', s.media.mediaText)
66 self.assertEqual(u'from file', s.title)
67 self.assertEqual(self.exp, s.cssText)
68
69 ir = s.cssRules[0]
70 self.assertEqual('import/import2.css', ir.href)
71 irs = ir.styleSheet
72 self.assert_(isinstance(irs, cssutils.css.CSSStyleSheet))
73 self.assertEqual(u'''@import "../t2.css";
74 /* sheets/import2.css */''', irs.cssText)
75
76
77
78 os.chdir(os.path.dirname(__file__))
79 name = os.path.join('..', '..', '..', 'sheets', 'import.css')
80
81 s = cssutils.parse(name, media='screen', title='from file')
82 self.assert_(isinstance(s, cssutils.css.CSSStyleSheet))
83 self.assert_(s.href.startswith('file:///'))
84 self.assert_(s.href.endswith('/sheets/import.css'))
85 self.assertEqual(u'utf-8', s.encoding)
86 self.assertEqual(u'screen', s.media.mediaText)
87 self.assertEqual(u'from file', s.title)
88 self.assertEqual(self.exp, s.cssText)
89
90 ir = s.cssRules[0]
91 self.assertEqual('import/import2.css', ir.href)
92 irs = ir.styleSheet
93 self.assert_(isinstance(irs, cssutils.css.CSSStyleSheet))
94 self.assertEqual(u'''@import "../t2.css";
95 /* sheets/import2.css */''', irs.cssText)
96
97
98 css = u'a:after { content: "羊蹄€\u2020" }'
99
100 fd, name = tempfile.mkstemp('_cssutilstest.css')
101 t = os.fdopen(fd, 'wb')
102 t.write(css.encode('utf-8'))
103 t.close()
104
105 self.assertRaises(
106 UnicodeDecodeError, cssutils.parse, name, 'ascii')
107
108
109 s = cssutils.parse(name, encoding='iso-8859-1')
110 self.assertEqual(cssutils.css.CSSStyleSheet, type(s))
111 self.assertEqual(s.cssRules[0].selectorText, 'a:after')
112
113 s = cssutils.parse(name, encoding='utf-8')
114 self.assertEqual(cssutils.css.CSSStyleSheet, type(s))
115 self.assertEqual(s.cssRules[0].selectorText, 'a:after')
116
117 css = u'@charset "iso-8859-1"; a:after { content: "ä" }'
118 t = codecs.open(name, 'w', 'iso-8859-1')
119 t.write(css)
120 t.close()
121
122 self.assertRaises(
123 UnicodeDecodeError, cssutils.parse, name, 'ascii')
124
125 s = cssutils.parse(name, encoding='iso-8859-1')
126 self.assertEqual(cssutils.css.CSSStyleSheet, type(s))
127 self.assertEqual(s.cssRules[1].selectorText, 'a:after')
128
129 self.assertRaises(
130 UnicodeDecodeError, cssutils.parse, name, 'utf-8')
131
132
133 os.remove(name)
134
136 "cssutils.parseUrl()"
137 href = os.path.join(os.path.dirname(__file__),
138 '..', '..', '..', 'sheets', 'import.css')
139 href = u'file:' + urllib.pathname2url(href)
140
141 s = cssutils.parseUrl(href,
142 media='tv, print',
143 title='from url')
144 self.assert_(isinstance(s, cssutils.css.CSSStyleSheet))
145 self.assertEqual(href, s.href)
146 self.assertEqual(self.exp, s.cssText)
147 self.assertEqual(u'utf-8', s.encoding)
148 self.assertEqual(u'tv, print', s.media.mediaText)
149 self.assertEqual('from url', s.title)
150
151 sr = s.cssRules[1]
152 img = sr.style.getProperty('background-image').cssValue.getStringValue()
153 self.assertEqual(img, 'test/x.gif')
154
155 ir = s.cssRules[0]
156 self.assertEqual(u'import/import2.css', ir.href)
157 irs = ir.styleSheet
158 self.assertEqual(u'''@import "../t2.css";
159 /* sheets/import2.css */''', irs.cssText)
160
161 ir2 = irs.cssRules[0]
162 self.assertEqual(u'../t2.css', ir2.href)
163 irs2 = ir2.styleSheet
164 self.assertEqual(u'/* t2 */', irs2.cssText)
165
182
184 "cssutils.getUrls()"
185 cssutils.ser.prefs.keepAllProperties = True
186
187 css='''
188 @import "im1";
189 @import url(im2);
190 @import url( im3 );
191 @import url( "im4" );
192 @import url( 'im5' );
193 a {
194 background-image: url(c) !important;
195 background-\image: url(b);
196 background: url(a) no-repeat !important;
197 }'''
198 s = cssutils.parseString(css)
199 urls = set(cssutils.getUrls(s))
200 self.assertEqual(urls, set(["im1", "im2", "im3", "im4", "im5",
201 "c", "b", "a"]))
202
203 cssutils.ser.prefs.keepAllProperties = False
204
206 "cssutils.replaceUrls()"
207 cssutils.ser.prefs.keepAllProperties = True
208
209 css='''
210 @import "im1";
211 @import url(im2);
212 a {
213 background-image: url(c) !important;
214 background-\image: url(b);
215 background: url(a) no-repeat !important;
216 }'''
217 s = cssutils.parseString(css)
218 cssutils.replaceUrls(s, lambda old: "NEW" + old)
219 self.assertEqual(u'@import "NEWim1";', s.cssRules[0].cssText)
220 self.assertEqual(u'NEWim2', s.cssRules[1].href)
221 self.assertEqual(u'''background-image: url(NEWc) !important;
222 background-\\image: url(NEWb);
223 background: url(NEWa) no-repeat !important''', s.cssRules[2].style.cssText)
224
225 cssutils.ser.prefs.keepAllProperties = False
226
227
228 if __name__ == '__main__':
229 import unittest
230 unittest.main()
231