1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
"""CSS4 selectors for Python.
cssselect2 is a straightforward implementation of CSS4 Selectors for markup
documents (HTML, XML, etc.) that can be read by ElementTree-like parsers
(including cElementTree, lxml, html5lib, etc.)
"""
from webencodings import ascii_lower
# Classes are imported here to expose them at the top level of the module
from .compiler import compile_selector_list # noqa
from .parser import SelectorError # noqa
from .tree import ElementWrapper # noqa
VERSION = __version__ = '0.8.0'
class Matcher:
"""A CSS selectors storage that can match against HTML elements."""
def __init__(self):
self.id_selectors = {}
self.class_selectors = {}
self.lower_local_name_selectors = {}
self.namespace_selectors = {}
self.lang_attr_selectors = []
self.other_selectors = []
self.order = 0
def add_selector(self, selector, payload):
"""Add a selector and its payload to the matcher.
:param selector:
A :class:`compiler.CompiledSelector` object.
:param payload:
Some data associated to the selector,
such as :class:`declarations <tinycss2.ast.Declaration>`
parsed from the :attr:`tinycss2.ast.QualifiedRule.content`
of a style rule.
It can be any Python object,
and will be returned as-is by :meth:`match`.
"""
self.order += 1
if selector.never_matches:
return
entry = (
selector.test, selector.specificity, self.order, selector.pseudo_element,
payload)
if selector.id is not None:
self.id_selectors.setdefault(selector.id, []).append(entry)
elif selector.class_name is not None:
self.class_selectors.setdefault(selector.class_name, []).append(entry)
elif selector.local_name is not None:
self.lower_local_name_selectors.setdefault(
selector.lower_local_name, []).append(entry)
elif selector.namespace is not None:
self.namespace_selectors.setdefault(selector.namespace, []).append(entry)
elif selector.requires_lang_attr:
self.lang_attr_selectors.append(entry)
else:
self.other_selectors.append(entry)
def match(self, element):
"""Match selectors against the given element.
:param element:
An :class:`ElementWrapper`.
:returns:
A list of the payload objects associated to selectors that match
element, in order of lowest to highest
:attr:`compiler.CompiledSelector` specificity and in order of
addition with :meth:`add_selector` among selectors of equal
specificity.
"""
relevant_selectors = []
if element.id is not None and element.id in self.id_selectors:
self.add_relevant_selectors(
element, self.id_selectors[element.id], relevant_selectors)
for class_name in element.classes:
if class_name in self.class_selectors:
self.add_relevant_selectors(
element, self.class_selectors[class_name], relevant_selectors)
lower_name = ascii_lower(element.local_name)
if lower_name in self.lower_local_name_selectors:
self.add_relevant_selectors(
element, self.lower_local_name_selectors[lower_name],
relevant_selectors)
if element.namespace_url in self.namespace_selectors:
self.add_relevant_selectors(
element, self.namespace_selectors[element.namespace_url],
relevant_selectors)
if 'lang' in element.etree_element.attrib:
self.add_relevant_selectors(
element, self.lang_attr_selectors, relevant_selectors)
self.add_relevant_selectors(element, self.other_selectors, relevant_selectors)
relevant_selectors.sort()
return relevant_selectors
@staticmethod
def add_relevant_selectors(element, selectors, relevant_selectors):
for test, specificity, order, pseudo, payload in selectors:
if test(element):
relevant_selectors.append((specificity, order, pseudo, payload))
|