1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
|
# -*- coding: utf-8 -*-
"""Testcases for css_parser.css.CSSCharsetRule"""
from __future__ import with_statement, unicode_literals
from __future__ import absolute_import
from . import basetest
import codecs
import css_parser
import os
import sys
import tempfile
class CSSutilsTestCase(basetest.BaseTestCase):
def setUp(self):
css_parser.ser.prefs.useDefaults()
def tearDown(self):
css_parser.ser.prefs.useDefaults()
exp = '''@import "import/import2.css";
.import {
/* ./import.css */
background-image: url(images/example.gif)
}'''
def test_import_from_above(self):
def fetch(url):
self.assertEqual(url, '../test2.css')
return None, ''
p = css_parser.CSSParser(fetcher=fetch)
s = p.parseString("@import url('../test2.css'); a { background-image: url(../test.jpg); }", href='test.css')
self.assertEqual(
s.cssRules[1].style.getPropertyCSSValue('background-image')[0].absoluteUri,
'../test.jpg'
)
def fetch2(url):
self.assertEqual(url, 'a/test2.css')
return None, ''
p = css_parser.CSSParser(fetcher=fetch2)
s = p.parseString("@import url('../test2.css'); a { background-image: url(../test.jpg); }", href='a/b/test.css')
self.assertEqual(
s.cssRules[1].style.getPropertyCSSValue('background-image')[0].absoluteUri,
'a/test.jpg'
)
results = [('a/test.css', '@import url(../test.css);'), ('test.css', 'p { background-image: url(a/test.jpg) }')]
def fetch3(url):
e, text = results.pop(0)
self.assertEqual(e, url)
return None, text
p = css_parser.CSSParser(fetcher=fetch3)
s = p.parseString("@import url('../test.css'); a { background-image: url(../test.jpg); }", href='a/b/test.css')
style = s.cssRules[0].styleSheet.cssRules[0].styleSheet.cssRules[0].style
self.assertEqual(style.getPropertyCSSValue('background-image')[0].absoluteUri, 'a/test.jpg')
def test_parseString(self):
"css_parser.parseString()"
s = css_parser.parseString(
self.exp, media='handheld, screen', title='from string')
self.assertIsInstance(s, css_parser.css.CSSStyleSheet)
self.assertEqual(None, s.href)
self.assertEqual(self.exp.encode(), s.cssText)
self.assertEqual('utf-8', s.encoding)
self.assertEqual('handheld, screen', s.media.mediaText)
self.assertEqual('from string', s.title)
self.assertEqual(self.exp.encode(), s.cssText)
ir = s.cssRules[0]
self.assertEqual('import/import2.css', ir.href)
irs = ir.styleSheet
self.assertEqual(css_parser.css.CSSStyleSheet, type(irs))
href = basetest.get_sheet_filename('import.css')
href = css_parser.helper.path2url(href)
s = css_parser.parseString(self.exp, href=href)
self.assertEqual(href, s.href)
ir = s.cssRules[0]
self.assertEqual('import/import2.css', ir.href)
irs = ir.styleSheet
self.assertIsInstance(irs, css_parser.css.CSSStyleSheet)
self.assertEqual(
irs.cssText,
'@import "../import3.css";\n@import "import-impossible.css" print;\n.import2 {\n /* sheets/import2.css */\n background: url(http://example.com/images/example.gif);\n background: url(//example.com/images/example.gif);\n background: url(/images/example.gif);\n background: url(images2/example.gif);\n background: url(./images2/example.gif);\n background: url(../images/example.gif);\n background: url(./../images/example.gif)\n }' # noqa
.encode())
tests = {
'a {color: red}': 'a {\n color: red\n }',
'a {color: rgb(1,2,3)}': 'a {\n color: rgb(1, 2, 3)\n }'
}
self.do_equal_p(tests)
def test_parseFile(self):
"css_parser.parseFile()"
# name if used with open, href used for @import resolving
name = basetest.get_sheet_filename('import.css')
href = css_parser.helper.path2url(name)
s = css_parser.parseFile(
name, href=href, media='screen', title='from file')
self.assertIsInstance(s, css_parser.css.CSSStyleSheet)
if sys.platform.startswith('java'):
# on Jython only file:
self.assertTrue(s.href.startswith('file:'))
else:
# normally file:/// on win and file:/ on unix
self.assertTrue(s.href.startswith('file:/'))
self.assertTrue(s.href.endswith('/sheets/import.css'))
self.assertEqual('utf-8', s.encoding)
self.assertEqual('screen', s.media.mediaText)
self.assertEqual('from file', s.title)
self.assertEqual(self.exp.encode(), s.cssText)
ir = s.cssRules[0]
self.assertEqual('import/import2.css', ir.href)
irs = ir.styleSheet
self.assertIsInstance(irs, css_parser.css.CSSStyleSheet)
self.assertEqual(
irs.cssText,
'@import "../import3.css";\n@import "import-impossible.css" print;\n.import2 {\n /* sheets/import2.css */\n background: url(http://example.com/images/example.gif);\n background: url(//example.com/images/example.gif);\n background: url(/images/example.gif);\n background: url(images2/example.gif);\n background: url(./images2/example.gif);\n background: url(../images/example.gif);\n background: url(./../images/example.gif)\n }' # noqa
.encode())
# name is used for open and setting of href automatically
# test needs to be relative to this test file!
os.chdir(os.path.dirname(__file__))
name = basetest.get_sheet_filename('import.css')
s = css_parser.parseFile(name, media='screen', title='from file')
self.assertIsInstance(s, css_parser.css.CSSStyleSheet)
if sys.platform.startswith('java'):
# on Jython only file:
self.assertTrue(s.href.startswith('file:'))
else:
# normally file:/// on win and file:/ on unix
self.assertTrue(s.href.startswith('file:/'))
self.assertTrue(s.href.endswith('/sheets/import.css'))
self.assertEqual('utf-8', s.encoding)
self.assertEqual('screen', s.media.mediaText)
self.assertEqual('from file', s.title)
self.assertEqual(self.exp.encode(), s.cssText)
ir = s.cssRules[0]
self.assertEqual('import/import2.css', ir.href)
irs = ir.styleSheet
self.assertIsInstance(irs, css_parser.css.CSSStyleSheet)
self.assertEqual(
irs.cssText,
'@import "../import3.css";\n@import "import-impossible.css" print;\n.import2 {\n /* sheets/import2.css */\n background: url(http://example.com/images/example.gif);\n background: url(//example.com/images/example.gif);\n background: url(/images/example.gif);\n background: url(images2/example.gif);\n background: url(./images2/example.gif);\n background: url(../images/example.gif);\n background: url(./../images/example.gif)\n }' # noqa
.encode())
# next test
css = 'a:after { content: "羊蹄€\u2020" }'
fd, name = tempfile.mkstemp('_css_parsertest.css')
t = os.fdopen(fd, 'wb')
t.write(css.encode('utf-8'))
t.close()
self.assertRaises(UnicodeDecodeError, css_parser.parseFile, name,
'ascii')
# ???
s = css_parser.parseFile(name, encoding='iso-8859-1')
self.assertEqual(css_parser.css.CSSStyleSheet, type(s))
self.assertEqual(s.cssRules[1].selectorText, 'a:after')
s = css_parser.parseFile(name, encoding='utf-8')
self.assertEqual(css_parser.css.CSSStyleSheet, type(s))
self.assertEqual(s.cssRules[1].selectorText, 'a:after')
css = '@charset "iso-8859-1"; a:after { content: "ä" }'
t = codecs.open(name, 'w', 'iso-8859-1')
t.write(css)
t.close()
self.assertRaises(UnicodeDecodeError, css_parser.parseFile, name,
'ascii')
s = css_parser.parseFile(name, encoding='iso-8859-1')
self.assertEqual(css_parser.css.CSSStyleSheet, type(s))
self.assertEqual(s.cssRules[1].selectorText, 'a:after')
self.assertRaises(UnicodeDecodeError, css_parser.parseFile, name,
'utf-8')
# clean up
try:
os.remove(name)
except EnvironmentError:
pass
def test_parseUrl(self):
"css_parser.parseUrl()"
href = basetest.get_sheet_filename('import.css')
# href = u'file:' + urllib.pathname2url(href)
href = css_parser.helper.path2url(href)
# href = 'http://seewhatever.de/sheets/import.css'
s = css_parser.parseUrl(href, media='tv, print', title='from url')
self.assertIsInstance(s, css_parser.css.CSSStyleSheet)
self.assertEqual(href, s.href)
self.assertEqual(self.exp.encode(), s.cssText)
self.assertEqual('utf-8', s.encoding)
self.assertEqual('tv, print', s.media.mediaText)
self.assertEqual('from url', s.title)
sr = s.cssRules[1]
img = sr.style.getProperty('background-image').propertyValue[0].value
self.assertEqual(img, 'images/example.gif')
ir = s.cssRules[0]
self.assertEqual('import/import2.css', ir.href)
irs = ir.styleSheet
self.assertEqual(
irs.cssText,
'@import "../import3.css";\n@import "import-impossible.css" print;\n.import2 {\n /* sheets/import2.css */\n background: url(http://example.com/images/example.gif);\n background: url(//example.com/images/example.gif);\n background: url(/images/example.gif);\n background: url(images2/example.gif);\n background: url(./images2/example.gif);\n background: url(../images/example.gif);\n background: url(./../images/example.gif)\n }' # noqa
.encode())
ir2 = irs.cssRules[0]
self.assertEqual('../import3.css', ir2.href)
irs2 = ir2.styleSheet
self.assertEqual(
irs2.cssText,
'/* import3 */\n.import3 {\n /* from ./import/../import3.css */\n background: url(images/example3.gif);\n background: url(./images/example3.gif);\n background: url(import/images2/example2.gif);\n background: url(./import/images2/example2.gif);\n background: url(import/images2/../../images/example3.gif)\n }' # noqa
.encode())
def test_setCSSSerializer(self):
"css_parser.setSerializer() and css_parser.ser"
s = css_parser.parseString('a { left: 0 }')
exp4 = '''a {
left: 0
}'''
exp1 = '''a {
left: 0
}'''
self.assertEqual(exp4.encode(), s.cssText)
newser = css_parser.CSSSerializer(
css_parser.serialize.Preferences(indent=' '))
css_parser.setSerializer(newser)
self.assertEqual(exp1.encode(), s.cssText)
newser = css_parser.CSSSerializer(
css_parser.serialize.Preferences(indent=' '))
css_parser.ser = newser
self.assertEqual(exp4.encode(), s.cssText)
def test_parseStyle(self):
"css_parser.parseStyle()"
s = css_parser.parseStyle('x:0; y:red')
self.assertEqual(type(s), css_parser.css.CSSStyleDeclaration)
self.assertEqual(s.cssText, 'x: 0;\ny: red')
s = css_parser.parseStyle('@import "x";')
self.assertEqual(type(s), css_parser.css.CSSStyleDeclaration)
self.assertEqual(s.cssText, '')
tests = [('content: "ä"', 'iso-8859-1'), ('content: "€"', 'utf-8')]
for v, e in tests:
s = css_parser.parseStyle(v.encode(e), encoding=e)
self.assertEqual(s.cssText, v)
self.assertRaises(UnicodeDecodeError, css_parser.parseStyle,
'content: "ä"'.encode('utf-8'), 'ascii')
def test_getUrls(self):
"css_parser.getUrls()"
css_parser.ser.prefs.keepAllProperties = True
css = r'''
@import "im1";
@import url(im2);
@import url( im3 );
@import url( "im4" );
@import url( 'im5' );
a {
background-image: url(a) !important;
background-\image: url(b);
background: url(c) no-repeat !important;
/* issue #46 */
src: local("xx"),
url("f.woff") format("woff"),
url("f.otf") format("opentype"),
url("f.svg#f") format("svg");
}'''
urls = set(css_parser.getUrls(css_parser.parseString(css)))
self.assertEqual(
urls,
set([
"im1", "im2", "im3", "im4", "im5", "a", "b", "c", 'f.woff',
'f.svg#f', 'f.otf'
]))
css_parser.ser.prefs.keepAllProperties = False
def test_replaceUrls(self):
"css_parser.replaceUrls()"
css_parser.ser.prefs.keepAllProperties = True
css = r'''
@import "im1";
@import url(im2);
a {
background-image: url(c) !important;
background-\image: url(b);
background: url(a) no-repeat !important;
}'''
s = css_parser.parseString(css)
css_parser.replaceUrls(s, lambda old: "NEW" + old)
self.assertEqual('@import "NEWim1";', s.cssRules[0].cssText)
self.assertEqual('NEWim2', s.cssRules[1].href)
self.assertEqual(
'''background-image: url(NEWc) !important;
background-\\image: url(NEWb);
background: url(NEWa) no-repeat !important''', s.cssRules[2].style.cssText)
css_parser.ser.prefs.keepAllProperties = False
# CSSStyleDeclaration
style = css_parser.parseStyle('''color: red;
background-image:
url(1.png),
url('2.png')''')
css_parser.replaceUrls(style, lambda url: 'prefix/' + url)
self.assertEqual(
style.cssText, '''color: red;
background-image: url(prefix/1.png), url(prefix/2.png)''')
def test_resolveImports(self):
"css_parser.resolveImports(sheet)"
self._tempSer()
css_parser.ser.prefs.useMinified()
a = '@charset "iso-8859-1";@import"b.css";\xe4{color:green}'.encode(
'iso-8859-1')
b = '@charset "ascii";\\E4 {color:red}'.encode('ascii')
# normal
with self.patch_default_fetcher((None, b)):
s = css_parser.parseString(a)
# py3 TODO
self.assertEqual(a, s.cssText)
self.assertEqual(b, s.cssRules[1].styleSheet.cssText)
c = css_parser.resolveImports(s)
# py3 TODO
self.assertEqual(
'\xc3\xa4{color:red}\xc3\xa4{color:green}'.encode(
'iso-8859-1'), c.cssText)
c.encoding = 'ascii'
self.assertEqual(
r'@charset "ascii";\E4 {color:red}\E4 {color:green}'.
encode(), c.cssText)
# b cannot be found
with self.patch_default_fetcher((None, None)):
s = css_parser.parseString(a)
# py3 TODO
self.assertEqual(a, s.cssText)
self.assertEqual(css_parser.css.CSSStyleSheet, type(s.cssRules[1].styleSheet))
c = css_parser.resolveImports(s)
# py3 TODO
self.assertEqual(
'@import"b.css";\xc3\xa4{color:green}'.encode(
'iso-8859-1'), c.cssText)
# @import with media
a = '@import"b.css";@import"b.css" print, tv ;@import"b.css" all;'
b = 'a {color: red}'
with self.patch_default_fetcher((None, b)):
s = css_parser.parseString(a)
c = css_parser.resolveImports(s)
self.assertEqual(
'a{color:red}@media print,tv{a{color:red}}a{color:red}'.
encode(), c.cssText)
# cannot resolve with media => keep original
a = '@import"b.css"print;'
b = '@namespace "http://example.com";'
with self.patch_default_fetcher((None, b)):
s = css_parser.parseString(a)
c = css_parser.resolveImports(s)
self.assertEqual(a.encode(), c.cssText)
# urls are adjusted too, layout:
# a.css
# c.css
# img/img.gif
# b/
# b.css
# subimg/subimg.gif
a = '''
@import"b/b.css";
a {
x: url(/img/abs.gif);
y: url(img/img.gif);
z: url(b/subimg/subimg.gif);
}'''
def fetcher(url):
c = {
'b.css':
'''
@import"../c.css";
b {
x: url(/img/abs.gif);
y: url(../img/img.gif);
z: url(subimg/subimg.gif);
}''',
'c.css':
'''
c {
x: url(/img/abs.gif);
y: url(./img/img.gif);
z: url(./b/subimg/subimg.gif);
}'''
}
return 'utf-8', c[os.path.split(url)[1]]
def do():
with self.patch_default_fetcher(fetcher):
s = css_parser.parseString(a)
r = css_parser.resolveImports(s)
return s, r
s, r = do()
css_parser.ser.prefs.useDefaults()
css_parser.ser.prefs.keepComments = False
self.assertEqual(
'''c {
x: url(/img/abs.gif);
y: url(img/img.gif);
z: url(b/subimg/subimg.gif)
}
b {
x: url(/img/abs.gif);
y: url(img/img.gif);
z: url(b/subimg/subimg.gif)
}
a {
x: url(/img/abs.gif);
y: url(img/img.gif);
z: url(b/subimg/subimg.gif)
}'''.encode(), r.cssText)
css_parser.ser.prefs.useDefaults()
if __name__ == '__main__':
import unittest
unittest.main()
|