U
    :g]                     @   sb   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 d Z
ZG d	d
 d
ZeddZdS )zCSS4 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.)

    N)ascii_lower   )compile_selector_list)SelectorError)ElementWrapperz0.7.0c                   @   s4   e Zd ZdZdd Zdd Zdd Zedd	 Zd
S )Matcherz=A CSS selectors storage that can match against HTML elements.c                 C   s.   i | _ i | _i | _i | _g | _g | _d| _d S )Nr   )id_selectorsclass_selectorslower_local_name_selectorsnamespace_selectorslang_attr_selectorsother_selectorsorder)self r   7/tmp/pip-unpacked-wheel-xeg67qda/cssselect2/__init__.py__init__   s    zMatcher.__init__c                 C   s   |  j d7  _ |jrdS |j|j| j |j|f}|jdk	rP| j|jg | n|j	dk	rr| j
|j	g | nd|jdk	r| j|jg | nB|jdk	r| j|jg | n |jr| j| n| j| dS )a  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`.

        r   N)r   Znever_matchestestspecificityZpseudo_elementidr   
setdefaultappend
class_namer	   
local_namer
   Zlower_local_name	namespacer   Zrequires_lang_attrr   r   )r   selectorpayloadentryr   r   r   add_selector    sF       

 
 
 zMatcher.add_selectorc                 C   s   g }|j dk	r0|j | jkr0| || j|j  | |jD ]"}|| jkr6| || j| | q6t|j}|| jkr| || j| | |j| j	kr| || j	|j | d|j
jkr| || j| | || j| |jtd |S )a  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.

        Nlang)key)r   r   add_relevant_selectorsclassesr	   r   r   r
   Znamespace_urlr   Zetree_elementattribr   r   sortSORT_KEY)r   elementrelevant_selectorsr   Z
lower_namer   r   r   matchF   sP     
 

 

  
    zMatcher.matchc                 C   s2   |D ](\}}}}}|| r| ||||f qd S )N)r   )r&   	selectorsr'   r   r   r   Zpseudor   r   r   r   r!   s   s
    
zMatcher.add_relevant_selectorsN)	__name__
__module____qualname____doc__r   r   r(   staticmethodr!   r   r   r   r   r      s   	&-r   )r-   operatorwebencodingsr   compilerr   parserr   treer   VERSION__version__r   
itemgetterr%   r   r   r   r   <module>   s   f