o
    Zh0hB5                     @   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   E/var/www/html/rh/venv/lib/python3.10/site-packages/cssselect2/tree.pyfrom_xml_root   s   zElementWrapper.from_xml_rootc                 C   r	   )zSame 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 )zReturn 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td< |S )z,The local name of this element, as a string.namespace_url_split_etree_tagr   r]   __dict__r^   r"   r_   
local_namer   r   r   rd   0     zElementWrapper.local_namec                 C   s"   t | jj\}}|| jt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   sF   |  d}|dkrd| fS | d dksJ | d| | |d d  fS )N}r=   ri   r   {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	