Package cssutils :: Package css :: Module cssunknownrule |
[hide private]
[frames] | no frames]

# Source Code for Module cssutils.css.cssunknownrule

      1  """CSSUnknownRule implements DOM Level 2 CSS CSSUnknownRule.
      2  """
      3  __all__ = ['CSSUnknownRule']
      4  __docformat__ = 'restructuredtext'
      5  __author__ = '$LastChangedBy: cthedot $'
      6  __date__ = '$LastChangedDate: 2008-02-19 22:56:27 +0100 (Di, 19 Feb 2008) $'
      7  __version__ = '$LastChangedRevision: 1068 $'
      8
      9  import xml.dom
     10  import cssrule
     11  import cssutils
     12
     13 -class CSSUnknownRule(cssrule.CSSRule):
     14      """
     15      represents an at-rule not supported by this user agent.
     16
     17      Properties
     18      ==========
     19      inherited from CSSRule
     20          - cssText
     21          - type
     22
     23      cssutils only
     24      -------------
     25      atkeyword
     26          the literal keyword used
     27      seq
     28          All parts of this rule excluding @KEYWORD but including CSSComments
     29      wellformed
     30          if this Rule is wellformed, for Unknown rules if an atkeyword is set
     31          at all
     32
     33      Format
     34      ======
     35      unknownrule:
     36          @xxx until ';' or block {...}
     37      """
     38      type = cssrule.CSSRule.UNKNOWN_RULE
     39
     40 -    def __init__(self, cssText=u'', parentRule=None,
     41                   parentStyleSheet=None, readonly=False):
     42          """
     43          cssText
     44              of type string
     45          """
     46          super(CSSUnknownRule, self).__init__(parentRule=parentRule,
     47                                               parentStyleSheet=parentStyleSheet)
     48          if cssText:
     49              self.cssText = cssText
     50          else:
     51              self.atkeyword = None
     52
     53          self._readonly = readonly
     54
     55 -    def _getCssText(self):
     56          """ returns serialized property cssText """
     57          return cssutils.ser.do_CSSUnknownRule(self)
     58
     59 -    def _setCssText(self, cssText):
     60          """
     61          DOMException on setting
     62
     63          - SYNTAX_ERR:
     64            Raised if the specified CSS string value has a syntax error and
     65            is unparsable.
     66          - INVALID_MODIFICATION_ERR:
     67            Raised if the specified CSS string value represents a different
     68            type of rule than the current one.
     69          - HIERARCHY_REQUEST_ERR: (never raised)
     70            Raised if the rule cannot be inserted at this point in the
     71            style sheet.
     72          - NO_MODIFICATION_ALLOWED_ERR: (CSSRule)
     73            Raised if the rule is readonly.
     74          """
     75          super(CSSUnknownRule, self)._setCssText(cssText)
     76          tokenizer = self._tokenize2(cssText)
     77          attoken = self._nexttoken(tokenizer, None)
     78          if not attoken or self._type(attoken) != self._prods.ATKEYWORD:
     79              self._log.error(u'CSSUnknownRule: No CSSUnknownRule found: %s' %
     80                  self._valuestr(cssText),
     81                  error=xml.dom.InvalidModificationErr)
     82          else:
     83              # for closures: must be a mutable
     84              new = {'nesting': [], # {} [] or ()
     85                     'wellformed': True
     86                     }
     87
     88              def CHAR(expected, seq, token, tokenizer=None):
     89                  type_, val, line, col = token
     90                  if expected != 'EOF':
     91                      if val in u'{[(':
     92                          new['nesting'].append(val)
     93                      elif val in u'}])':
     94                          opening = {u'}': u'{', u']': u'[', u')': u'('}[val]
     95                          try:
     96                              if new['nesting'][-1] == opening:
     97                                  new['nesting'].pop()
     98                              else:
     99                                  raise IndexError()
    100                          except IndexError:
    101                              new['wellformed'] = False
    102                              self._log.error(u'CSSUnknownRule: Wrong nesting of {, [ or (.',
    103                                  token=token)
    104
    105                      if val in u'};' and not new['nesting']:
    106                          expected = 'EOF'
    107
    108                      seq.append(val, type_, line=line, col=col)
    109                      return expected
    110                  else:
    111                      new['wellformed'] = False
    112                      self._log.error(u'CSSUnknownRule: Expected end of rule.',
    113                          token=token)
    114                      return expected
    115
    116              def EOF(expected, seq, token, tokenizer=None):
    117                  "close all blocks and return 'EOF'"
    118                  for x in reversed(new['nesting']):
    119                      closing = {u'{': u'}', u'[': u']', u'(': u')'}[x]
    120                      seq.append(closing, closing)
    121                  new['nesting'] = []
    122                  return 'EOF'
    123
    124              def INVALID(expected, seq, token, tokenizer=None):
    125                  # makes rule invalid
    126                  self._log.error(u'CSSUnknownRule: Bad syntax.',
    127                              token=token, error=xml.dom.SyntaxErr)
    128                  new['wellformed'] = False
    129                  return expected
    130
    131              def STRING(expected, seq, token, tokenizer=None):
    132                  type_, val, line, col = token
    133                  val = self._stringtokenvalue(token)
    134                  if expected != 'EOF':
    135                      seq.append(val, type_, line=line, col=col)
    136                      return expected
    137                  else:
    138                      new['wellformed'] = False
    139                      self._log.error(u'CSSUnknownRule: Expected end of rule.',
    140                          token=token)
    141                      return expected
    142
    143              def URI(expected, seq, token, tokenizer=None):
    144                  type_, val, line, col = token
    145                  val = self._uritokenvalue(token)
    146                  if expected != 'EOF':
    147                      seq.append(val, type_, line=line, col=col)
    148                      return expected
    149                  else:
    150                      new['wellformed'] = False
    151                      self._log.error(u'CSSUnknownRule: Expected end of rule.',
    152                          token=token)
    153                      return expected
    154
    155              def default(expected, seq, token, tokenizer=None):
    156                  type_, val, line, col = token
    157                  if expected != 'EOF':
    158                      seq.append(val, type_, line=line, col=col)
    159                      return expected
    160                  else:
    161                      new['wellformed'] = False
    162                      self._log.error(u'CSSUnknownRule: Expected end of rule.',
    163                          token=token)
    164                      return expected
    165
    166              # unknown : ATKEYWORD S* ... ; | }
    167              newseq = self._tempSeq()
    168              wellformed, expected = self._parse(expected=None,
    169                  seq=newseq, tokenizer=tokenizer,
    170                  productions={'CHAR': CHAR,
    171                               'EOF': EOF,
    172                               'INVALID': INVALID,
    173                               'STRING': STRING,
    174                               'URI': URI,
    175                               'S': default # overwrite default default!
    176                              },
    177                              default=default)
    178
    179              # wellformed set by parse
    180              wellformed = wellformed and new['wellformed']
    181
    182              # post conditions
    183              if expected != 'EOF':
    184                  wellformed = False
    185                  self._log.error(
    186                      u'CSSUnknownRule: No ending ";" or "}" found: %r' %
    187                      self._valuestr(cssText))
    188              elif new['nesting']:
    189                  wellformed = False
    190                  self._log.error(
    191                      u'CSSUnknownRule: Unclosed "{", "[" or "(": %r' %
    192                      self._valuestr(cssText))
    193
    194              # set all
    195              if wellformed:
    196                  self.atkeyword = self._tokenvalue(attoken)
    197                  self._setSeq(newseq)
    198
    199      cssText = property(fget=_getCssText, fset=_setCssText,
    200          doc="(DOM) The parsable textual representation.")
    201
    202      wellformed = property(lambda self: bool(self.atkeyword))
    203
    204 -    def __repr__(self):
    205          return "cssutils.css.%s(cssText=%r)" % (
    206                  self.__class__.__name__, self.cssText)
    207
    208 -    def __str__(self):
    209          return "<cssutils.css.%s object cssText=%r at 0x%x>" % (
    210                  self.__class__.__name__, self.cssText, id(self))
    211