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

# Source Code for Module cssutils.css.property

      1  """Property is a single CSS property in a CSSStyleDeclaration
      2
      3  Internal use only, may be removed in the future!
      4  """
      5  __all__ = []
      6  __docformat__ = 'restructuredtext'
      7  __author__ = '$LastChangedBy: cthedot $'
      8  __date__ = '$LastChangedDate: 2007-08-29 13:51:35 +0200 (Mi, 29 Aug 2007) $'
      9  __version__ = '$LastChangedRevision: 297 $'
     10
     11  import xml.dom
     12  import cssutils
     13  import cssproperties
     14  from cssvalue import CSSValue
     15
     16 -class _Property(cssutils.util.Base):
     17      """
     18      (cssutils) a CSS property in a StyleDeclaration of a CSSStyleRule
     19
     20      Properties
     21      ==========
     22      name
     23          of the property
     24      normalname
     25          normalized name of the property, e.g. "color" when name is "c\olor"
     26      cssValue
     27          the relevant CSSValue instance for this property
     28      priority
     29          of the property (currently only "!important" or None)
     30      seqs
     31          combination of a list for seq of name, a CSSValue object, and
     32          a list for seq of  priority (empty or [!important] currently)
     33      valid
     34          if this Property is valid
     35
     36      DEPRECATED: value
     37          the string value of the property, use cssValue.cssText instead!
     38
     39      Format
     40      ======
     41      ::
     42
     43          property = name
     44            : IDENT S*
     45            ;
     46
     47          expr = value
     48            : term [ operator term ]*
     49            ;
     50          term
     51            : unary_operator?
     52              [ NUMBER S* | PERCENTAGE S* | LENGTH S* | EMS S* | EXS S* | ANGLE S* |
     53                TIME S* | FREQ S* | function ]
     54            | STRING S* | IDENT S* | URI S* | hexcolor
     55            ;
     56          function
     57            : FUNCTION S* expr ')' S*
     58            ;
     59          /*
     60           * There is a constraint on the color that it must
     61           * have either 3 or 6 hex-digits (i.e., [0-9a-fA-F])
     62           * after the "#"; e.g., "#000" is OK, but "#abcd" is not.
     63           */
     64          hexcolor
     65            : HASH S*
     66            ;
     67
     68          prio
     69            : IMPORTANT_SYM S*
     70            ;
     71
     72      """
     73 -    def __init__(self, name, value, priority=None):
     74          """
     75          inits property
     76          """
     77          super(_Property, self).__init__()
     78
     79          self.seqs = [[], None, []]
     80          self.valid = True
     81          self.name = name
     82          self.cssValue = value
     83          self.priority = priority
     84
     85 -    def __invalidToken(self, tokens, x):
     86          """
     87          raises SyntaxErr if an INVALID token in tokens
     88
     89          x
     90              name, value or priority, used for error message
     91
     92          returns True if INVALID found, else False
     93          """
     94          for t in tokens:
     95              if t.type == self._ttypes.INVALID:
     96                  self._log.error(u'Property: Invalid token found in %s.' % x, t)
     97                  return True
     98          return False
     99
    100 -    def _getName(self):
    101          try:
    102              return self._name
    103          except AttributeError:
    104              return u''
    105
    106 -    def _setName(self, name):
    107          """
    108          Format
    109          ======
    110          property = name
    111            : IDENT S*
    112            ;
    113
    114          DOMException on setting
    115
    116          - SYNTAX_ERR: (self)
    117            Raised if the specified name has a syntax error and is
    118            unparsable.
    119          """
    120          tokens = self._tokenize(name)
    121          if self.__invalidToken(tokens, 'name'):
    122              return
    123          newname = newnormalname = None
    124          newseq = []
    125          t = None # used later
    126          for i in range(0, len(tokens)):
    127              t = tokens[i]
    128              if self._ttypes.S == t.type: # ignore
    129                  pass
    130
    131              elif self._ttypes.COMMENT == t.type: # just add
    132                  newseq.append(cssutils.css.CSSComment(t))
    133
    134              elif self._ttypes.IDENT == t.type and not newname:
    135                  newname = t.value.lower()
    136                  newnormalname = t.normalvalue
    137                  newseq.append(newname)
    138
    139              else:
    140                  self._log.error(u'Property: Syntax error in name.', t)
    141                  return
    142
    143          if newname:
    144              self._name = newname
    145              self.normalname = newnormalname
    146              self.seqs[0] = newseq
    147
    148              # validate
    149              if newname not in cssproperties.cssvalues:
    150                  self._log.info(u'Property: No CSS2 Property: "%s".' %
    151                           newname, t, neverraise=True)
    152
    153          else:
    154              self._log.error(u'Property: No name found: "%s".' % name, t)
    155
    156      name = property(_getName, _setName,
    157          doc="(cssutils) Name of this property")
    158
    159 -    def _getCSSValue(self):
    160          return self.seqs[1]
    161
    162 -    def _setCSSValue(self, cssText):
    163          """
    164          see css.CSSValue
    165
    166          DOMException on setting?
    167
    168          - SYNTAX_ERR: (self)
    169            Raised if the specified CSS string value has a syntax error
    170            (according to the attached property) or is unparsable.
    171          - TODO: INVALID_MODIFICATION_ERR:
    172            Raised if the specified CSS string value represents a different
    173            type of values than the values allowed by the CSS property.
    174          """
    175          cssvalue = CSSValue(cssText=cssText, _propertyName=self.name)
    176          if cssvalue._value:
    177              self.seqs[1] = cssvalue
    178
    179      cssValue = property(_getCSSValue, _setCSSValue,
    180          doc="(cssutils) CSSValue object of this property")
    181
    182 -    def _getPriority(self):
    183          try:
    184              return self._priority
    185          except AttributeError:
    186              return u''
    187
    188 -    def _setPriority(self, priority):
    189          """
    190          priority
    191              currently "!important" to set an important priority
    192              or None or the empty string to set no priority only
    193
    194          Format
    195          ======
    196          ::
    197
    198              prio
    199                : IMPORTANT_SYM S*
    200                ;
    201              "!"{w}"important"   {return IMPORTANT_SYM;}
    202
    203          DOMException on setting
    204
    205          - SYNTAX_ERR: (self)
    206            Raised if the specified priority has a syntax error and is
    207            unparsable.
    208            In this case a priority not equal to None, "" or "!{w}important".
    209          """
    210          if priority is None or priority == u'':
    211              self._priority = u''
    212              self.seqs[2] = []
    213          else:
    214              tokens = self._tokenize(priority)
    215              if self.__invalidToken(tokens, 'priority'):
    216                  return
    217
    218              newpriority = None
    219              for t in tokens:
    220                  if t.type in (self._ttypes.S, self._ttypes.COMMENT): # ignored
    221                      pass
    222                  elif self._ttypes.IMPORTANT_SYM == t.type and not newpriority:
    223                      newpriority = t.value.lower()
    224                  else:
    225                      self._log.error(u'Property: Unknown priority.', t)
    226                      return
    227
    228              if newpriority:
    229                  self._priority = newpriority
    230                  self.seqs[2] = ['!important']
    231              else:
    232                  self._log.error(u'Property: Unknown priority: "%s".' % priority)
    233
    234      priority = property(_getPriority, _setPriority,
    235          doc="(cssutils) Priority of this property")
    236
    237 -    def __repr__(self):
    238          return "cssutils.css.property.%s(name=%r, value=%r, priority=%r)" % (
    239                  self.__class__.__name__,
    240                  self.name, self.cssValue.cssText, self.priority)
    241
    242 -    def __str__(self):
    243          return "<%s.%s object name=%r value=%r priority=%r at 0x%x>" % (
    244                  self.__class__.__module__, self.__class__.__name__,
    245                  self.name, self.cssValue.cssText, self.priority, id(self))
    246
    247      # DEPRECATED
    248 -    def _getValue(self):
    249          import warnings
    250          warnings.warn(
    251              'value is deprecated, use cssValue instead.',
    252              DeprecationWarning)
    253          if self.cssValue: return self.cssValue._value
    254          else: return u''
    255 -    def _setValue(self, value):
    256          import warnings
    257          warnings.warn(
    258              'value is deprecated, use cssValue instead.',
    259              DeprecationWarning)
    260          self.cssValue.cssText = value
    261      value = property(_getValue, _setValue,
    262                       doc="DEPRECATED use cssValue instead")
    263
    264
    265
    266  if __name__ == '__main__':
    267      p = _Property(u'color', 'red', '! important')
    268      print p.name, p.cssValue, p.priority
    269