Package cssutils :: Package tests :: Module test_cssutils |
[hide private]
[frames] | no frames]

# Source Code for Module cssutils.tests.test_cssutils

      1  # -*- coding: utf-8 -*-
      2  """Testcases for cssutils.css.CSSCharsetRule"""
      3  __version__ = '$Id: test_cssutils.py 1137 2008-03-15 18:24:39Z cthedot $'
      4
      5  import os
      6  import tempfile
      7  import xml.dom
      8  import basetest
      9  import cssutils
     10  import codecs
     11
     12 -class CSSutilsTestCase(basetest.BaseTestCase):
     13
     14 -    def test_parse(self):
     15          "cssutils.parse()"
     16          css = u'a:after { content: "羊蹄€\u2020" }'
     17
     18          fd, name = tempfile.mkstemp('_cssutilstest.css')
     19          t = os.fdopen(fd, 'wb')
     20          t.write(css.encode('utf-8'))
     21          t.close()
     22
     23          self.assertRaises(
     24              UnicodeDecodeError, cssutils.parse, name, 'ascii')
     25
     26          # ???
     27          s = cssutils.parse(name, encoding='iso-8859-1')
     28          self.assertEqual(cssutils.css.CSSStyleSheet, type(s))
     29          self.assertEqual(s.cssRules[0].selectorText, 'a:after')
     30
     31          s = cssutils.parse(name, encoding='utf-8')
     32          self.assertEqual(cssutils.css.CSSStyleSheet, type(s))
     33          self.assertEqual(s.cssRules[0].selectorText, 'a:after')
     34
     35          css = u'@charset "iso-8859-1"; a:after { content: "ä" }'
     36          t = codecs.open(name, 'w', 'iso-8859-1')
     37          t.write(css)
     38          t.close()
     39
     40          self.assertRaises(
     41              UnicodeDecodeError, cssutils.parse, name, 'ascii')
     42
     43          s = cssutils.parse(name, encoding='iso-8859-1')
     44          self.assertEqual(cssutils.css.CSSStyleSheet, type(s))
     45          self.assertEqual(s.cssRules[1].selectorText, 'a:after')
     46
     47          self.assertRaises(
     48              UnicodeDecodeError, cssutils.parse, name, 'utf-8')
     49
     50          # clean up
     51          os.remove(name)
     52
     53 -    def test_parseString(self):
     54          "cssutils.parseString()"
     55          exp = '''a {
     56      left: 0
     57      }'''
     58          s = cssutils.parseString(exp)
     59          self.assertEqual(cssutils.css.CSSStyleSheet, type(s))
     60          self.assertEqual(exp, s.cssText)
     61
     62 -    def test_setCSSSerializer(self):
     63          "cssutils.setSerializer() and cssutils.ser"
     64          s = cssutils.parseString('a { left: 0 }')
     65          exp4 = '''a {
     66      left: 0
     67      }'''
     68          exp1 = '''a {
     69   left: 0
     70   }'''
     71          self.assertEqual(exp4, s.cssText)
     72          newser = cssutils.CSSSerializer(cssutils.serialize.Preferences(indent=' '))
     73          cssutils.setSerializer(newser)
     74          self.assertEqual(exp1, s.cssText)
     75          newser = cssutils.CSSSerializer(cssutils.serialize.Preferences(indent='    '))
     76          cssutils.ser = newser
     77          self.assertEqual(exp4, s.cssText)
     78
     79 -    def test_getUrls(self):
     80          "cssutils.getUrls()"
     81          cssutils.ser.prefs.keepAllProperties = True
     82
     83          css='''
     84          @import "im1";
     85          @import url(im2);
     86          @import url( im3 );
     87          @import url( "im4" );
     88          @import url( 'im5' );
     89          a {
     90              background-image: url(c) !important;
     91              background-\image: url(b);
     92              background: url(a) no-repeat !important;
     93              }'''
     94          s = cssutils.parseString(css)
     95          urls = set(cssutils.getUrls(s))
     96          self.assertEqual(urls, set(["im1", "im2", "im3", "im4", "im5",
     97                                      "c", "b", "a"]))
     98
     99          cssutils.ser.prefs.keepAllProperties = False
    100
    101 -    def test_replaceUrls(self):
    102          "cssutils.replaceUrls()"
    103          cssutils.ser.prefs.keepAllProperties = True
    104
    105          css='''
    106          @import "im1";
    107          @import url(im2);
    108          a {
    109              background-image: url(c) !important;
    110              background-\image: url(b);
    111              background: url(a) no-repeat !important;
    112              }'''
    113          s = cssutils.parseString(css)
    114          cssutils.replaceUrls(s, lambda old: "NEW" + old)
    115          self.assertEqual(u'@import "NEWim1";', s.cssRules[0].cssText)
    116          self.assertEqual(u'NEWim2', s.cssRules[1].href)
    117          self.assertEqual(u'''background-image: url(NEWc) !important;
    118  background-\\image: url(NEWb);
    119  background: url(NEWa) no-repeat !important''', s.cssRules[2].style.cssText)
    120
    121          cssutils.ser.prefs.keepAllProperties = False
    122
    123
    124  if __name__ == '__main__':
    125      import unittest
    126      unittest.main()
    127