示例#1
0
def select_pseudo(cache, pseudo):
    try:
        func = cache.dispatch_map[pseudo.ident.replace('-', '_')]
    except KeyError:
        if pseudo.ident == 'root':
            yield cache.root
            return

        if pseudo.ident in cache.ignore_inappropriate_pseudo_classes:

            def func(cache, item):
                return True

            func.is_pseudo = True
        else:
            raise ExpressionError("The pseudo-class :%s is not supported" %
                                  pseudo.ident)

    try:
        func.is_pseudo
    except AttributeError:
        raise ExpressionError("The pseudo-class :%s is invalid" % pseudo.ident)

    for item in cache.iterparsedselector(pseudo.selector):
        if func(cache, item):
            yield item
示例#2
0
def get_func_for_pseudo(cache, ident):
    try:
        func = cache.dispatch_map[ident.replace('-', '_')]
    except KeyError:
        if ident in cache.ignore_inappropriate_pseudo_classes:
            func = allow_all
        else:
            raise ExpressionError("The pseudo-class :%s is not supported" %
                                  ident)

    try:
        func.is_pseudo
    except AttributeError:
        raise ExpressionError("The pseudo-class :%s is invalid" % ident)
    return func
示例#3
0
 def parsed_arguments(self):
     if self._parsed_arguments is None:
         try:
             self._parsed_arguments = parse_series(self.arguments)
         except ValueError:
             raise ExpressionError("Invalid series: '%r'" % self.arguments)
     return self._parsed_arguments
示例#4
0
 def iterparsedselector(self, parsed_selector):
     type_name = type(parsed_selector).__name__
     try:
         func = self.dispatch_map[ascii_lower(type_name)]
     except KeyError:
         raise ExpressionError('%s is not supported' % type_name)
     for item in func(self, parsed_selector):
         yield item
示例#5
0
def select_selector(cache, selector):
    if selector.pseudo_element is None:
        for item in cache.iterparsedselector(selector.parsed_tree):
            yield item
        return
    if isinstance(selector.pseudo_element, FunctionalPseudoElement):
        raise ExpressionError(
            "The pseudo-element ::%s is not supported" % selector.pseudo_element.name)
    func = get_func_for_pseudo(cache, selector.pseudo_element)
    for item in cache.iterparsedselector(selector.parsed_tree):
        if func(cache, item):
            yield item
示例#6
0
def select_lang(cache, function):
    ' Implement :lang() '
    if function.argument_types() not in (['STRING'], ['IDENT']):
        raise ExpressionError("Expected a single string or ident for :lang(), got %r" % function.arguments)
    lang = function.arguments[0].value
    if lang:
        lang = ascii_lower(lang)
        lp = lang + '-'
        for tlang, elem_set in iteritems(cache.lang_map):
            if tlang == lang or (tlang is not None and tlang.startswith(lp)):
                for elem in elem_set:
                    yield elem
示例#7
0
def select_function(cache, function):
    """Select with a functional pseudo-class."""
    fname = function.name.replace('-', '_')
    try:
        func = cache.dispatch_map[fname]
    except KeyError:
        raise ExpressionError(
            "The pseudo-class :%s() is unknown" % function.name)
    if fname == 'lang':
        items = frozenset(func(cache, function))
        for item in cache.iterparsedselector(function.selector):
            if item in items:
                yield item
    else:
        for item in cache.iterparsedselector(function.selector):
            if func(cache, function, item):
                yield item