o
    ½Ùøi,5  ã                   @   sV   d dl mZ d dlmZ d dlmZ ddlmZmZ G dd„ dƒZ	dd	„ Z
d
d„ ZdS )é    )Úcached_property)Úwarn)Úascii_loweré   )Úcompile_selector_listÚsplit_whitespacec                   @   s&  e Zd ZdZed:dd„ƒZed:dd„ƒZed;dd	„ƒZ	d:d
d„Zdd„ Z	dd„ Z
dd„ Zdd„ Zedd„ ƒZedd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zed$d%„ ƒZd&d'„ Zd(d)„ Zd*d+„ Zed,d-„ ƒZed.d/„ ƒZed0d1„ ƒZed2d3„ ƒZed4d5„ ƒZ ed6d7„ ƒZ!ed8d9„ ƒZ"dS )<ÚElementWrapperaí  Wrapper of :class:`xml.etree.ElementTree.Element` for Selector matching.

    This class should not be instanciated directly. :meth:`from_xml_root` or
    :meth:`from_html_root` should be used for the root element of a document,
    and other elements should be accessed (and wrappers generated) using
    methods such as :meth:`iter_children` and :meth:`iter_subtree`.

    :class:`ElementWrapper` objects compare equal if their underlying
    :class:`xml.etree.ElementTree.Element` do.

    Nc                 C   ó   | j ||dd�S )a.  Wrap for selector matching the root of an XML or XHTML document.

        :param root:
            An ElementTree :class:`xml.etree.ElementTree.Element`
            for the root element of a document.
            If the given element is not the root,
            selector matching will behave is if it were.
            In other words, selectors will be not be `scoped`_
            to the subtree rooted at that element.
        :returns:
            A new :class:`ElementWrapper`

        .. _scoped: https://drafts.csswg.org/selectors-4/#scoping

        F©Úin_html_document©Ú
_from_root©ÚclsÚrootÚcontent_language© r   ú\/home/develop/edictos_py/central_edictos/env/lib/python3.10/site-packages/cssselect2/tree.pyÚfrom_xml_root   s   zElementWrapper.from_xml_rootc                 C   r	   )zæSame as :meth:`from_xml_root` with case-insensitive attribute names.

        Useful for documents parsed with an HTML parser like html5lib, which
        should be the case of documents with the ``text/html`` MIME type.

        Tr
   r   r   r   r   r   Úfrom_html_root(   s   zElementWrapper.from_html_rootTc                 C   s&   t |dƒr	| ¡ }| |d dd ||d�S )NÚgetrootr   )ÚparentÚindexÚpreviousr   r   )Úhasattrr   )r   r   r   r   r   r   r   r   2   s   
þzElementWrapper._from_rootc                 C   sN   || _ || _|| _|d ur|j| _n|g| _|| _|| _|| _d | _d | _	d S ©N)
Úetree_elementr   r   Úetree_childrenÚetree_siblingsr   r   Útransport_content_languageÚ
_ancestorsÚ_previous_siblings)Úselfr   r   r   r   r   r   r   r   r   Ú__init__:   s   

zElementWrapper.__init__c                 C   s   t | ƒt |ƒu o| j|jkS r   )Útyper   ©r"   Úotherr   r   r   Ú__eq__V   s   
þzElementWrapper.__eq__c                 C   s
   | |k S r   r   r%   r   r   r   Ú__ne__[   s   
zElementWrapper.__ne__c                 C   s   t t| ƒ| jfƒS r   )Úhashr$   r   ©r"   r   r   r   Ú__hash__^   s   zElementWrapper.__hash__c                 c   s   � |   ¡ E d H  d S r   )Úiter_childrenr*   r   r   r   Ú__iter__a   s   €zElementWrapper.__iter__c                 C   ó4   | j du r| jdu rdn	g | jj¢| j‘R | _ | j S )uÂ   Tuple of existing ancestors.

        Tuple of existing :class:`ElementWrapper` objects for this elementâ€™s
        ancestors, in reversed tree order, from :attr:`parent` to the root.

        Nr   )r    r   Ú	ancestorsr*   r   r   r   r/   d   s   
 ÿzElementWrapper.ancestorsc                 C   r.   )u¨   Tuple of previous siblings.

        Tuple of existing :class:`ElementWrapper` objects for this elementâ€™s
        previous siblings, in reversed tree order.

        Nr   )r!   r   Úprevious_siblingsr*   r   r   r   r0   q   s
   
