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