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-08-20 21:57:18 +0200 (Mo, 20 Aug 2007) $'
     20  __version__ = '$LastChangedRevision: 254 $'
     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          tokens = self._tokenize(newSelector)
     67          newS = Selector(tokens)
     68          if not newS.selectorText:
     69              self._log.error(u'SelectorList: Not a valid selector: "%s".' %
     70                        self._valuestr(newSelector), error=xml.dom.SyntaxErr)
     71              return
     72          else:
     73              if len(self.seq) > 0:
     74                  self.seq.append(u',')
     75              self.seq.append(newS)
     76              self.append(newS)
     77
     78          return newS
     79
     80 -    def _getLength(self):
     81          return len(self)
     82
     83      length = property(_getLength,
     84          doc="The number of Selector elements in the list.")
     85
     86 -    def _getSelectorText(self):
     87          """ returns serialized format """
     88          return cssutils.ser.do_css_SelectorList(self)
     89
     90 -    def _setSelectorText(self, selectorText):
     91          """
     92          DOMException on setting
     93
     94          - SYNTAX_ERR: (self)
     95            Raised if the specified CSS string value has a syntax error
     96            and is unparsable.
     97          - NO_MODIFICATION_ALLOWED_ERR: (self)
     98            Raised if this rule is readonly.
     99          """
    100          self._checkReadonly()
    101          tokens = self._tokenize(selectorText)
    102          if tokens:
    103              oldseq, self.seq = self.seq, [] # save and empty
    104              self.seq = []
    105              selectorparts = []
    106              found = None
    107              for i in range(len(tokens)):
    108                  t = tokens[i]
    109                  if self._ttypes.COMMA == t.type: # add new selector
    110                      found = 'comma'
    111                      try:
    112                          done = self.appendSelector(selectorparts)
    113                      except xml.dom.SyntaxErr, e:
    114                          self.seq = oldseq
    115                          self._log.error(e)
    116                          return
    117                      selectorparts = []
    118                  else:
    119                      found = 'selectorpart'
    120                      selectorparts.append(t)
    121
    122              if found == 'comma':
    123                  self._log.error(u'SelectorList: Selectorlist ends with ",".')
    124                  self.seq = oldseq
    125                  return
    126              elif selectorparts: # add new selector
    127                  try:
    128                      done = self.appendSelector(selectorparts)
    129                  except xml.dom.SyntaxErr, e:
    130                      self.seq = oldseq
    131                      self._log.error(e)
    132                      return
    133
    134          else:
    135              self._log.error(u'SelectorList: No selectors found.')
    136
    137      selectorText = property(_getSelectorText, _setSelectorText,
    138          doc="""(cssutils) The textual representation of the selector for
    139              a rule set.""")
    140
    141
    142  if __name__ == '__main__':
    143      cssutils.css.cssstylerule.Selector = Selector # for main test
    144      L = SelectorList()
    145      L.selectorText = 'a'
    146      print 1, L.selectorText
    147      try:
    148          L.selectorText = ','
    149      except Exception, e:
    150          print e
    151      print 2, L.selectorText
    152