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

# Source Code for Module cssutils.tests.test_cssstylesheet

      1  """
      2  tests for css.CSSStyleSheet
      3  """
      4  __author__ = '$LastChangedBy: cthedot $'
      5  __date__ = '$LastChangedDate: 2007-10-27 21:33:00 +0200 (Sa, 27 Okt 2007) $'
      6  __version__ = '$LastChangedRevision: 579 $'
      7
      8  import xml.dom
      9  import basetest
     10  import cssutils.css
     11
     12 -class CSSStyleSheetTestCase(basetest.BaseTestCase):
     13
     14 -    def setUp(self):
     15          super(CSSStyleSheetTestCase, self).setUp()
     16          self.r = cssutils.css.CSSStyleSheet() # used by basetest
     17          self.s = self.r # used here
     18          self.rule = cssutils.css.CSSStyleRule()
     19
     20 -    def test_init(self):
     21          "CSSStyleSheet.__init__()"
     22          self.assertEqual(False, self.s._readonly)
     23          self.assertEqual([], self.s.cssRules)
     24          self.assertEqual('text/css', self.s.type)
     25          self.assertEqual(False, self.s.disabled)
     26          self.assertEqual(None, self.s.href)
     27          self.assertEqual(None, self.s.media)
     28          self.assertEqual(None, self.s.ownerNode)
     29          self.assertEqual(None, self.s.parentStyleSheet)
     30          self.assertEqual(u'', self.s.title)
     31
     32 -    def test_NoModificationAllowedErr(self):
     33          "CSSStyleSheet NoModificationAllowedErr"
     34          css = cssutils.css.CSSStyleSheet(readonly=True)
     35
     36          self.assertEqual(True, css._readonly) # internal...
     37
     38          self.assertRaises(xml.dom.NoModificationAllowedErr,
     39                            css._setCssText, u'@x;')
     40          self.assertRaises(xml.dom.NoModificationAllowedErr,
     41                            css.insertRule, self.rule)
     42          self.assertRaises(xml.dom.NoModificationAllowedErr,
     43                            css.insertRule, self.rule, 0)
     44          self.assertRaises(xml.dom.NoModificationAllowedErr,
     45                            css.deleteRule, 0)
     46
     47 -    def test_cssText_HierarchyRequestErr(self):
     48          "CSSStyleSheet.cssText HierarchyRequestErr"
     49          tests = {
     50              # @charset: only one and always 1st
     51              u' @charset "utf-8";': xml.dom.HierarchyRequestErr,
     52              u'@charset "ascii";@charset "ascii";': xml.dom.HierarchyRequestErr,            u'/*c*/@charset "ascii";': xml.dom.HierarchyRequestErr,
     53              u'@import "x"; @charset "ascii";': xml.dom.HierarchyRequestErr,
     54              u'@namespace a "x"; @charset "ascii";': xml.dom.HierarchyRequestErr,
     55              u'@media all {} @charset "ascii";': xml.dom.HierarchyRequestErr,
     56              u'@page {} @charset "ascii";': xml.dom.HierarchyRequestErr,
     57              u'a {} @charset "ascii";': xml.dom.HierarchyRequestErr,
     58
     59              # @import: before @namespace, @media, @page, sr
     60              u'@namespace a "x"; @import "x";': xml.dom.HierarchyRequestErr,
     61              u'@media all {} @import "x";': xml.dom.HierarchyRequestErr,
     62              u'@page {} @import "x";': xml.dom.HierarchyRequestErr,
     63              u'a {} @import "x";': xml.dom.HierarchyRequestErr,
     64
     65              # @namespace: before @media, @page, sr
     66              u'@media all {} @namespace a "x";': xml.dom.HierarchyRequestErr,
     67              u'@page {} @namespace a "x";': xml.dom.HierarchyRequestErr,
     68              u'a {} @namespace a "x";': xml.dom.HierarchyRequestErr,
     69              }
     70          self.do_raise_r(tests)
     71          self.do_raise_p(tests)
     72
     73 -    def test_cssText_SyntaxErr(self):
     74          """CSSStyleSheet.cssText SyntaxErr
     75
     76          for single {, } or ;
     77          """
     78          tests = {
     79              u'{': xml.dom.SyntaxErr,
     80              u'}': xml.dom.SyntaxErr,
     81              u';': xml.dom.SyntaxErr,
     82              u'@charset "ascii";{': xml.dom.SyntaxErr,
     83              u'@charset "ascii";}': xml.dom.SyntaxErr,
     84              u'@charset "ascii";;': xml.dom.SyntaxErr,
     85              }
     86          self.do_raise_r(tests)
     87          self.do_raise_p(tests)
     88
     89 -    def test_incomplete(self):
     90          "CSSStyleRule (incomplete)"
     91          tests = {
     92              u'@import "a': u'@import "a";', # no }
     93              u'a { x: 1': u'a {\n    x: 1\n    }', # no }
     94              u'a { font-family: "arial sans': # no "
     95                  u'a {\n    font-family: "arial sans"\n    }',
     96          }
     97          self.do_equal_p(tests) # parse
     98
     99 -    def test_cssText(self):
    100          "CSSStyleSheet.cssText"
    101          tests = {
    102              u'': None,
    103              # @charset
    104              u'@charset "ascii";\n@import "x";': None,
    105              u'@charset "ascii";\n@media all {}': u'@charset "ascii";',
    106              u'@charset "ascii";\n@x;': None,
    107              u'@charset "ascii";\na {\n    x: 1\n    }': None,
    108              # @import
    109              u'@x;\n@import "x";': None,
    110              u'@import "x";\n@import "y";': None,
    111              u'@import "x";\n@media all {}': u'@import "x";',
    112              u'@import "x";\n@x;': None,
    113              u'@import "x";\na {\n    x: 1\n    }': None,
    114              # @namespace
    115              u'@x;\n@namespace a "x";': None,
    116              u'@namespace a "x";\n@namespace b "y";': None,
    117              u'@import "x";\n@namespace a "x";\n@media all {}': u'@import "x";\n@namespace a "x";',
    118              u'@namespace a "x";\n@x;': None,
    119              u'@namespace a "x";\na {\n    x: 1\n    }': None,
    120  #            ur'\1 { \2: \3 }': ur'''\1 {
    121  #    \2: \3
    122  #    }''',
    123  #            ur'''
    124  #            \@ { \@: \@ }
    125  #            \1 { \2: \3 }
    126  #            \{{\::\;;}
    127  #            ''': ur'''\@ {
    128  #    \@: \@
    129  #    }
    130  #\1 {
    131  #    \2: \3
    132  #    }
    133  #\{
    134  #    {\:: \;
    135  #    }'''
    136              }
    137          self.do_equal_r(tests)
    138          self.do_equal_p(tests)
    139
    140          s = cssutils.css.CSSStyleSheet()
    141          s.cssText = u'''@charset "ascii";@import "x";@namespace a "x";
    142          @media all {/*1*/}@page {margin: 0}a {\n    x: 1\n    }@unknown;/*comment*/'''
    143          for r in s.cssRules:
    144              self.assertEqual(s, r.parentStyleSheet)
    145
    146 -    def test_HTMLComments(self):
    147          "CSSStyleSheet CDO CDC"
    148          css = u'''body { color: red }
    149  <!-- comment -->
    150  body { color: blue }
    151  body { color: pink }
    152  <!-- comment -->
    153  body { color: green }
    154  '''
    155          exp = u'''body {
    156      color: red
    157      }
    158  body {
    159      color: pink
    160      }'''
    161          sheet = cssutils.parseString(css)
    162          self.assertEqual(sheet.cssText, exp)
    163
    164 -    def test_deleteRule(self):
    165          "CSSStyleSheet.deleteRule()"
    166          self.s.cssText = u'@charset "ascii"; @import "x"; @x; a {\n    x: 1\n    }@y;'
    167          self.assertEqual(5, self.s.cssRules.length)
    168
    169          self.assertRaises(xml.dom.IndexSizeErr, self.s.deleteRule, 5)
    170
    171          # end -1
    172          # check parentStyleSheet
    173          r = self.s.cssRules[-1]
    174          self.assertEqual(self.s, r.parentStyleSheet)
    175          self.s.deleteRule(-1)
    176          self.assertEqual(None, r.parentStyleSheet)
    177
    178          self.assertEqual(4, self.s.cssRules.length)
    179          self.assertEqual(
    180              u'@charset "ascii";\n@import "x";\n@x;\na {\n    x: 1\n    }', self.s.cssText)
    181          # beginning
    182          self.s.deleteRule(0)
    183          self.assertEqual(3, self.s.cssRules.length)
    184          self.assertEqual(u'@import "x";\n@x;\na {\n    x: 1\n    }', self.s.cssText)
    185          # middle
    186          self.s.deleteRule(1)
    187          self.assertEqual(2, self.s.cssRules.length)
    188          self.assertEqual(u'@import "x";\na {\n    x: 1\n    }', self.s.cssText)
    189          # end
    190          self.s.deleteRule(1)
    191          self.assertEqual(1, self.s.cssRules.length)
    192          self.assertEqual(u'@import "x";', self.s.cssText)
    193
    194 -    def _gets(self):
    195          # complete
    196          self.cr = cssutils.css.CSSCharsetRule('ascii')
    197          self.c = cssutils.css.CSSComment(u'/*c*/')
    198          self.ur = cssutils.css.CSSUnknownRule('@x;')
    199          self.ir = cssutils.css.CSSImportRule('x')
    200          self.nr = cssutils.css.CSSNamespaceRule('uri')
    201          self.mr = cssutils.css.CSSMediaRule()
    202          self.mr.cssText = u'@media all { @m; }'
    203          self.pr = cssutils.css.CSSPageRule()
    204          self.pr.style = u'margin: 0;'
    205          self.sr = cssutils.css.CSSStyleRule()
    206          self.sr.cssText = 'a {\n    x: 1\n    }'
    207
    208          s = cssutils.css.CSSStyleSheet()
    209          s.insertRule(self.cr) # 0
    210          s.insertRule(self.ir) # 1
    211          s.insertRule(self.nr) # 2
    212          s.insertRule(self.mr) # 3
    213          s.insertRule(self.sr) # 4
    214          s.insertRule(self.mr) # 5
    215          s.insertRule(self.pr) # 6
    216          s.insertRule(self.sr) # 7
    217          self.assertEqual(u'@charset "ascii";\n@import url(x);\n@namespace "uri";\n@media all {\n    @m;\n    }\na {\n    x: 1\n    }\n@media all {\n    @m;\n    }\n@page {\n    margin: 0\n    }\na {\n    x: 1\n    }', s.cssText)
    218          return s, s.cssRules.length
    219
    220 -    def test_insertRule(self):
    221          "CSSStyleSheet.insertRule()"
    222          s, L = self._gets()
    223
    224          # INVALID index
    225          self.assertRaises(xml.dom.IndexSizeErr,
    226                    s.insertRule, self.sr, -1)
    227          self.assertRaises(xml.dom.IndexSizeErr,
    228                    s.insertRule, self.sr, s.cssRules.length + 1)
    229          #   check if rule is really not in
    230          self.assertEqual(L, s.cssRules.length)
    231
    232 -    def _insertRule(self, rules, notbefore, notafter, anywhere):
    233          """
    234          helper
    235          test if any rule in rules cannot be inserted before rules in before
    236          or after rules in after but before and after rules in anywhere
    237          """
    238          for rule in rules:
    239              for r in notbefore:
    240                  s = cssutils.css.CSSStyleSheet()
    241                  s.insertRule(r)
    242                  self.assertRaises(xml.dom.HierarchyRequestErr,
    243                                    s.insertRule, rule, 0)
    244              for r in notafter:
    245                  s = cssutils.css.CSSStyleSheet()
    246                  s.insertRule(r)
    247                  self.assertRaises(xml.dom.HierarchyRequestErr,
    248                                    s.insertRule, rule, 1)
    249              for r in anywhere:
    250                  s = cssutils.css.CSSStyleSheet()
    251                  s.insertRule(r)
    252                  s.insertRule(rule, 0) # before
    253                  s.insertRule(rule) # after
    254                  self.assertEqual(s.cssRules.length, 3)
    255                  self.assertEqual(s, r.parentStyleSheet)
    256
    257 -    def test_insertRule_charset(self):
    258          "CSSStyleSheet.insertRule(@charset)"
    259          s, L = self._gets()
    260          notbefore = (self.cr,)
    261          notafter = (self.cr, self.ir, self.nr, self.mr, self.pr, self.sr,
    262                      self.c, self.ur)
    263          anywhere = ()
    264          self._insertRule((self.cr,),
    265                           notbefore, notafter, anywhere)
    266
    267 -    def test_insertRule_import(self):
    268          "CSSStyleSheet.insertRule(@import)"
    269          s, L = self._gets()
    270          notbefore = (self.cr,)
    271          notafter = (self.nr, self.pr, self.mr, self.sr)
    272          anywhere = (self.c, self.ur, self.ir)
    273          self._insertRule((self.ir,),
    274                           notbefore, notafter, anywhere)
    275
    276 -    def test_insertRule_namespace(self):
    277          "CSSStyleSheet.insertRule(@namespace)"
    278          s, L = self._gets()
    279          notbefore = (self.cr, self.ir)
    280          notafter = (self.pr, self.mr, self.sr)
    281          anywhere = (self.c, self.ur, self.nr)
    282          self._insertRule((self.nr,), notbefore, notafter, anywhere)
    283
    284 -    def test_insertRule_media_page_style(self):
    285          "CSSStyleSheet.insertRule(@media, @page, stylerule)"
    286          s, L = self._gets()
    287          notbefore = (self.cr, self.ir, self.nr)
    288          notafter = ()
    289          anywhere = (self.c, self.ur, self.mr, self.pr, self.sr)
    290          self._insertRule((self.pr, self.mr, self.sr),
    291                           notbefore, notafter, anywhere)
    292
    293 -    def test_insertRule_unknownandcomment(self):
    294          "CSSStyleSheet.insertRule(@ unknown, comment)"
    295          s, L = self._gets()
    296          notbefore = (self.cr,)
    297          notafter = ()
    298          anywhere = (self.c, self.ur, self.ir, self.nr, self.mr, self.pr,
    299                      self.sr)
    300          self._insertRule((self.ur,),
    301                           notbefore, notafter, anywhere)
    302
    303 -    def test_replaceUrls(self):
    304          cssutils.ser.prefs.keepAllProperties = True
    305
    306          css='''
    307          @import "im1";
    308          @import url(im2);
    309          a {
    310              background-image: url(c) !important;
    311              background-\image: url(b);
    312              background: url(a) no-repeat !important;
    313              }'''
    314          s = cssutils.parseString(css)
    315          s.replaceUrls(lambda old: "NEW" + old)
    316          self.assertEqual(u'@import "NEWim1";', s.cssRules[0].cssText)
    317          self.assertEqual(u'NEWim2', s.cssRules[1].href)
    318          self.assertEqual(u'''background-image: url(NEWc) !important;
    319  background-\\image: url(NEWb);
    320  background: url(NEWa) no-repeat !important''', s.cssRules[2].style.cssText)
    321
    322          cssutils.ser.prefs.keepAllProperties = False
    323
    324 -    def test_reprANDstr(self):
    325          "CSSStyleSheet.__repr__(), .__str__()"
    326          href = 'file:foo.css'
    327          title = 'title-of-css'
    328
    329          s = cssutils.css.CSSStyleSheet(href=href, title=title)
    330
    331          self.assert_(href in str(s))
    332          self.assert_(title in str(s))
    333
    334          s2 = eval(repr(s))
    335          self.assert_(isinstance(s2, s.__class__))
    336          self.assert_(href == s2.href)
    337          self.assert_(title == s2.title)
    338
    339
    340  if __name__ == '__main__':
    341      import unittest
    342      unittest.main()
    343