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

# Source Code for Module cssutils.css.selectorlist

      1  """SelectorList is a list of CSS Selector objects.
      2
      3  TODO
      4      - ??? CSS2 gives a special meaning to the comma (,) in selectors.
      5          However, since it is not known if the comma may acquire other
      6          meanings in future versions of CSS, the whole statement should be
      7          ignored if there is an error anywhere in the selector, even though
      8          the rest of the selector may look reasonable in CSS2.
      9
     10          Illegal example(s):
     11
     12          For example, since the "&" is not a valid token in a CSS2 selector,
     13          a CSS2 user agent must ignore the whole second line, and not set
     14          the color of H3 to red:
     15  """
     16  __all__ = ['SelectorList']
     17  __docformat__ = 'restructuredtext'
     18  __author__ = '$LastChangedBy: cthedot $'
     19  __date__ = '$LastChangedDate: 2007-12-25 21:53:11 +0100 (Di, 25 Dez 2007) $'
     20  __version__ = '$LastChangedRevision: 723 $'
     21
     22  import xml.dom
     23  import cssutils
     24  from selector import Selector
     25
     26 -class SelectorList(cssutils.util.Base, cssutils.util.ListSeq):
     27      """
     28      (cssutils) a list of Selectors of a CSSStyleRule
     29
     30      Properties
     31      ==========
     32      length: of type unsigned long, readonly
     33          The number of Selector elements in the list.
     34      selectorText: of type DOMString
     35          The textual representation of the selector for the rule set. The
     36          implementation may have stripped out insignificant whitespace while
     37          parsing the selector.
     38      seq:
     39          A list of interwoven Selector objects and u','
     40      wellformed
     41          if this selectorlist is wellformed regarding the Selector spec
     42      """
     43 -    def __init__(self, selectorText=None, readonly=False):
     44          """
     45          initializes SelectorList with optional selectorText
     46          """
     47          super(SelectorList, self).__init__()
     48
     49          self.wellformed = False
     50          if selectorText:
     51              self.selectorText = selectorText
     52          self._readonly = readonly
     53
     54
     55 -    def __prepareset(self, newSelector):
     56          # used by appendSelector and __setitem__
     57          self._checkReadonly()
     58
     59          if not isinstance(newSelector, Selector):
     60              newSelector = Selector(newSelector)
     61
     62          if newSelector.wellformed:
     63              return newSelector
     64
     65 -    def __setitem__(self, index, newSelector):
     66          """
     67          overwrites ListSeq.__setitem__
     68
     69          Any duplicate Selectors are **not** removed.
     70          """
     71          newSelector = self.__prepareset(newSelector)
     72          if newSelector:
     73              self.seq[index] = newSelector
     74          # TODO: remove duplicates?
     75
     76 -    def appendSelector(self, newSelector):
     77          """
     78          Append newSelector (is a string will be converted to a new
     79          Selector. A duplicate Selector is removed.
     80
     81          Returns new Selector or None if newSelector is no wellformed
     82          Selector.
     83
     84          DOMException on setting
     85
     86          - SYNTAX_ERR: (self)
     87            Raised if the specified CSS string value has a syntax error
     88            and is unparsable.
     89          - NO_MODIFICATION_ALLOWED_ERR: (self)
     90            Raised if this rule is readonly.
     91          """
     92          newSelector = self.__prepareset(newSelector)
     93          if newSelector:
     94              self.wellformed = True
     95              seq = self.seq[:]
     96              del self.seq[:]
     97              for s in seq:
     98                  if s.selectorText != newSelector.selectorText:
     99                      self.seq.append(s)
    100              self.seq.append(newSelector)
    101              return newSelector
    102
    103 -    def append(self, newSelector):
    104          "overwrites ListSeq.append"
    105          self.appendSelector(newSelector)
    106
    107 -    def _getLength(self):
    108          return len(self)
    109
    110      length = property(_getLength,
    111          doc="The number of Selector elements in the list.")
    112
    113 -    def _getSelectorText(self):
    114          """ returns serialized format """
    115          return cssutils.ser.do_css_SelectorList(self)
    116
    117 -    def _setSelectorText(self, selectorText):
    118          """
    119          selectortext
    120              comma-separated list of selectors
    121
    122          DOMException on setting
    123
    124          - SYNTAX_ERR: (self)
    125            Raised if the specified CSS string value has a syntax error
    126            and is unparsable.
    127          - NO_MODIFICATION_ALLOWED_ERR: (self)
    128            Raised if this rule is readonly.
    129          """
    130          self._checkReadonly()
    131          wellformed = True
    132          tokenizer = self._tokenize2(selectorText)
    133          newseq = []
    134
    135          expected = True
    136          while True:
    137              # find all upto and including next ",", EOF or nothing
    138              selectortokens = self._tokensupto2(tokenizer, listseponly=True)
    139              if selectortokens:
    140                  if self._tokenvalue(selectortokens[-1]) == ',':
    141                      expected = selectortokens.pop()
    142                  else:
    143                      expected = None
    144
    145                  selector = Selector(selectortokens)
    146                  if selector.wellformed:
    147                      newseq.append(selector)
    148                  else:
    149                      wellformed = False
    150                      self._log.error(u'SelectorList: Invalid Selector: %s' %
    151                                      self._valuestr(selectortokens))
    152              else:
    153                  break
    154
    155          # post condition
    156          if u',' == expected:
    157              wellformed = False
    158              self._log.error(u'SelectorList: Cannot end with ",": %r' %
    159                              self._valuestr(selectorText))
    160          elif expected:
    161              wellformed = False
    162              self._log.error(u'SelectorList: Unknown Syntax: %r' %
    163                              self._valuestr(selectorText))
    164          if wellformed:
    165              self.wellformed = wellformed
    166              self.seq = newseq
    167              #for selector in newseq:
    168              #    self.appendSelector(selector)
    169
    170      selectorText = property(_getSelectorText, _setSelectorText,
    171          doc="""(cssutils) The textual representation of the selector for
    172              a rule set.""")
    173
    174 -    def __repr__(self):
    175          return "cssutils.css.%s(selectorText=%r)" % (
    176                  self.__class__.__name__, self.selectorText)
    177
    178 -    def __str__(self):
    179          return "<cssutils.css.%s object selectorText=%r at 0x%x>" % (
    180                  self.__class__.__name__, self.selectorText, id(self))
    181