Home | Trees | Indices | Help |
|
---|
|
1 """ 2 MediaList implements DOM Level 2 Style Sheets MediaList. 3 4 TODO: 5 delete: maybe if deleting from all, replace *all* with all others? 6 is unknown media an exception? 7 """ 8 __all__ = ['MediaList'] 9 __docformat__ = 'restructuredtext' 10 __author__ = '$LastChangedBy: doerwalter $' 11 __date__ = '$LastChangedDate: 2007-08-02 22:58:23 +0200 (Do, 02 Aug 2007) $' 12 __version__ = '0.9.2a2 $LastChangedRevision: 160 $' 13 14 import xml.dom 15 16 from cssutils.css import csscomment 17 import cssutils 18 1921 """ 22 Provides the abstraction of an ordered collection of media, 23 without defining or constraining how this collection is 24 implemented. 25 An empty list is the same as a list that contains the medium "all". 26 27 Properties 28 ========== 29 length: 30 The number of media in the list. 31 mediaText: of type DOMString 32 The parsable textual representation of this medialist 33 seq: a list (cssutils) 34 All parts of this MediaList including CSSComments 35 valid: 36 if this list is valid 37 38 Format 39 ====== 40 medium [ COMMA S* medium ]* 41 """ 42 43 _MEDIA = [u'all', u'aural', u'braille', u'embossed', u'handheld', 44 u'print', u'projection', u'screen', u'tty', u'tv'] 45 "available media types" 46263 264 265 if __name__ == '__main__': 266 m = MediaList() 267 m.mediaText = u'all; @x' 268 print m.mediaText 26948 """ 49 mediaText 50 unicodestring of parsable comma separared media 51 """ 52 super(MediaList, self).__init__() 53 54 self.valid = True 55 56 if mediaText: 57 self._seq = [] 58 self.mediaText = mediaText 59 else: 60 self.seq = [] 61 self._readonly = readonly62 6365 """ 66 returns count of media in this list which is not the same as 67 len(MediaListInstance) which also contains CSSComments 68 """ 69 return len(self)70 71 length = property(_getLength, 72 doc="(DOM readonly) The number of media in the list.") 73 74 7779 self._seq = seq80 81 seq = property(_getSeq, _setSeq, 82 doc="All parts of this MediaList including CSSComments") 83 8486 """ 87 returns serialized property mediaText 88 """ 89 return cssutils.ser.do_stylesheets_medialist(self)9092 """ 93 mediaText 94 simple value or comma-separated list of media 95 96 DOMException 97 98 - SYNTAX_ERR: (self) 99 Raised if the specified string value has a syntax error and is 100 unparsable. 101 - NO_MODIFICATION_ALLOWED_ERR: (self) 102 Raised if this media list is readonly. 103 """ 104 self._checkReadonly() 105 tokens = self._tokenize(mediaText) 106 107 newseq = [] 108 del self[:] # reset 109 expected = 'medium1' 110 valid = True 111 for i in range(len(tokens)): 112 t = tokens[i] 113 if self._ttypes.S == t.type: # ignore 114 pass 115 116 elif self._ttypes.COMMENT == t.type: # just add 117 newseq.append(csscomment.CSSComment(t)) 118 119 elif expected.startswith('medium') and self._ttypes.IDENT == t.type: 120 _newmed = t.value.lower() 121 self.appendMedium(_newmed) 122 newseq.append(_newmed) 123 expected = 'comma' 124 125 elif self._ttypes.IDENT == t.type: 126 valid = False 127 self._log.error( 128 u'MediaList: Syntax Error, expected ",".', t) 129 130 elif 'comma' == expected and self._ttypes.COMMA == t.type: 131 newseq.append(t.value) 132 expected = 'medium' 133 134 elif self._ttypes.COMMA == t.type: 135 valid = False 136 self._log.error(u'MediaList: Syntax Error, expected ",".', t) 137 138 else: 139 self._log.error(u'MediaList: Syntax Error in "%s".' % 140 self._valuestr(tokens), t) 141 142 if 'medium' == expected: 143 valid = False 144 self._log.error( 145 u'MediaList: Syntax Error, cannot end with ",".') 146 self.seq = newseq 147 self.valid = valid148 149 mediaText = property(_getMediaText, _setMediaText, 150 doc="""(DOM) The parsable textual representation of the media list. 151 This is a comma-separated list of media.""") 152 153155 """ 156 (DOM) 157 Adds the medium newMedium to the end of the list. If the newMedium 158 is already used, it is first removed. 159 160 returns if newMedium is valid 161 162 DOMException 163 164 - INVALID_CHARACTER_ERR: (self) 165 If the medium contains characters that are invalid in the 166 underlying style language. 167 - NO_MODIFICATION_ALLOWED_ERR: (self) 168 Raised if this list is readonly. 169 """ 170 self._checkReadonly() 171 tokens = self._tokenize(newMedium) 172 173 valid = True 174 175 # ? should check format only? 176 try: 177 newMedium = tokens[0].value.lower() 178 except (IndexError, AttributeError): 179 self._log.error( 180 u'MediaList: "%s" is not a valid medium.' % self._valuestr( 181 newMedium), error=xml.dom.InvalidCharacterErr) 182 return 183 184 if newMedium not in self._MEDIA: 185 valid = False 186 self._log.error( 187 u'MediaList: "%s" is not a valid medium.' % newMedium, 188 tokens[0], xml.dom.InvalidCharacterErr) 189 190 # all contains every other (except handheld!) 191 if u'all' in self and newMedium != u'handheld': 192 return valid 193 if newMedium == u'all': 194 if u'handheld' in self: 195 addhandheld2seq = True 196 else: 197 addhandheld2seq = False 198 del self[:] 199 self.append(u'all') 200 self._seq = [u'all'] 201 if addhandheld2seq: 202 #self.append(u'handheld') 203 self._seq.append(u',') 204 self._seq.append(u'handheld') 205 else: 206 if newMedium in self: 207 self.remove(newMedium) 208 209 # remove medium and possible ,! 210 look4comma = False 211 newseq = [] 212 for x in self._seq: 213 if newMedium == x: 214 look4comma = True 215 continue # remove 216 if u',' == x and look4comma: 217 look4comma = False 218 continue 219 else: 220 newseq.append(x) 221 self._seq = newseq 222 223 if len(self) > 0: # already 1 there, add "," + medium 2 seq 224 self._seq.append(u',') 225 226 self._seq.append(newMedium) 227 self.append(newMedium) 228 return valid229 230232 """ 233 (DOM) 234 Deletes the medium indicated by oldMedium from the list. 235 236 DOMException 237 238 - NO_MODIFICATION_ALLOWED_ERR: (self) 239 Raised if this list is readonly. 240 - NOT_FOUND_ERR: (self) 241 Raised if oldMedium is not in the list. 242 """ 243 self._checkReadonly() 244 oldMedium = oldMedium.lower() 245 try: 246 self.remove(oldMedium) 247 self._seq.remove(oldMedium) 248 except ValueError: 249 raise xml.dom.NotFoundErr( 250 u'"%s" not in this MediaList' % oldMedium)251 252
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0beta1 on Sat Aug 04 12:58:33 2007 | http://epydoc.sourceforge.net |