home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyc (Python 2.6) __all__ = [ 'Selector'] __docformat__ = 'restructuredtext' __version__ = '$Id: selector.py 1868 2009-10-17 19:36:54Z cthedot $' from cssutils.util import _SimpleNamespaces from cssutils.helper import Deprecated import cssutils import xml.dom as xml class Selector(cssutils.util.Base2): def __init__(self, selectorText = None, parent = None, readonly = False): super(Selector, self).__init__() self._Selector__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 __repr__(self): if self._Selector__getNamespaces(): st = (self.selectorText, self._getUsedNamespaces()) else: st = self.selectorText return u'cssutils.css.%s(selectorText=%r)' % (self.__class__.__name__, st) def __str__(self): return u'<cssutils.css.%s object selectorText=%r specificity=%r _namespaces=%r at 0x%x>' % (self.__class__.__name__, self.selectorText, self.specificity, self._getUsedNamespaces(), id(self)) def _getUsedUris(self): uris = set() for item in self.seq: type_ = item.type val = item.value if (type_.endswith(u'-selector') or type_ == u'universal') and type(val) == tuple and val[0] not in (None, u'*'): uris.add(val[0]) continue return uris def _getUsedNamespaces(self): useduris = self._getUsedUris() namespaces = _SimpleNamespaces(log = self._log) for p, uri in self._namespaces.items(): if uri in useduris: namespaces[p] = uri continue return namespaces def __getNamespaces(self): try: return self._parent.parentRule.parentStyleSheet.namespaces except AttributeError: return self._Selector__namespaces _namespaces = property(__getNamespaces, doc = 'If this Selector is attached\n to a CSSStyleSheet the namespaces of that sheet are mirrored here.\n While the Selector (or parent SelectorList or parentRule(s) of that are\n not attached a own dict of {prefix: namespaceURI} is used.') element = property((lambda self: self._element), doc = u'Effective element target of this selector.') parent = property((lambda self: self._parent), doc = '(DOM) The SelectorList that contains this Selector or None if this Selector is not attached to a SelectorList.') def _getSelectorText(self): return cssutils.ser.do_css_Selector(self) def _setSelectorText(self, selectorText): self._checkReadonly() (selectorText, namespaces) = self._splitNamespacesOff(selectorText) try: namespaces = self.parent.parentRule.parentStyleSheet.namespaces except AttributeError: pass tokenizer = self._tokenize2(selectorText) if not tokenizer: self._log.error(u'Selector: No selectorText given.') else: tokens = [] for t in tokenizer: (typ, val, lin, col) = t if val == u':' and tokens and self._tokenvalue(tokens[-1]) == ':': tokens[-1] = (typ, u'::', lin, col) continue if typ == 'IDENT' and tokens and self._tokenvalue(tokens[-1]) == u'.': tokens[-1] = ('class', u'.' + val, lin, col) continue if typ == 'IDENT' and tokens and self._tokenvalue(tokens[-1]).startswith(u':') and not self._tokenvalue(tokens[-1]).endswith(u'('): if self._tokenvalue(tokens[-1]).startswith(u'::'): t = 'pseudo-element' else: t = 'pseudo-class' tokens[-1] = (t, self._tokenvalue(tokens[-1]) + val, lin, col) continue if typ == 'FUNCTION' and val == u'not(' and tokens and u':' == self._tokenvalue(tokens[-1]): tokens[-1] = ('negation', u':' + val, lin, tokens[-1][3]) continue if typ == 'FUNCTION' and tokens and self._tokenvalue(tokens[-1]).startswith(u':'): if self._tokenvalue(tokens[-1]).startswith(u'::'): t = 'pseudo-element' else: t = 'pseudo-class' tokens[-1] = (t, self._tokenvalue(tokens[-1]) + val, lin, col) continue if val == u'*' and tokens and self._type(tokens[-1]) == 'namespace_prefix' and self._tokenvalue(tokens[-1]).endswith(u'|'): tokens[-1] = ('universal', self._tokenvalue(tokens[-1]) + val, lin, col) continue if val == u'*': tokens.append(('universal', val, lin, col)) continue if val == u'|' and tokens and self._type(tokens[-1]) in (self._prods.IDENT, 'universal') and self._tokenvalue(tokens[-1]).find(u'|') == -1: tokens[-1] = ('namespace_prefix', self._tokenvalue(tokens[-1]) + u'|', lin, col) continue if val == u'|': tokens.append(('namespace_prefix', val, lin, col)) continue tokens.append(t) tokenizer = (lambda .0: for t in .0: t)(tokens) new = { 'context': [ ''], 'element': None, '_PREFIX': None, 'specificity': [ 0, 0, 0, 0], 'wellformed': True } S = u' ' def append(seq, val, typ = None, token = (None, None, None)): context = new['context'][-1] if token: line = token[2] col = token[3] else: (line, col) = (None, None) if typ == '_PREFIX': new['_PREFIX'] = val[:-1] return None if new['_PREFIX'] is not None: prefix = new['_PREFIX'] new['_PREFIX'] = None elif typ == 'universal' and '|' in val: (prefix, val) = val.split('|') else: prefix = None if typ.endswith('-selector') or typ == 'universal': if 'attribute-selector' == typ: pass if not (not prefix): if prefix == u'*': namespaceURI = cssutils._ANYNS elif prefix is None: namespaceURI = namespaces.get(u'', None) elif prefix == u'': namespaceURI = u'' else: namespaceURI = namespaces[prefix] if namespaceURI is None: new['wellformed'] = False self._log.error(u'Selector: No namespaceURI found for prefix %r' % prefix, token = token, error = xml.dom.NamespaceErr) return None val = (namespaceURI, val) if not context or context == 'negation': if 'id' == typ: new['specificity'][1] += 1 elif 'class' == typ or '[' == val: new['specificity'][2] += 1 elif typ in ('type-selector', 'negation-type-selector', 'pseudo-element'): new['specificity'][3] += 1 if not context and typ in ('type-selector', 'universal'): new['element'] = val seq.append(val, typ, line = line, col = col) simple_selector_sequence = 'type_selector universal HASH class attrib pseudo negation ' simple_selector_sequence2 = 'HASH class attrib pseudo negation ' element_name = 'element_name' negation_arg = 'type_selector universal HASH class attrib pseudo' negationend = ')' attname = 'prefix attribute' attname2 = 'attribute' attcombinator = 'combinator ]' attvalue = 'value' attend = ']' expressionstart = 'PLUS - DIMENSION NUMBER STRING IDENT' expression = expressionstart + ' )' combinator = ' combinator' def _COMMENT(expected, seq, token, tokenizer = (None,)): append(seq, cssutils.css.CSSComment([ token]), 'COMMENT', token = token) return expected def _S(expected, seq, token, tokenizer = (None, None, None, None, None)): context = new['context'][-1] if context.startswith('pseudo-'): if seq and seq[-1].value not in u'+-': append(seq, S, 'S', token = token) return expected if context != 'attrib' and 'combinator' in expected: append(seq, S, 'descendant', token = token) return simple_selector_sequence + combinator return expected def _universal(expected, seq, token, tokenizer = (None, None, None, None, None, None)): context = new['context'][-1] val = self._tokenvalue(token) if 'universal' in expected: append(seq, val, 'universal', token = token) if 'negation' == context: return negationend return simple_selector_sequence2 + combinator 'universal' in expected new['wellformed'] = False self._log.error(u'Selector: Unexpected universal.', token = token) return expected def _namespace_prefix(expected, seq, token, tokenizer = (None, None, None, None, None)): context = new['context'][-1] val = self._tokenvalue(token) if 'attrib' == context and 'prefix' in expected: append(seq, val, '_PREFIX', token = token) return attname2 if 'type_selector' in expected: append(seq, val, '_PREFIX', token = token) return element_name new['wellformed'] = False self._log.error(u'Selector: Unexpected namespace prefix.', token = token) return expected def _pseudo(expected, seq, token, tokenizer = (None, None, None, None, None, None, None)): context = new['context'][-1] val = self._tokenvalue(token, normalize = True) typ = self._type(token) if 'pseudo' in expected: if val in (':first-line', ':first-letter', ':before', ':after'): typ = 'pseudo-element' append(seq, val, typ, token = token) if val.endswith(u'('): new['context'].append(typ) return expressionstart if 'negation' == context: return negationend if 'pseudo-element' == typ: return combinator return simple_selector_sequence2 + combinator 'pseudo' in expected new['wellformed'] = False self._log.error(u'Selector: Unexpected start of pseudo.', token = token) return expected def _expression(expected, seq, token, tokenizer = (None, None, None, None)): context = new['context'][-1] val = self._tokenvalue(token) typ = self._type(token) if context.startswith('pseudo-'): append(seq, val, typ, token = token) return expression new['wellformed'] = False self._log.error(u'Selector: Unexpected %s.' % typ, token = token) return expected def _attcombinator(expected, seq, token, tokenizer = (None, None, None, None)): context = new['context'][-1] val = self._tokenvalue(token) typ = self._type(token) if 'attrib' == context and 'combinator' in expected: append(seq, val, typ.lower(), token = token) return attvalue new['wellformed'] = False self._log.error(u'Selector: Unexpected %s.' % typ, token = token) return expected def _string(expected, seq, token, tokenizer = (None, None, None, None, None)): context = new['context'][-1] typ = self._type(token) val = self._stringtokenvalue(token) if 'attrib' == context and 'value' in expected: append(seq, val, typ, token = token) return attend if context.startswith('pseudo-'): append(seq, val, typ, token = token) return expression new['wellformed'] = False self._log.error(u'Selector: Unexpected STRING.', token = token) return expected def _ident(expected, seq, token, tokenizer = (None, None, None, None, None, None, None, None, None, None)): context = new['context'][-1] val = self._tokenvalue(token) typ = self._type(token) if 'attrib' == context and 'attribute' in expected: append(seq, val, 'attribute-selector', token = token) return attcombinator if 'attrib' == context and 'value' in expected: append(seq, val, 'attribute-value', token = token) return attend if 'negation' == context: append(seq, val, 'negation-type-selector', token = token) return negationend if context.startswith('pseudo-'): append(seq, val, typ, token = token) return expression if 'type_selector' in expected or element_name == expected: append(seq, val, 'type-selector', token = token) return simple_selector_sequence2 + combinator new['wellformed'] = False self._log.error(u'Selector: Unexpected IDENT.', token = token) return expected def _class(expected, seq, token, tokenizer = (None, None, None, None, None, None)): context = new['context'][-1] val = self._tokenvalue(token) if 'class' in expected: append(seq, val, 'class', token = token) if 'negation' == context: return negationend return simple_selector_sequence2 + combinator 'class' in expected new['wellformed'] = False self._log.error(u'Selector: Unexpected class.', token = token) return expected def _hash(expected, seq, token, tokenizer = (None, None, None, None, None, None)): context = new['context'][-1] val = self._tokenvalue(token) if 'HASH' in expected: append(seq, val, 'id', token = token) if 'negation' == context: return negationend return simple_selector_sequence2 + combinator 'HASH' in expected new['wellformed'] = False self._log.error(u'Selector: Unexpected HASH.', token = token) return expected def _char(expected, seq, token, tokenizer = (None, None, None, None, None, None, None, None, None, None, None)): context = new['context'][-1] val = self._tokenvalue(token) if u']' == val and 'attrib' == context and ']' in expected: append(seq, val, 'attribute-end', token = token) context = new['context'].pop() context = new['context'][-1] if 'negation' == context: return negationend return simple_selector_sequence2 + combinator ']' in expected if u'=' == val and 'attrib' == context and 'combinator' in expected: append(seq, val, 'equals', token = token) return attvalue if u')' == val and 'negation' == context and u')' in expected: append(seq, val, 'negation-end', token = token) new['context'].pop() context = new['context'][-1] return simple_selector_sequence + combinator if val in u'+-' and context.startswith('pseudo-'): _names = { '+': 'plus', '-': 'minus' } return expression if u')' == val and context.startswith('pseudo-') and expression == expected: append(seq, val, 'function-end', token = token) new['context'].pop() if 'pseudo-element' == context: return combinator return simple_selector_sequence + combinator expression == expected if u'[' == val and 'attrib' in expected: append(seq, val, 'attribute-start', token = token) new['context'].append('attrib') return attname if val in u'+>~' and 'combinator' in expected: _names = { '>': 'child', '+': 'adjacent-sibling', '~': 'following-sibling' } return simple_selector_sequence if u',' == val: new['wellformed'] = False self._log.error(u'Selector: Single selector only.', error = xml.dom.InvalidModificationErr, token = token) return expected new['wellformed'] = False self._log.error(u'Selector: Unexpected CHAR.', token = token) return expected def _negation(expected, seq, token, tokenizer = (None, None, None, None)): context = new['context'][-1] val = self._tokenvalue(token, normalize = True) if 'negation' in expected: new['context'].append('negation') append(seq, val, 'negation-start', token = token) return negation_arg new['wellformed'] = False self._log.error(u'Selector: Unexpected negation.', token = token) return expected def _atkeyword(expected, seq, token, tokenizer = (None, None)): new['wellformed'] = False self._log.error(u'Selector: Unexpected ATKEYWORD.', token = token) return expected newseq = self._tempSeq() (wellformed, expected) = self._parse(expected = simple_selector_sequence, seq = newseq, tokenizer = tokenizer, productions = { 'CHAR': _char, 'class': _class, 'HASH': _hash, 'STRING': _string, 'IDENT': _ident, 'namespace_prefix': _namespace_prefix, 'negation': _negation, 'pseudo-class': _pseudo, 'pseudo-element': _pseudo, 'universal': _universal, 'NUMBER': _expression, 'DIMENSION': _expression, 'PREFIXMATCH': _attcombinator, 'SUFFIXMATCH': _attcombinator, 'SUBSTRINGMATCH': _attcombinator, 'DASHMATCH': _attcombinator, 'INCLUDES': _attcombinator, 'S': _S, 'COMMENT': _COMMENT, 'ATKEYWORD': _atkeyword }) if wellformed: pass wellformed = new['wellformed'] if len(new['context']) > 1 or not newseq: wellformed = False self._log.error(u'Selector: Invalid or incomplete selector: %s' % self._valuestr(selectorText)) if expected == 'element_name': wellformed = False self._log.error(u'Selector: No element name found: %s' % self._valuestr(selectorText)) if expected == simple_selector_sequence and newseq: wellformed = False self._log.error(u'Selector: Cannot end with combinator: %s' % self._valuestr(selectorText)) if newseq and hasattr(newseq[-1].value, 'strip') and newseq[-1].value.strip() == u'': del newseq[-1] if wellformed: self._Selector__namespaces = namespaces self._element = new['element'] self._specificity = tuple(new['specificity']) self._setSeq(newseq) self._Selector__namespaces = self._getUsedNamespaces() selectorText = property(_getSelectorText, _setSelectorText, doc = '(DOM) The parsable textual representation of the selector.') specificity = property((lambda self: self._specificity), doc = 'Specificity of this selector (READONLY). \n Tuple of (a, b, c, d) where: \n \n a\n presence of style in document, always 0 if not used on a document\n b\n number of ID selectors\n c \n number of .class selectors\n d \n number of Element (type) selectors\n ') wellformed = property((lambda self: bool(len(self.seq)))) def _getParentList(self): return self.parent _getParentList = Deprecated('Use property parent instead')(_getParentList) parentList = property(_getParentList, doc = 'DEPRECATED, see property parent instead')