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