
    +gB5                     X    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y
)    )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"y) 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                 *    | j                  ||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      g/home/viktor/gitlab-persoonlijk/factuur-applicatie/venv/lib/python3.12/site-packages/cssselect2/tree.pyfrom_xml_rootzElementWrapper.from_xml_root   s    " ~~d$4u~MM    c                 *    | j                  ||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Lr   c                 V    t        |d      r|j                         } | |d dd ||      S )Ngetrootr   )parentindexpreviousr   r   )hasattrr   )r   r   r   r   s       r   r   zElementWrapper._from_root2   s5    4#<<>DQ-@PR 	Rr   c                     || _         || _        || _        ||j                  | _        n|g| _        || _        || _        || _        d | _        d | _	        y 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__:   sh     +  !
 #)"7"7D#0/D 
 0*:' "&r   c                 d    t        |       t        |      u xr | j                  |j                  k(  S r    )typer!   r'   others     r   __eq__zElementWrapper.__eq__V   s0    J$u+% 6%"5"55	7r   c                     | |k(   S r     r+   s     r   __ne__zElementWrapper.__ne__[   s    EM""r   c                 B    t        t        |       | j                  f      S r    )hashr*   r!   r'   s    r   __hash__zElementWrapper.__hash__^   s    T$Z!3!3455r   c              #   @   K   | j                         E d {    y 7 wr    )iter_childrenr3   s    r   __iter__zElementWrapper.__iter__a   s     %%'''s   c                     | j                   8| j                  dn$g | j                  j                  | j                  | _         | 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.

        r/   )r%   r   	ancestorsr3   s    r   r9   zElementWrapper.ancestorsd   sF     ??"kk)/T1F1F/T/T Or   c                     | j                   8| j                  dn$g | j                  j                  | j                  | _         | j                   S )u   Tuple of previous siblings.

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

        r/   )r&   r   previous_siblingsr3   s    r   r;   z ElementWrapper.previous_siblingsq   sM     ""*mm+A$--11A4==A # &&&r   c              #   X   K   t        dt               | j                  E d{    y7 w)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   s'      	5	  >>!!    *(*c              #   X   K   t        dt               | j                  E d{    y7 w)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   s)      	=	  ))))r?   c              #   v   K   | j                   |  y| j                   j                         E d{    y7 w)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   s-      ;;J{{00222s   /979c              #   V   K   d}| j                         D ]  }|r| || k(  sd} yw)u   Iterate over next siblings.

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

        FTN)rC   )r'   foundsiblings      r   iter_next_siblingsz!ElementWrapper.iter_next_siblings   s9      ))+ 	G$		s   !))c              #      K   d}t        | j                        D ])  \  }} t        |       || ||| j                        }| + yw)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   sT      '(;(;< 	NA{DJDE!%!6!68E K		s   AAc              #      K   t        | g      g}|rIt        |d   d      }||j                          n#| |j                  |j	                                |rHyyw)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   sU       tf59d+G		W2245 s   AAAc                     | D cg c]E  }t        |d      r|gn
t        |      D ]&  }|j                  |j                  s|j                  ( G c}}S c c}}w )Ntest)r   r   pseudo_elementnever_matchesrW   )	selectorsselectorcompiled_selectors      r   _compilezElementWrapper._compile   si     &1%h7
*841 " !//7!// ""1"1 	1 1s   A
Ac                 J     t         fd j                  |      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   | ]  } |        y wr    r/   ).0rW   r'   s     r   	<genexpr>z)ElementWrapper.matches.<locals>.<genexpr>   s     C$4:C   )anyr]   r'   rZ   s   ` r   matcheszElementWrapper.matches   s     C$--	*BCCCr   c                     | j                  |      t              dk(  rt        d   | j                               S |rfd| j                         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   L   K   | ]  t        fd D              r  yw)c              3   .   K   | ]  } |        y wr    r/   )r`   rW   rT   s     r   ra   z5ElementWrapper.query_all.<locals>.<genexpr>.<genexpr>  s     7tG}7rb   N)rc   )r`   rT   testss    @r   ra   z+ElementWrapper.query_all.<locals>.<genexpr>  s'      9#777 9s    $r/   )r]   lenfilterrU   rO   )r'   rZ   ri   s     @r   	query_allzElementWrapper.query_all   sa     i(u:?%(D$5$5$7889'+'8'8':9 9 8Or   c                 4    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)rP   rl   rd   s     r   queryzElementWrapper.query  s     NDNNI.55r   c                 t    | j                   D cg c]  }t        |j                  t              r|  c}S c c}w )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.

        )r!   
isinstancetagstr)r'   rT   s     r   r"   zElementWrapper.etree_children!  s8     $(#5#5-'++s+ - 	- -s   #5c                 z    t        | j                  j                        \  }}|| j                  t	        d      <   |S )z,The local name of this element, as a string.namespace_url_split_etree_tagr!   rq   __dict__rr   r'   rt   
local_names      r   ry   zElementWrapper.local_name0  s8     %5T5G5G5K5K$L!z.;c/*+r   c                 z    t        | j                  j                        \  }}|| j                  t	        d      <   |S )z/The namespace URL of this element, as a string.ry   ru   rx   s      r   rt   zElementWrapper.namespace_url7  s8     %5T5G5G5K5K$L!z+5c,'(r   c                 8    | j                   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                 ^    t        t        | j                  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$6$:$:7B$GHIIr   c                 2   | j                   j                  d      }|t        |      S | j                  xs | j                  dk(  }|r(| j                   j                  d      }|t        |      S | j
                  | j
                  j                  S |rld}| j                   j                  d      }|D ]=  }|j                  dd      }t        |      dk(  s$t        |j                  d	            }? |t        |      S t        | j                        xs 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   rt   r   r   rO   _parse_content_languager$   )r'   xml_langis_htmlr   r   iteratormeta
http_equivs           r   r   zElementWrapper.langH  s"    %%))*VWx((!! A"@@ 	 %%))&1D"4((;;";;####))../STH  T!XXlB7
z*.@@'>txx	?R'S$T  +"#344&t'F'FGM2Mr   c                 p   | j                   yd}d| j                   j                  j                  |k(  xrf | j                   j                  j                  d      d uxr= | j                  j                  k7  xs" t	        fd| j                         D              }|xs | j                   j                  S )NFz&{http://www.w3.org/1999/xhtml}fieldsetz${http://www.w3.org/1999/xhtml}legenddisabledc              3   P   K   | ]  }|j                   j                  k(    y wr    )r!   rq   )r`   rF   legends     r   ra   z6ElementWrapper.in_disabled_fieldset.<locals>.<genexpr>n  s,      8B ))--78Bs   #&)r   r!   rq   r}   rc   rA   in_disabled_fieldset)r'   fieldsetdisabled_fieldsetr   s      @r   r   z#ElementWrapper.in_disabled_fieldsete  s    ;;;7KK%%))X5 CKK%%))*5TAC""&&&0 BC 8B#'#>#>#@8B 5B 	 !DDKK$D$DDr   r    )T)#__name__
__module____qualname____doc__classmethodr   r   r   r(   r-   r0   r4   r7   propertyr9   r;   r>   rA   rC   rG   r6   rU   staticmethodr]   re   rl   rn   r   r"   ry   rt   r|   r   r   r   r/   r   r   r
   r
   	   sc   
 N N$ M M R R 59'87
#6( 
 
 ' '"(*&
362 1 1D06 - -     , , J J N N8 E Er   r
   c                 d    | j                  d      }|dk(  rd| fS | d   dk(  sJ | d| | |dz   d  fS )N}rN   r   r   {r   )rfind)rq   positions     r   rv   rv   t  sK    yy~H2~3w1v}}1XHQJK 000r   c                 P    | $d| vrt        |       }t        |      dk(  r|d   S y y y )N,r   r   )r   rj   )valuepartss     r   r   r   }  s:    S- 'u:?8O  .r   N)	functoolsr   warningsr   webencodingsr   compilerr   r   r
   rv   r   r/   r   r   <module>r      s*    %  $ =hE hEV1r   