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