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

# Source Code for Module cssutils.tests.test_cssstyledeclaration

      1  """Testcases for cssutils.css.cssstyledelaration.CSSStyleDeclaration."""
      2  __author__ = '$LastChangedBy: cthedot $'
      3  __date__ = '$LastChangedDate: 2007-08-26 15:35:29 +0200 (So, 26 Aug 2007) $'
      4  __version__ = '$LastChangedRevision: 282 $'
      5
      6  import xml.dom
      7  import basetest
      8  import cssutils
      9
     10
     11 -class CSSStyleDeclarationTestCase(basetest.BaseTestCase):
     12
     13 -    def setUp(self):
     14          self.r = cssutils.css.CSSStyleDeclaration()
     15
     16 -    def test_init(self):
     17          "CSSStyleDeclaration.__init__()"
     18          s = cssutils.css.CSSStyleDeclaration()
     19          self.assertEqual(u'', s.cssText)
     20          self.assertEqual(0, s.length)
     21          self.assertEqual(None, s.parentRule)
     22
     23          s = cssutils.css.CSSStyleDeclaration(cssText='left: 0')
     24          self.assertEqual(u'left: 0', s.cssText)
     25          self.assertEqual('0', s.getPropertyValue('left'))
     26
     27          sheet = cssutils.css.CSSStyleRule()
     28          s = cssutils.css.CSSStyleDeclaration(sheet)
     29          self.assertEqual(sheet, s.parentRule)
     30
     31 -    def test_parseString(self):
     32          "CSSStyleDeclaration parseString()"
     33          # error but parse
     34
     35          tests = {
     36              # property names are caseinsensitive
     37              u'TOP:0': u'top: 0',
     38              u'top:0': u'top: 0',
     39              # simple escape
     40              u'c\\olor: red; color:green': u'color: green',
     41              u'color:g\\reen': u'color: g\\reen',
     42
     43              u'color:green': u'color: green',
     44              u'color:green; color': u'color: green',
     45              u'color:red;   color; color:green': u'color: green',
     46              u'color:green; color:': u'color: green',
     47              u'color:red;   color:; color:green': u'color: green',
     48              u'color:green; color{;color:maroon}': u'color: green',
     49              u'color:red;   color{;color:maroon}; color:green':
     50                  u'color: green',
     51              # tantek hack
     52              ur'''color: red;
     53  voice-family: "\"}\"";
     54  voice-family:inherit;
     55  color: green;''': 'color: green;\nvoice-family: inherit'
     56              }
     57          for test, exp in tests.items():
     58              sh = cssutils.parseString('a { %s }' % test)
     59              if exp is None:
     60                  exp = u'%s' % test
     61              elif exp != u'':
     62                  exp = u'%s' % exp
     63              self.assertEqual(exp, sh.cssRules[0].style.cssText)
     64
     65 -    def test_cssText(self):
     66          "CSSStyleDeclaration.cssText"
     67          # empty
     68          s = cssutils.css.CSSStyleDeclaration()
     69          tests = {
     70              u'': u'',
     71              u' ': u'',
     72              u' \t \n  ': u'',
     73              u'/*x*/': u'/*x*/'
     74              }
     75          for test, exp in tests.items():
     76              s.cssText = 'left: 0;' # dummy to reset s
     77              s.cssText = test
     78              self.assertEqual(exp, s.cssText)
     79
     80          # normal
     81          s = cssutils.css.CSSStyleDeclaration()
     82          tests = {
     83              u'left: 0': u'left: 0',
     84              u'left:0': u'left: 0',
     85              u' left : 0 ': u'left: 0',
     86              u'left: 0;': u'left: 0',
     87              u'left: 0 !important ': u'left: 0 !important',
     88              u'left:0!important': u'left: 0 !important',
     89              u'left: 0; top: 1': u'left: 0;\ntop: 1',
     90              }
     91          for test, exp in tests.items():
     92              s.cssText = test
     93              self.assertEqual(exp, s.cssText)
     94
     95          # exception
     96          tests = {
     97              u'top': xml.dom.SyntaxErr,
     98              u'top:': xml.dom.SyntaxErr,
     99              u'top : ': xml.dom.SyntaxErr,
    100              u'top:!important': xml.dom.SyntaxErr,
    101              u'top:!important;': xml.dom.SyntaxErr,
    102              u'top:;': xml.dom.SyntaxErr,
    103              u'top 0': xml.dom.SyntaxErr,
    104              u'top 0;': xml.dom.SyntaxErr,
    105
    106              u':': xml.dom.SyntaxErr,
    107              u':0': xml.dom.SyntaxErr,
    108              u':0;': xml.dom.SyntaxErr,
    109              u':0!important': xml.dom.SyntaxErr,
    110              u':;': xml.dom.SyntaxErr,
    111              u':0;': xml.dom.SyntaxErr,
    112              u':0!important;': xml.dom.SyntaxErr,
    113
    114              u'0': xml.dom.SyntaxErr,
    115              u'0!important': xml.dom.SyntaxErr,
    116              u'0!important;': xml.dom.SyntaxErr,
    117
    118              u'!important': xml.dom.SyntaxErr,
    119              u'!important;': xml.dom.SyntaxErr,
    120
    121              u';': xml.dom.SyntaxErr,
    122              }
    123          self.do_raise_r(tests)
    124
    125 -    def test_cssText(self):
    126          "CSSStyleDeclaration.getCssText(separator)"
    127          s = cssutils.css.CSSStyleDeclaration(cssText=u'a:1;b:2')
    128          self.assertEqual(u'a: 1;\nb: 2', s.getCssText())
    129          self.assertEqual(u'a: 1;b: 2', s.getCssText(separator=u''))
    130          self.assertEqual(u'a: 1;/*x*/b: 2', s.getCssText(separator=u'/*x*/'))
    131
    132 -    def test_length(self):
    133          "CSSStyleDeclaration.length"
    134          s = cssutils.css.CSSStyleDeclaration()
    135
    136          # cssText
    137          s.cssText = u'left: 0'
    138          self.assertEqual(1, s.length)
    139          self.assertEqual(1, len(s.seq))
    140          s.cssText = u'/*1*/left/*x*/:/*x*/0/*x*/;/*2*/ top: 1;/*3*/'
    141          self.assertEqual(2, s.length)
    142          self.assertEqual(5, len(s.seq))
    143
    144          # set
    145          s = cssutils.css.CSSStyleDeclaration()
    146          s.setProperty('top', '0', '!important')
    147          self.assertEqual(1, s.length)
    148          s.setProperty('top', '1')
    149          self.assertEqual(1, s.length)
    150          s.setProperty('left', '1')
    151
    152 -    def test_parentRule(self):
    153          "CSSStyleDeclaration.parentRule"
    154          s = cssutils.css.CSSStyleDeclaration()
    155          sheet = cssutils.css.CSSStyleRule()
    156          s.parentRule = sheet
    157          self.assertEqual(sheet, s.parentRule)
    158
    159          sheet = cssutils.parseString(u'a{x:1}')
    160          s = sheet.cssRules[0]
    161          d = s.style
    162          self.assertEqual(s, d.parentRule)
    163
    164 -    def test_getPropertyCSSValue(self):
    165          "CSSStyleDeclaration.getPropertyCSSValue()"
    166          s = cssutils.css.CSSStyleDeclaration()
    167
    168          # not set
    169          self.assertEqual(None, s.getPropertyCSSValue('color'))
    170
    171  #        # shorthand CSSValue should be None
    172  #        SHORTHAND = [
    173  #            u'background',
    174  #            u'border',
    175  #            u'border-left', u'border-right',
    176  #            u'border-top', u'border-bottom',
    177  #            u'border-color', u'border-style', u'border-width',
    178  #            u'cue',
    179  #            u'font',
    180  #            u'list-style',
    181  #            u'margin',
    182  #            u'outline',
    183  #            u'padding',
    184  #            u'pause']
    185  #        for short in SHORTHAND:
    186  #            s.setProperty(short, u'inherit')
    187  #            self.assertEqual(None, s.getPropertyCSSValue(short))
    188
    189 -    def test_getPropertyPriority(self):
    190          "CSSStyleDeclaration.getPropertyPriority()"
    191          s = cssutils.css.CSSStyleDeclaration()
    192          self.assertEqual(u'', s.getPropertyPriority('unset'))
    193
    194          s.setProperty(u'left', u'0', u'!important')
    195          self.assertEqual(u'!important', s.getPropertyPriority('left'))
    196
    197 -    def test_getPropertyValue(self):
    198          "CSSStyleDeclaration.getPropertyValue()"
    199          s = cssutils.css.CSSStyleDeclaration()
    200          self.assertEqual(u'', s.getPropertyValue('unset'))
    201
    202          s.setProperty(u'left', '0')
    203          self.assertEqual(u'0', s.getPropertyValue('left'))
    204
    205          s.setProperty(u'border', '1px  solid  green')
    206          self.assertEqual(u'1px solid green', s.getPropertyValue('border'))
    207
    208 -    def test_getSameNamePropertyList(self):
    209          "CSSStyleDeclaration.getSameNamePropertyList()"
    210          s = cssutils.css.CSSStyleDeclaration(cssText='color: red')
    211          pl = s.getSameNamePropertyList('color')
    212          self.assertEqual('color', pl.name)
    213          self.assertEqual(1, len(pl))
    214          self.assertEqual(0, pl._currentIndex())
    215
    216          s.setProperty('C\olor', 'green', '!important', overwrite=False)
    217          self.assertEqual('color', pl.name)
    218          self.assertEqual(2, len(pl))
    219          self.assertEqual(1, pl._currentIndex())
    220  ##        self.assertEqual(
    221  ##            '[<Property> color: red , <Property> c\\olor: green !important]',
    222  ##            str(pl))
    223
    224          s.setProperty('COLOR', 'blue', overwrite=False)
    225          self.assertEqual('color', pl.name)
    226          self.assertEqual(3, len(pl))
    227          self.assertEqual(1, pl._currentIndex())
    228  ##        self.assertEqual(
    229  ##            '[<Property> color: red , <Property> c\\olor: green !important, <Property> color: blue ]',
    230  ##            str(pl))
    231
    232 -    def test_item(self):
    233          "CSSStyleDeclaration.item()"
    234          _props = ('left', 'top', 'right')
    235          s = cssutils.css.CSSStyleDeclaration(cssText=
    236              '\left:0;TOP:1;right:3')
    237          for i in range(0,3):
    238              self.assertEqual(_props[i], s.item(i))
    239              self.assertEqual(_props[-1-i], s.item(-1-i))
    240          self.assertEqual(u'', s.item(3))
    241          self.assertEqual(u'', s.item(-4))
    242
    243 -    def test_removeProperty(self):
    244          "CSSStyleDeclaration.removeProperty()"
    245          s = cssutils.css.CSSStyleDeclaration(cssText='top: 0 !important')
    246          self.assertEqual('0', s.removeProperty('top'))
    247          self.assertEqual(0, s.length)
    248          self.assertEqual('', s.removeProperty('top'))
    249          self.assertEqual(0, s.length)
    250
    251 -    def test_setProperty_overwrite(self):
    252          "CSSStyleDeclaration.setProperty(overwrite=True)"
    253          s = cssutils.css.CSSStyleDeclaration()
    254          s.cssText = 'color: red; top: 1px'
    255          self.assertEqual(2, s.length)
    256          pl = s.getSameNamePropertyList('color')
    257          self.assertEqual(1, len(pl))
    258
    259          # overwrite (default)
    260          s.setProperty('color', 'green', '!important')
    261          self.assertEqual(2, s.length)
    262          self.assertEqual('color', s.item(0))
    263          self.assertEqual('top', s.item(1))
    264          self.assertEqual('green', s.getPropertyValue('color'))
    265          pl = s.getSameNamePropertyList('color')
    266          self.assertEqual(1, len(pl))
    267
    268          # append value
    269          s.setProperty('color', 'blue', overwrite=False)
    270          self.assertEqual(2, s.length)
    271          self.assertEqual('color', s.item(0))
    272          self.assertEqual('top', s.item(1))
    273          # new value is not important
    274          self.assertEqual('green', s.getPropertyValue('color'))
    275          pl = s.getSameNamePropertyList('color')
    276          self.assertEqual(2, len(pl))
    277
    278          # overwrite
    279          s.setProperty('color', 'red', overwrite=True)
    280          self.assertEqual(2, s.length)
    281          self.assertEqual('color', s.item(0))
    282          self.assertEqual('top', s.item(1))
    283          self.assertEqual('red', s.getPropertyValue('color'))
    284          pl = s.getSameNamePropertyList('color')
    285          self.assertEqual(1, len(pl))
    286
    287 -    def test_setProperty(self):
    288          "CSSStyleDeclaration.setProperty()"
    289          s = cssutils.css.CSSStyleDeclaration()
    290          s.setProperty('top', '0', '!important')
    291          self.assertEqual('0', s.getPropertyValue('top'))
    292          self.assertEqual('!important', s.getPropertyPriority('top'))
    293          s.setProperty('top', '1')
    294          self.assertEqual('1', s.getPropertyValue('top'))
    295          self.assertEqual('', s.getPropertyPriority('top'))
    296
    297          # case insensitive
    298          s.setProperty('TOP', '0', '!IMPORTANT')
    299          self.assertEqual('0', s.getPropertyValue('top'))
    300          self.assertEqual('!important', s.getPropertyPriority('top'))
    301          self.assertEqual('0', s.getPropertyValue('top'))
    302          self.assertEqual('!important', s.getPropertyPriority('top'))
    303
    304          tests = {
    305              (u'left', u'0px', u''): u'left: 0px',
    306              (u'left', u'0px', u'!important'): u'left: 0px !important',
    307              (u'LEFT', u'0px', u'!important'): u'left: 0px !important',
    308              (u'left', u'0px', u'!important'): u'left: 0px !important',
    309              }
    310          for test, exp in tests.items():
    311              s = cssutils.css.CSSStyleDeclaration()
    312              n, v, p = test
    313              s.setProperty(n, v, p)
    314              self.assertEqual(exp, s.cssText)
    315              self.assertEqual(v, s.getPropertyValue(n))
    316              self.assertEqual(p, s.getPropertyPriority(n))
    317
    318 -    def test_nameParameter(self):
    319          "CSSStyleDeclaration.XXX(name)"
    320          s = cssutils.css.CSSStyleDeclaration()
    321          s.setProperty('top', '1px', '!important')
    322
    323          self.assertEqual('1px', s.getPropertyValue('top'))
    324          self.assertEqual('1px', s.getPropertyValue('TOP'))
    325          self.assertEqual('1px', s.getPropertyValue('T\op'))
    326
    327  ##        self.assertEqual('1px', s.getPropertyCSSValue('top'))
    328  ##        self.assertEqual('1px', s.getPropertyCSSValue('TOP'))
    329  ##        self.assertEqual('1px', s.getPropertyCSSValue('T\op'))
    330
    331          self.assertEqual('!important', s.getPropertyPriority('top'))
    332          self.assertEqual('!important', s.getPropertyPriority('TOP'))
    333          self.assertEqual('!important', s.getPropertyPriority('T\op'))
    334
    335          s.setProperty('top', '2px', '!important')
    336          self.assertEqual('2px', s.removeProperty('top'))
    337          s.setProperty('top', '2px', '!important')
    338          self.assertEqual('2px', s.removeProperty('TOP'))
    339          s.setProperty('top', '2px', '!important')
    340          self.assertEqual('2px', s.removeProperty('T\op'))
    341
    342 -    def test_css2properties(self):
    343          "CSSStyleDeclaration.$css2property get set del"
    344          s = cssutils.css.CSSStyleDeclaration(
    345              cssText='left: 1px;color: red; font-style: italic')
    346
    347          s.color = 'green'
    348          s.fontStyle = 'normal'
    349          self.assertEqual('green', s.color)
    350          self.assertEqual('normal', s.fontStyle)
    351          self.assertEqual('green', s.getPropertyValue('color'))
    352          self.assertEqual('normal', s.getPropertyValue('font-style'))
    353          self.assertEqual(
    354              u'''left: 1px;\ncolor: green;\nfont-style: normal''',
    355              s.cssText)
    356
    357          del s.color
    358          self.assertEqual(
    359              u'''left: 1px;\nfont-style: normal''',
    360              s.cssText)
    361          del s.fontStyle
    362          self.assertEqual(u'left: 1px', s.cssText)
    363
    364          self.assertRaises(AttributeError, s.__setattr__, 'UNKNOWN', 'red')
    365          # unknown properties must be set with setProperty!
    366          s.setProperty('UNKNOWN', 'red')
    367          # but are still not usable as property!
    368          self.assertRaises(AttributeError, s.__getattribute__, 'UNKNOWN')
    369          self.assertRaises(AttributeError, s.__delattr__, 'UNKNOWN')
    370          # but are kept
    371          self.assertEqual('red', s.getPropertyValue('UNKNOWN'))
    372          self.assertEqual(
    373              '''left: 1px;\nunknown: red''', s.cssText)
    374
    375 -    def test_reprANDstr(self):
    376          "CSSStyleDeclaration.__repr__(), .__str__()"
    377          s = cssutils.css.CSSStyleDeclaration(cssText='a:1;b:2')
    378
    379          self.assert_("2" in str(s)) # length
    380
    381          s2 = eval(repr(s))
    382          self.assert_(isinstance(s2, s.__class__))
    383
    384
    385 -class SameNamePropertyListTestCase(basetest.BaseTestCase):
    386
    387 -    def test_reprANDstr(self):
    388          "SameNamePropertyList.__repr__(), .__str__()"
    389          name='test'
    390
    391          s = cssutils.css.SameNamePropertyList(name=name)
    392
    393          self.assert_(name in str(s))
    394
    395          s2 = eval(repr(s))
    396          self.assert_(isinstance(s2, s.__class__))
    397          self.assert_(name == s2.name)
    398
    399
    400  if __name__ == '__main__':
    401      import unittest
    402      unittest.main()
    403