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-24 23:34:27 +0100 (Sa, 24 Nov 2007) $'
      4  __version__ = '$LastChangedRevision: 683 $'
      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          # case insensitive
    298          s.setProperty('TOP', '0', '!IMPORTANT')
    299          self.assertEqual('0', s.getPropertyValue('top'))
    300          self.assertEqual('!important', s.getPropertyPriority('top'))
    301
    302          tests = {
    303              (u'left', u'0px', u''): u'left: 0px',
    304              (u'left', u'0px', u'!important'): u'left: 0px !important',
    305              (u'LEFT', u'0px', u'!important'): u'left: 0px !important',
    306              (u'left', u'0px', u'!important'): u'left: 0px !important',
    307              }
    308          for test, exp in tests.items():
    309              s = cssutils.css.CSSStyleDeclaration()
    310              n, v, p = test
    311              s.setProperty(n, v, p)
    312              self.assertEqual(exp, s.cssText)
    313              self.assertEqual(v, s.getPropertyValue(n))
    314              self.assertEqual(p, s.getPropertyPriority(n))
    315
    316 -    def test_item(self):
    317          "CSSStyleDeclaration.item()"
    318          _props = ('left', 'top', 'right')
    319          s = cssutils.css.CSSStyleDeclaration(cssText=
    320              '\left:0;TOP:1;right:3')
    321          for i in range(0,3):
    322              self.assertEqual(_props[i], s.item(i))
    323              self.assertEqual(_props[-1-i], s.item(-1-i))
    324          self.assertEqual(u'', s.item(3))
    325          self.assertEqual(u'', s.item(-4))
    326
    327 -    def test_length(self):
    328          "CSSStyleDeclaration.length"
    329          s = cssutils.css.CSSStyleDeclaration()
    330
    331          # cssText
    332          s.cssText = u'left: 0'
    333          self.assertEqual(1, s.length)
    334          self.assertEqual(1, len(s.seq))
    335          s.cssText = u'/*1*/left/*x*/:/*x*/0/*x*/;/*2*/ top: 1;/*3*/'
    336          self.assertEqual(2, s.length)
    337          self.assertEqual(5, len(s.seq))
    338
    339          # set
    340          s = cssutils.css.CSSStyleDeclaration()
    341          s.setProperty('top', '0', '!important')
    342          self.assertEqual(1, s.length)
    343          s.setProperty('top', '1px')
    344          self.assertEqual(1, s.length)
    345          s.setProperty('left', '1px')
    346
    347 -    def test_nameParameter(self):
    348          "CSSStyleDeclaration.XXX(name)"
    349          s = cssutils.css.CSSStyleDeclaration()
    350          s.setProperty('top', '1px', '!important')
    351
    352          self.assertEqual('1px', s.getPropertyValue('top'))
    353          self.assertEqual('1px', s.getPropertyValue('TOP'))
    354          self.assertEqual('1px', s.getPropertyValue('T\op'))
    355
    356          self.assertEqual('!important', s.getPropertyPriority('top'))
    357          self.assertEqual('!important', s.getPropertyPriority('TOP'))
    358          self.assertEqual('!important', s.getPropertyPriority('T\op'))
    359
    360          s.setProperty('top', '2px', '!important')
    361          self.assertEqual('2px', s.removeProperty('top'))
    362          s.setProperty('top', '2px', '!important')
    363          self.assertEqual('2px', s.removeProperty('TOP'))
    364          s.setProperty('top', '2px', '!important')
    365          self.assertEqual('2px', s.removeProperty('T\op'))
    366
    367 -    def test_css2properties(self):
    368          "CSSStyleDeclaration.$css2property get set del"
    369          s = cssutils.css.CSSStyleDeclaration(
    370              cssText='left: 1px;color: red; font-style: italic')
    371
    372          s.color = 'green'
    373          s.fontStyle = 'normal'
    374          self.assertEqual('green', s.color)
    375          self.assertEqual('normal', s.fontStyle)
    376          self.assertEqual('green', s.getPropertyValue('color'))
    377          self.assertEqual('normal', s.getPropertyValue('font-style'))
    378          self.assertEqual(
    379              u'''left: 1px;\ncolor: green;\nfont-style: normal''',
    380              s.cssText)
    381
    382          del s.color
    383          self.assertEqual(
    384              u'''left: 1px;\nfont-style: normal''',
    385              s.cssText)
    386          del s.fontStyle
    387          self.assertEqual(u'left: 1px', s.cssText)
    388
    389          self.assertRaises(AttributeError, s.__setattr__, 'UNKNOWN', 'red')
    390          # unknown properties must be set with setProperty!
    391          s.setProperty('UNKNOWN', 'red')
    392          # but are still not usable as property!
    393          self.assertRaises(AttributeError, s.__getattribute__, 'UNKNOWN')
    394          self.assertRaises(AttributeError, s.__delattr__, 'UNKNOWN')
    395          # but are kept
    396          self.assertEqual('red', s.getPropertyValue('UNKNOWN'))
    397          self.assertEqual(
    398              '''left: 1px;\nunknown: red''', s.cssText)
    399
    400 -    def test_reprANDstr(self):
    401          "CSSStyleDeclaration.__repr__(), .__str__()"
    402          s = cssutils.css.CSSStyleDeclaration(cssText='a:1;b:2')
    403
    404          self.assert_("2" in str(s)) # length
    405
    406          s2 = eval(repr(s))
    407          self.assert_(isinstance(s2, s.__class__))
    408
    409
    410  if __name__ == '__main__':
    411      import unittest
    412      unittest.main()
    413