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-11-25 18:03:41 +0100 (So, 25 Nov 2007) $'
     20  __version__ = '$LastChangedRevision: 691 $'
     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      """
     41 -    def __init__(self, selectorText=None, readonly=False):
     42          """
     43          initializes SelectorList with optional selectorText
     44          """
     45          super(SelectorList, self).__init__()
     46
     47          if selectorText:
     48              self.selectorText = selectorText
     49          self._readonly = readonly
     50
     51
     52 -    def __prepareset(self, newSelector):
     53          # used by appendSelector and __setitem__
     54          self._checkReadonly()
     55
     56          if not isinstance(newSelector, Selector):
     57              newSelector = Selector(newSelector)
     58
     59          if newSelector.valid:
     60              return newSelector
     61
     62 -    def __setitem__(self, index, newSelector):
     63          """
     64          overwrites ListSeq.__setitem__
     65
     66          Any duplicate Selectors are **not** removed.
     67          """
     68          newSelector = self.__prepareset(newSelector)
     69          if newSelector:
     70              self.seq[index] = newSelector
     71          # TODO: remove duplicates?
     72
     73 -    def appendSelector(self, newSelector):
     74          """
     75          Append newSelector (is a string will be converted to a new
     76          Selector. A duplicate Selector is removed.
     77
     78          Returns new Selector or None if newSelector is no valid
     79          Selector.
     80
     81          DOMException on setting
     82
     83          - SYNTAX_ERR: (self)
     84            Raised if the specified CSS string value has a syntax error
     85            and is unparsable.
     86          - NO_MODIFICATION_ALLOWED_ERR: (self)
     87            Raised if this rule is readonly.
     88          """
     89          newSelector = self.__prepareset(newSelector)
     90          if newSelector:
     91              seq = self.seq[:]
     92              del self.seq[:]
     93              for s in seq:
     94                  if s.selectorText != newSelector.selectorText:
     95                      self.seq.append(s)
     96              self.seq.append(newSelector)
     97              return newSelector
     98
     99 -    def append(self, newSelector):
    100          "overwrites ListSeq.append"
    101          self.appendSelector(newSelector)
    102
    103 -    def _getLength(self):
    104          return len(self)
    105
    106      length = property(_getLength,
    107          doc="The number of Selector elements in the list.")
    108
    109 -    def _getSelectorText(self):
    110          """ returns serialized format """
    111          return cssutils.ser.do_css_SelectorList(self)
    112
    113 -    def _setSelectorText(self, selectorText):
    114          """
    115          selectortext
    116              comma-separated list of selectors
    117
    118          DOMException on setting
    119
    120          - SYNTAX_ERR: (self)
    121            Raised if the specified CSS string value has a syntax error
    122            and is unparsable.
    123          - NO_MODIFICATION_ALLOWED_ERR: (self)
    124            Raised if this rule is readonly.
    125          """
    126          self._checkReadonly()
    127          valid = True
    128          tokenizer = self._tokenize2(selectorText)
    129          newseq = []
    130
    131          expected = True
    132          while True:
    133              # find all upto and including next ",", EOF or nothing
    134              selectortokens = self._tokensupto2(tokenizer, listseponly=True)
    135              if selectortokens:
    136                  if self._tokenvalue(selectortokens[-1]) == ',':
    137                      expected = selectortokens.pop()
    138                  else:
    139                      expected = None
    140
    141                  selector = Selector(selectortokens)
    142                  if selector.valid:
    143                      newseq.append(selector)
    144                  else:
    145                      valid = False
    146                      self._log.error(u'SelectorList: Invalid Selector: %s' %
    147                                      self._valuestr(selectortokens))
    148              else:
    149                  break
    150
    151          # post condition
    152          if u',' == expected:
    153              valid = False
    154              self._log.error(u'SelectorList: Cannot end with ",": %r' %
    155                              self._valuestr(selectorText))
    156          elif expected:
    157              valid = False
    158              self._log.error(u'SelectorList: Unknown Syntax: %r' %
    159                              self._valuestr(selectorText))
    160
    161          if valid:
    162              self.seq = newseq
    163              #for selector in newseq:
    164              #    self.appendSelector(selector)
    165
    166      selectorText = property(_getSelectorText, _setSelectorText,
    167          doc="""(cssutils) The textual representation of the selector for
    168              a rule set.""")
    169
    170 -    def __repr__(self):
    171          return "cssutils.css.%s(selectorText=%r)" % (
    172                  self.__class__.__name__, self.selectorText)
    173
    174 -    def __str__(self):
    175          return "<cssutils.css.%s object selectorText=%r at 0x%x>" % (
    176                  self.__class__.__name__, self.selectorText, id(self))
    177