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: doerwalter $' 
 19  __date__ = '$LastChangedDate: 2007-08-02 22:58:23 +0200 (Do, 02 Aug 2007) $' 
 20  __version__ = '0.9.2a1, $LastChangedRevision: 160 $' 
 21   
 22  import xml.dom 
 23   
 24  import cssutils 
 25  from selector import Selector 
 26   
 27   
28 -class SelectorList(cssutils.util.Base, list):
29 """ 30 (cssutils) a list of Selectors of a CSSStyleRule 31 32 Properties 33 ========== 34 length: of type unsigned long, readonly 35 The number of Selector elements in the list. 36 selectorText: of type DOMString 37 The textual representation of the selector for the rule set. The 38 implementation may have stripped out insignificant whitespace while 39 parsing the selector. 40 seq: 41 A list of interwoven Selector objects and u',' 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.seq = [] 50 if selectorText: 51 self.selectorText = selectorText 52 self._readonly = readonly
53 54
55 - def appendSelector(self, newSelector):
56 """ 57 append a new Selector made from newSelector 58 returns new Selector 59 60 DOMException on setting 61 62 - SYNTAX_ERR: (self) 63 Raised if the specified CSS string value has a syntax error 64 and is unparsable. 65 - NO_MODIFICATION_ALLOWED_ERR: (self) 66 Raised if this rule is readonly. 67 """ 68 self._checkReadonly() 69 tokens = self._tokenize(newSelector) 70 newS = Selector(tokens) 71 if not newS.selectorText: 72 self._log.error(u'SelectorList: Not a valid selector: "%s".' % 73 self._valuestr(newSelector), error=xml.dom.SyntaxErr) 74 return 75 else: 76 if len(self.seq) > 0: 77 self.seq.append(u',') 78 self.seq.append(newS) 79 self.append(newS) 80 81 return newS
82 83
84 - def _getLength(self):
85 return len(self)
86 87 length = property(_getLength, 88 doc="The number of Selector elements in the list.") 89 90
91 - def _getSelectorText(self):
92 """ returns serialized format """ 93 return cssutils.ser.do_css_SelectorList(self)
94
95 - def _setSelectorText(self, selectorText):
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 tokens = self._tokenize(selectorText) 107 if tokens: 108 oldseq, self.seq = self.seq, [] # save and empty 109 self.seq = [] 110 selectorparts = [] 111 found = None 112 for i in range(len(tokens)): 113 t = tokens[i] 114 if self._ttypes.COMMA == t.type: # add new selector 115 found = 'comma' 116 try: 117 done = self.appendSelector(selectorparts) 118 except xml.dom.SyntaxErr, e: 119 self.seq = oldseq 120 self._log.error(e) 121 return 122 selectorparts = [] 123 else: 124 found = 'selectorpart' 125 selectorparts.append(t) 126 127 if found == 'comma': 128 self._log.error(u'SelectorList: Selectorlist ends with ",".') 129 self.seq = oldseq 130 return 131 elif selectorparts: # add new selector 132 try: 133 done = self.appendSelector(selectorparts) 134 except xml.dom.SyntaxErr, e: 135 self.seq = oldseq 136 self._log.error(e) 137 return 138 139 else: 140 self._log.error(u'SelectorList: No selectors found.')
141 142 selectorText = property(_getSelectorText, _setSelectorText, 143 doc="""(cssutils) The textual representation of the selector for 144 a rule set.""")
145 146 147 if __name__ == '__main__': 148 cssutils.css.cssstylerule.Selector = Selector # for main test 149 L = SelectorList() 150 L.selectorText = 'a' 151 print 1, L.selectorText 152 try: 153 L.selectorText = ',' 154 except Exception, e: 155 print e 156 print 2, L.selectorText 157