þz ElementWrapper.previous_siblingsc                 c   ó   � t dtƒ | jE dH  dS )u¨  Iterate over ancestors.

        Return an iterator of existing :class:`ElementWrapper` objects for this
        elementâ€™s ancestors, in reversed tree order (from :attr:`parent` to the
        root).

        The element itself is not included, this is an empty sequence for the
        root element.

        This method is deprecated and will be removed in version 0.7.0. Use
        :attr:`ancestors` instead.

        zfThis method is deprecated and will be removed in version 0.7.0. Use the "ancestors" attribute instead.N)r   ÚDeprecationWarningr/   r*   r   r   r   Úiter_ancestors   s   €ýzElementWrapper.iter_ancestorsc                 c   r1   )u§  Iterate over previous siblings.

        Return an iterator of existing :class:`ElementWrapper` objects for this
        elementâ€™s previous siblings, in reversed tree order.

        The element itself is not included, this is an empty sequence for a
        first child or the root element.

        This method is deprecated and will be removed in version 0.7.0. Use
        :attr:`previous_siblings` instead.

        znThis method is deprecated and will be removed in version 0.7.0. Use the "previous_siblings" attribute instead.N)r   r2   r0   r*   r   r   r   Úiter_previous_siblings“   s   €ýz%ElementWrapper.iter_previous_siblingsc                 c   s*   � | j du r| V  dS | j  ¡ E dH  dS )u£   Iterate over siblings.

        Return an iterator of newly-created :class:`ElementWrapper` objects for
        this elementâ€™s siblings, in tree order.

        N)r   r,   r*   r   r   r   Úiter_siblings¦   s   €

zElementWrapper.iter_siblingsc                 c   s.   � d}|   ¡ D ]}|r|V  || krd}qdS )u­   Iterate over next siblings.

        Return an iterator of newly-created :class:`ElementWrapper` objects for
        this elementâ€™s next siblings, in tree order.

        FTN)r5   )r"   ÚfoundÚsiblingr   r   r   Úiter_next_siblings²   s   €€üz!ElementWrapper.iter_next_siblingsc                 c   s<   � d}t | jƒD ]\}}t| ƒ|| ||| jd�}|V  qdS )u©   Iterate over children.

        Return an iterator of newly-created :class:`ElementWrapper` objects for
        this elementâ€™s child elements, in tree order.

        N)r   r   r   r   )Ú	enumerater   r$   r   )r"   ÚchildÚiÚetree_childr   r   r   r,   À   s   €þüzElementWrapper.iter_childrenc                 c   sR   � t | gƒg}|r't|d dƒ}|du r| ¡  n
|V  | | ¡ ¡ |s	dS dS )a¨  Iterate over subtree.

        Return an iterator of newly-created :class:`ElementWrapper` objects for
        the entire subtree rooted at this element, in tree order.

        Unlike in other methods, the element itself *is* included.

        This loops over an entire document:

        .. code-block:: python

            for element in ElementWrapper.from_root(root_etree).iter_subtree():
                ...

        éÿÿÿÿN)ÚiterÚnextÚpopÚappendr,   )r"   ÚstackÚelementr   r   r   Úiter_subtreeÎ   s   €
úzElementWrapper.iter_subtreec                 C   s   dd„ | D ƒS )Nc                 S   s@   g | ]}t |d ƒr|gnt|ƒD ]}|jdu r|js|j‘qqS )ÚtestN)r   r   Úpseudo_elementÚnever_matchesrE   )Ú.0ÚselectorÚcompiled_selectorr   r   r   Ú
<listcomp>é   s    û
ÿûz+ElementWrapper._compile.<locals>.<listcomp>r   )Ú	selectorsr   r   r   Ú_compileç   s   þzElementWrapper._compilec                    s   t ‡ fdd„ˆ  |¡D ƒƒS )zðReturn wether this elememt matches any of the given selectors.

        :param selectors:
            Each given selector is either a :class:`compiler.CompiledSelector`,
            or an argument to :func:`compile_selector_list`.

        c                 3   ó   � | ]}|ˆ ƒV  qd S r   r   ©rH   rE   r*   r   r   Ú	<genexpr>ú   ó   € z)ElementWrapper.matches.<locals>.<genexpr>)ÚanyrM   ©r"   rL   r   r*   r   Úmatchesò   s   zElementWrapper.matchesc                    sJ   |   |¡‰ tˆ ƒdkrtˆ d |  ¡ ƒS |r!‡ fdd„|  ¡ D ƒS tdƒS )aÛ  Return elements, in tree order, that match any of given selectors.

        Selectors are `scoped`_ to the subtree rooted at this element.

        .. _scoped: https://drafts.csswg.org/selectors-4/#scoping

        :param selectors:
            Each given selector is either a :class:`compiler.CompiledSelector`,
            or an argument to :func:`compile_selector_list`.
        :returns:
            An iterator of newly-created :class:`ElementWrapper` objects.

        r   r   c                 3   s*   � | ]‰ t ‡ fd d„ˆD ƒƒrˆ V  qdS )c                 3   rN   r   r   rO   ©rC   r   r   rP     rQ   z5ElementWrapper.query_all.<locals>.<genexpr>.<genexpr>N)rR   )rH   ©ÚtestsrU   r   rP     s   € þ
