
    j,5                     `    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          Z	d Z
d	 Zd
S )    )cached_property)warn)ascii_lower   )compile_selector_listsplit_whitespacec                      e Zd ZdZedd            Zedd            Zed d            Z	 ddZd Z	d	 Z
d
 Zd Zed             Zed             Zd Zd Zd Zd Zd Zd Zed             Zd Zd Zd Zed             Zed             Zed             Zed             Zed             Z ed             Z!ed             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                 2    |                      ||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_languages      V/home/ubuntu/budget-transfer-bot/.venv/lib/python3.11/site-packages/cssselect2/tree.pyfrom_xml_rootzElementWrapper.from_xml_root   s    " ~~d$4u~MMM    c                 2    |                      ||d          S )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   s      r   from_html_rootzElementWrapper.from_html_root(   s     ~~d$4t~LLLr   Tc                 l    t          |d          r|                                } | |d dd ||          S )Ngetrootr   )parentindexpreviousr   r   )hasattrr   )r   r   r   r   s       r   r   zElementWrapper._from_root2   sM    4## 	"<<>>DsQ-@PR R R 	Rr   c                     || _         || _        || _        ||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   s          r   __init__zElementWrapper.__init__:   sn     +  !
 #)"7D#0/D 
 0*:' "&r   c                 ^    t          |           t          |          u o| j        |j        k    S r    )typer!   r'   others     r   __eq__zElementWrapper.__eq__V   s.    JJ$u++% 6%"55	7r   c                     | |k     S r     r+   s     r   __ne__zElementWrapper.__ne__[   s    EM""r   c                 H    t          t          |           | j        f          S r    )hashr*   r!   r'   s    r   __hash__zElementWrapper.__hash__^   s    T$ZZ!34555r   c              #   >   K   |                                  E d {V  d S r    )iter_childrenr3   s    r   __iter__zElementWrapper.__iter__a   s0      %%'''''''''''r   c                 f    | j         $| j        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   	ancestorsr3   s    r   r9   zElementWrapper.ancestorsd   s>     ?"k)/T1F/T/T/T Or   c                 f    | j         $| j        dng | j        j        | j        R | _         | j         S )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_siblingsr3   s    r   r;   z ElementWrapper.previous_siblingsq   sC     "*m+A$-1A4=AA # &&r   c              #   N   K   t          dt                     | j        E d{V  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   DeprecationWarningr9   r3   s    r   iter_ancestorszElementWrapper.iter_ancestors   sD       	5	  	  	  >!!!!!!!!!r   c              #   N   K   t          dt                     | j        E d{V  dS )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   r=   r;   r3   s    r   iter_previous_siblingsz%ElementWrapper.iter_previous_siblings   sE       	=	  	  	  ))))))))))r   c              #   b   K   | j         | V  dS | j                                         E d{V  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   r6   r3   s    r   iter_siblingszElementWrapper.iter_siblings   sI       ;JJJJJ{0022222222222r   c              #   X   K   d}|                                  D ]}|r|V  || k    rd}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)rB   )r'   foundsiblings      r   iter_next_siblingsz!ElementWrapper.iter_next_siblings   sR       ))++ 	 	G $		 	r   c              #      K   d}t          | j                  D ]+\  }} t          |           || ||| j                  }|V  ,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_childs       r   r6   zElementWrapper.iter_children   sm       '(;<< 	 	NA{DJJDE!%!68 8 8E KKKK		 	r   c              #      K   t          | g          g}|r\t          |d         d          }||                                 n+|V  |                    |                                           |Zd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appendr6   )r'   stackelements      r   iter_subtreezElementWrapper.iter_subtree   s        tf 	659d++G		W2244555  	6 	6 	6 	6 	6r   c                     d | D             S )Nc                     g | ]<}t          |d           r|gnt          |          D ]}|j        	|j        |j        =S )test)r   r   pseudo_elementnever_matchesrW   ).0selectorcompiled_selectors      r   
<listcomp>z+ElementWrapper._compile.<locals>.<listcomp>   sr     1 1 1%h77 5

*8441 1 " !/7!/ 8 "
 8777r   r/   )	selectorss    r   _compilezElementWrapper._compile   s#    1 1%1 1 1 	1r   c                 `     t           f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   .   K   | ]} |          V  d S r    r/   )rZ   rW   r'   s     r   	<genexpr>z)ElementWrapper.matches.<locals>.<genexpr>   s+      CC$44::CCCCCCr   )anyr_   r'   r^   s   ` r   matcheszElementWrapper.matches   s4     CCCC$--	*B*BCCCCCCr   c                    |                      |          t                    dk    r(t          d         |                                           S |r f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   T   K   | ]!t          fd D                       V  "dS )c              3   .   K   | ]} |          V  d S r    r/   )rZ   rW   rS   s     r   rb   z5ElementWrapper.query_all.<locals>.<genexpr>.<genexpr>  s+      77ttG}}777777r   N)rc   )rZ   rS   testss    @r   rb   z+ElementWrapper.query_all.<locals>.<genexpr>  s[       9 9#77777777799 9 9 9 9 9r   r/   )r_   lenfilterrT   rN   )r'   r^   ri   s     @r   	query_allzElementWrapper.query_all   s     i((u::??%(D$5$5$7$7888 	9 9 9 9'+'8'8':':9 9 9 9 88Or   c                 2    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)rO   rl   rd   s     r   queryzElementWrapper.query  s     NDNI.555r   c                 $    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                 F    g | ]}t          |j        t                    |S r/   )
isinstancetagstr)rZ   rS   s     r   r]   z1ElementWrapper.etree_children.<locals>.<listcomp>,  s:     - - -'+s++-- - -r   )r!   r3   s    r   r"   zElementWrapper.etree_children!  s&    - -#'#5- - - 	-r   c                 R    t          | j        j                  \  }}|| j        d<   |S )z,The local name of this element, as a string.namespace_url_split_etree_tagr!   rr   __dict__r'   ru   
local_names      r   rz   zElementWrapper.local_name0  s.     %5T5G5K$L$L!z)6o&r   c                 R    t          | j        j                  \  }}|| j        d<   |S )z/The namespace URL of this element, as a string.rz   rv   ry   s      r   ru   zElementWrapper.namespace_url7  s.     %5T5G5K$L$L!z&0l#r   c                 6    | j                             d          S )z$The ID of this element, as a string.id)r!   getr3   s    r   r}   zElementWrapper.id>  s     !%%d+++r   c                 l    t          t          | j                            dd                              S )z:The classes of this element, as a :class:`set` of strings.class )setr   r!   r~   r3   s    r   classeszElementWrapper.classesC  s.     #D$6$:$:7B$G$GHHIIIr   c                 &   | j                             d          }|t          |          S | j        p
| j        dk    }|r+| j                             d          }|t          |          S | j        | j        j        S |r}d}| j                             d          }|D ]M}|                    dd          }t          |          dk    r"t          |                    d	                    }N|t          |          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-equivr   zcontent-languagecontent)
r!   r~   r   r   ru   r   r   rN   _parse_content_languager$   )r'   xml_langis_htmlr   r   iteratormeta
http_equivs           r   r   zElementWrapper.langH  s6    %))*VWWx(((! A"@@ 	  	)%))&11D"4(((;";## 	5#)../STTH  T T!XXlB77
z**.@@@'>txx	?R?R'S'S$+"#3444&t'FGGM2Mr   c                     | j         dS d}d| j         j        j        |k    o]| j         j                            d          d uo<| j        j        k    p,t	          fd|                                 D                       }|p| j         j        S )NFz&{http://www.w3.org/1999/xhtml}fieldsetz${http://www.w3.org/1999/xhtml}legenddisabledc              3   8   K   | ]}|j         j        k    V  d S r    )r!   rr   )rZ   rE   legends     r   rb   z6ElementWrapper.in_disabled_fieldset.<locals>.<genexpr>n  sJ       8B 8B )-78B 8B 8B 8B 8B 8Br   )r   r!   rr   r~   rc   r@   in_disabled_fieldset)r'   fieldsetdisabled_fieldsetr   s      @r   r   z#ElementWrapper.in_disabled_fieldsete  s    ;5;7K%)X5 CK%))*55TAC"&&0 BC 8B 8B 8B 8B#'#>#>#@#@8B 8B 8B 5B 5B 	 !DDK$DDr   r    )T)#__name__
__module____qualname____doc__classmethodr   r   r   r(   r-   r0   r4   r7   propertyr9   r;   r>   r@   rB   rF   r6   rT   staticmethodr_   re   rl   rn   r   r"   rz   ru   r}   r   r   r   r/   r   r   r
   r
   	   s       
 
 N N N [N$ M M M [M R R R [R 59' ' ' '87 7 7
# # #6 6 6( ( ( 
 
 X
 ' ' X'" " "(* * *&
3 
3 
3    6 6 62 1 1 \1D D D  06 6 6 - - _-   _   _ , , _, J J _J N N _N8 E E _E E Er   r
   c                     |                      d          }|dk    s| d         dk    rd| fS | d|         | |dz   d          fS )N}rM   r   {r   r   )rfind)rr   positions     r   rw   rw   t  sO    yy~~H2~~Q33w1X:HQJKK 000r   c                 n    | .d| vr,t          |           }t          |          dk    r|d         S d S d S d S )N,r   r   )r   rj   )valuepartss     r   r   r   |  sJ    S-- ''u::??8O --?r   N)	functoolsr   warningsr   webencodingsr   compilerr   r   r
   rw   r   r/   r   r   <module>r      s    % % % % % %       $ $ $ $ $ $ = = = = = = = =hE hE hE hE hE hE hE hEV1 1 1    r   