def _getUsedNamespaces(self): "Return actually used namespaces only." useduris = self._getUsedUris() namespaces = _SimpleNamespaces(log=self._log) for p, uri in self._namespaces.items(): if uri in useduris: namespaces[p] = uri return namespaces
def _getUsedNamespaces(self): "Return actually used namespaces only." useduris = self._getUsedUris() namespaces = _SimpleNamespaces(log=self._log) for p, uri in list(self._namespaces.items()): if uri in useduris: namespaces[p] = uri return namespaces
def __init__(self, selectorText=None, parent=None, readonly=False): """ :Parameters: selectorText initial value of this selector parent a SelectorList readonly default to False """ super(Selector, self).__init__() self.__namespaces = _SimpleNamespaces(log=self._log) self._element = None self._parent = parent self._specificity = (0, 0, 0, 0) if selectorText: self.selectorText = selectorText self._readonly = readonly
def _setCssText(self, cssText): """Parse `cssText` and overwrites the whole stylesheet. :param cssText: a parseable string or a tuple of (cssText, dict-of-namespaces) :exceptions: - :exc:`~xml.dom.NamespaceErr`: If a namespace prefix is found which is not declared. - :exc:`~xml.dom.NoModificationAllowedErr`: Raised if the rule is readonly. - :exc:`~xml.dom.SyntaxErr`: Raised if the specified CSS string value has a syntax error and is unparsable. """ self._checkReadonly() cssText, namespaces = self._splitNamespacesOff(cssText) if not namespaces: namespaces = _SimpleNamespaces(log=self._log) tokenizer = self._tokenize2(cssText) newseq = [] #cssutils.css.CSSRuleList() # for closures: must be a mutable new = {'encoding': None, # needed for setting encoding of @import rules 'namespaces': namespaces} def S(expected, seq, token, tokenizer=None): # @charset must be at absolute beginning of style sheet if expected == 0: return 1 else: return expected def COMMENT(expected, seq, token, tokenizer=None): "special: sets parent*" comment = cssutils.css.CSSComment([token], parentStyleSheet=self.parentStyleSheet) seq.append(comment) return expected def charsetrule(expected, seq, token, tokenizer): rule = cssutils.css.CSSCharsetRule(parentStyleSheet=self) rule.cssText = self._tokensupto2(tokenizer, token) if expected > 0 or len(seq) > 0: self._log.error( u'CSSStylesheet: CSSCharsetRule only allowed at beginning of stylesheet.', token, xml.dom.HierarchyRequestErr) else: if rule.wellformed: seq.append(rule) new['encoding'] = rule.encoding return 1 def importrule(expected, seq, token, tokenizer): if new['encoding']: # set temporarily as used by _resolveImport # save newEncoding which have been set by resolveImport self.__newEncoding = new['encoding'] rule = cssutils.css.CSSImportRule(parentStyleSheet=self) rule.cssText = self._tokensupto2(tokenizer, token) if expected > 1: self._log.error( u'CSSStylesheet: CSSImportRule not allowed here.', token, xml.dom.HierarchyRequestErr) else: if rule.wellformed: #del rule._parentEncoding # remove as later it is read from this sheet! seq.append(rule) try: # remove as only used temporarily but may not be set at all del self.__newEncoding except AttributeError, e: pass return 1
def _setCssText(self, cssText): """Parse `cssText` and overwrites the whole stylesheet. :param cssText: a parseable string or a tuple of (cssText, dict-of-namespaces) :exceptions: - :exc:`~xml.dom.NamespaceErr`: If a namespace prefix is found which is not declared. - :exc:`~xml.dom.NoModificationAllowedErr`: Raised if the rule is readonly. - :exc:`~xml.dom.SyntaxErr`: Raised if the specified CSS string value has a syntax error and is unparsable. """ self._checkReadonly() cssText, namespaces = self._splitNamespacesOff(cssText) if not namespaces: namespaces = _SimpleNamespaces(log=self._log) tokenizer = self._tokenize2(cssText) newseq = [] #cssutils.css.CSSRuleList() # for closures: must be a mutable new = { 'encoding': None, # needed for setting encoding of @import rules 'namespaces': namespaces } def S(expected, seq, token, tokenizer=None): # @charset must be at absolute beginning of style sheet if expected == 0: return 1 else: return expected def COMMENT(expected, seq, token, tokenizer=None): "special: sets parent*" comment = cssutils.css.CSSComment( [token], parentStyleSheet=self.parentStyleSheet) seq.append(comment) return expected def charsetrule(expected, seq, token, tokenizer): rule = cssutils.css.CSSCharsetRule(parentStyleSheet=self) rule.cssText = self._tokensupto2(tokenizer, token) if expected > 0 or len(seq) > 0: self._log.error( u'CSSStylesheet: CSSCharsetRule only allowed at beginning of stylesheet.', token, xml.dom.HierarchyRequestErr) else: if rule.wellformed: seq.append(rule) new['encoding'] = rule.encoding return 1 def importrule(expected, seq, token, tokenizer): if new['encoding']: # set temporarily as used by _resolveImport # save newEncoding which have been set by resolveImport self.__newEncoding = new['encoding'] rule = cssutils.css.CSSImportRule(parentStyleSheet=self) rule.cssText = self._tokensupto2(tokenizer, token) if expected > 1: self._log.error( u'CSSStylesheet: CSSImportRule not allowed here.', token, xml.dom.HierarchyRequestErr) else: if rule.wellformed: #del rule._parentEncoding # remove as later it is read from this sheet! seq.append(rule) try: # remove as only used temporarily but may not be set at all del self.__newEncoding except AttributeError, e: pass return 1