ÿz+ElementWrapper.query_all.<locals>.<genexpr>r   )rM   ÚlenÚfilterrD   r>   rS   r   rV   r   Ú	query_allü   s   

ÿzElementWrapper.query_allc                 G   s   t | j|Ž dƒS )aj  Return first element that matches any of given selectors.

        :param selectors:
            Each given selector is either a :class:`compiler.CompiledSelector`,
            or an argument to :func:`compile_selector_list`.
        :returns:
            A newly-created :class:`ElementWrapper` object,
            or :obj:`None` if there is no match.

        N)r?   rZ   rS   r   r   r   Úquery  s   zElementWrapper.queryc                 C   s   dd„ | j D ƒS )a%  Children as a list of :class:`xml.etree.ElementTree.Element`.

        Other ElementTree nodes such as
        :func:`comments <xml.etree.ElementTree.Comment>` and
        :func:`processing instructions
        <xml.etree.ElementTree.ProcessingInstruction>`
        are not included.

        c                 S   s   g | ]
}t |jtƒr|‘qS r   )Ú
isinstanceÚtagÚstr)rH   rC   r   r   r   rK   ,  s    
þÿz1ElementWrapper.etree_children.<locals>.<listcomp>)r   r*   r   r   r   r   !  s   ÿzElementWrapper.etree_childrenc                 C   s   t | jjƒ\}}|| jd< |S )z,The local name of this element, as a string.Únamespace_url©Ú_split_etree_tagr   r]   Ú__dict__©r"   r_   Ú
local_namer   r   r   rd   0  ó   
zElementWrapper.local_namec                 C   s   t | jjƒ\}}|| jd< |S )z/The namespace URL of this element, as a string.rd   r`   rc   r   r   r   r_   7  re   zElementWrapper.namespace_urlc                 C   s   | j  d¡S )z$The ID of this element, as a string.Úid)r   Úgetr*   r   r   r   rf   >  s   zElementWrapper.idc                 C   s   t t| j dd¡ƒƒS )z:The classes of this element, as a :class:`set` of strings.ÚclassÚ )Úsetr   r   rg   r*   r   r   r   ÚclassesC  s   zElementWrapper.classesc                 C   sÀ   | j  d¡}|durt|ƒS | jp| jdk}|r&| j  d¡}|dur&t|ƒS | jdur/| jjS |rYd}| j  d¡}|D ]}| dd¡}t|ƒdkrPt| d	¡ƒ}q;|durYt|ƒS t| j	ƒp_dS )
z*The language of this element, as a string.z*{http://www.w3.org/XML/1998/namespace}langNzhttp://www.w3.org/1999/xhtmlÚlangz"{http://www.w3.org/1999/xhtml}metaz
http-equivri   zcontent-languageÚcontent)
r   rg   r   r   r_   r   rl   r>   Ú_parse_content_languager   )r"   Úxml_langÚis_htmlrl   r   ÚiteratorÚmetaÚ
http_equivr   r   r   rl   H  s.   þ
€zElementWrapper.langc                    sh   | j d u rdS d}d‰ | j jj|ko-| j j d¡d uo-| jjˆ kp-t‡ fdd„|  ¡ D ƒƒ}|p3| j jS )NFz&{http://www.w3.org/1999/xhtml}fieldsetz${http://www.w3.org/1999/xhtml}legendÚdisabledc                 3   s   � | ]	}|j jˆ kV  qd S r   )r   r]   )rH   r7   ©Úlegendr   r   rP   n  s
   € 
ÿ
ÿz6ElementWrapper.in_disabled_fieldset.<locals>.<genexpr>)r   r   r]   rg   rR   r4   Úin_disabled_fieldset)r"   ÚfieldsetÚdisabled_fieldsetr   ru   r   rw   e  s   
ÿþýz#ElementWrapper.in_disabled_fieldsetr   )T)#Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úclassmethodr   r   r   r#   r'   r(   r+   r-   Úpropertyr/   r0   r3   r4   r5   r8   r,   rD   ÚstaticmethodrM   rT   rZ   r[   r   r   rd   r_   rf   rk   rl   rw   r   r   r   r   r   	   sV    	
ÿ










r   c                 C   sB   |   d¡}|dks| d dkrd| fS | d|… | |d d … fS )NÚ}r=   r   Ú{ri   r   )Úrfind)r]   Úpositionr   r   r   ra   t  s   
ra   c                 C   s8   | d urd| vrt | ƒ}t|ƒdkr|d S d S d S d S )Nú,r   r   )r   rX   )ÚvalueÚpartsr   r   r   rn   |  s   ýrn   N)Ú	functoolsr   Úwarningsr   Úwebencodingsr   Úcompilerr   r   r   ra   rn   r   r   r   r   Ú<module>   s      m