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-10-16 21:41:43 +0200 (Di, 16 Okt 2007) $'
     20  __version__ = '$LastChangedRevision: 486 $'
     21
     22  import xml.dom
     23  import cssutils
     24  from selector import Selector
     25
     26 -class SelectorList(cssutils.util.Base, list):
     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          self.seq = []
     48          if selectorText:
     49              self.selectorText = selectorText
     50          self._readonly = readonly
     51
     52 -    def appendSelector(self, newSelector):
     53          """
     54          append a new Selector made from newSelector
     55          returns new Selector
     56
     57          DOMException on setting
     58
     59          - SYNTAX_ERR: (self)
     60            Raised if the specified CSS string value has a syntax error
     61            and is unparsable.
     62          - NO_MODIFICATION_ALLOWED_ERR: (self)
     63            Raised if this rule is readonly.
     64          """
     65          self._checkReadonly()
     66
     67          if not isinstance(newSelector, Selector):
     68              newSelector = Selector(newSelector)
     69
     70          if newSelector.valid:
     71              newseq = []
     72              for s in self.seq:
     73                  if s.selectorText != newSelector.selectorText:
     74                      newseq.append(s)
     75              newseq.append(newSelector)
     76              self.seq = newseq
     77              return True
     78          else:
     79              return False
     80
     81 -    def _getLength(self):
     82          return len(self.seq)
     83
     84      length = property(_getLength,
     85          doc="The number of Selector elements in the list.")
     86
     87 -    def _getSelectorText(self):
     88          """ returns serialized format """
     89          return cssutils.ser.do_css_SelectorList(self)
     90
     91
     92 -    def _setSelectorText(self, selectorText):
     93          """
     94          selectortext
     95              comma-separated list of selectors
     96
     97          DOMException on setting
     98
     99          - SYNTAX_ERR: (self)
    100            Raised if the specified CSS string value has a syntax error
    101            and is unparsable.
    102          - NO_MODIFICATION_ALLOWED_ERR: (self)
    103            Raised if this rule is readonly.
    104          """
    105          self._checkReadonly()
    106          valid = True
    107          tokenizer = self._tokenize2(selectorText)
    108          newseq = []
    109
    110          expected = True
    111          while True:
    112              # find all upto and including next ",", EOF or nothing
    113              selectortokens = self._tokensupto2(tokenizer, listseponly=True)
    114              if selectortokens:
    115                  if self._tokenvalue(selectortokens[-1]) == ',':
    116                      expected = selectortokens.pop()
    117                  else:
    118                      expected = None
    119
    120                  selector = Selector(selectortokens)
    121                  if selector.valid:
    122                      newseq.append(selector)
    123                  else:
    124                      valid = False
    125                      self._log.error(u'SelectorList: Invalid Selector: %s' %
    126                                      self._valuestr(selectortokens))
    127              else:
    128                  break
    129
    130          # post condition
    131          if u',' == expected:
    132              valid = False
    133              self._log.error(u'SelectorList: Cannot end with ",": %r' %
    134                              self._valuestr(selectorText))
    135          elif expected:
    136              valid = False
    137              self._log.error(u'SelectorList: Unknown Syntax: %r' %
    138                              self._valuestr(selectorText))
    139
    140          if valid:
    141              self.seq = newseq
    142              #for selector in newseq:
    143              #    self.appendSelector(selector)
    144
    145      selectorText = property(_getSelectorText, _setSelectorText,
    146          doc="""(cssutils) The textual representation of the selector for
    147              a rule set.""")
    148
    149 -    def __repr__(self):
    150          return "cssutils.css.%s(selectorText=%r)" % (
    151                  self.__class__.__name__, self.selectorText)
    152
    153 -    def __str__(self):
    154          return "<cssutils.css.%s object selectorText=%r at 0x%x>" % (
    155                  self.__class__.__name__, self.selectorText, id(self))
    156