U
    }g1                     @  s   U d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
mZ ddlZddlmZ er~ddlmZ ddlmZ dd	lmZmZ d
ed< zddlZW n" ek
r   dZed Y nX G dd deZdS )a,  Integration code for CSS selectors using `Soup Sieve <https://facelessuser.github.io/soupsieve/>`_ (pypi: ``soupsieve``).

Acquire a `CSS` object through the `element.Tag.css` attribute of
the starting point of your CSS selector, or (if you want to run a
selector against the entire document) of the `BeautifulSoup` object
itself.

The main advantage of doing this instead of using ``soupsieve``
functions is that you don't need to keep passing the `element.Tag` to be
selected against, since the `CSS` object is permanently scoped to that
`element.Tag`.

    )annotations)
ModuleType)AnycastIterableIteratorOptionalTYPE_CHECKINGN)_NamespaceMapping)	SoupSieve)element)	ResultSetTagOptional[ModuleType]	soupsievezEThe soupsieve package is not installed. CSS selectors cannot be used.c                   @  s   e Zd ZdZd.dddddZddd	d
dZddddddZdddddZd/ddddddddZd0ddddddddZ	d1ddddddd d!d"Z
d2dddddd#d d$d%Zd3ddddd&dd'd(Zd4ddddd)dd*d+Zd5ddddddd,d-ZdS )6CSSa  A proxy object against the ``soupsieve`` library, to simplify its
    CSS selector API.

    You don't need to instantiate this class yourself; instead, use
    `element.Tag.css`.

    :param tag: All CSS selectors run by this object will use this as
        their starting point.

    :param api: An optional drop-in replacement for the ``soupsieve`` module,
        intended for use in unit tests.
    Nzelement.Tagr   )tagapic                 C  s,   |d krt }|d krtd|| _|| _d S )NzLCannot execute CSS selectors because the soupsieve package is not installed.)r   NotImplementedErrorr   r   )selfr   r    r   +/tmp/pip-unpacked-wheel-kgiupv3k/bs4/css.py__init__:   s    zCSS.__init__str)identreturnc                 C  s"   t dkrtdtt| j|S )zEscape a CSS identifier.

        This is a simple wrapper around `soupsieve.escape() <https://facelessuser.github.io/soupsieve/api/#soupsieveescape>`_. See the
        documentation for that function for more information.
        NzMCannot escape CSS identifiers because the soupsieve package is not installed.)r   r   r   r   r   escape)r   r   r   r   r   r   D   s
    z
CSS.escapezOptional[_NamespaceMapping])nsselectr   c                 C  s"   t || jjs|dkr| jj}|S )z%Normalize a dictionary of namespaces.N)
isinstancer   r   r   _namespaces)r   r   r   r   r   r   _nsP   s    zCSS._nszIterable[Tag]zResultSet[Tag])resultsr   c                 C  s   ddl m} |d|S )a?  Normalize a list of results to a py:class:`ResultSet`.

        A py:class:`ResultSet` is more consistent with the rest of
        Beautiful Soup's API, and :py:meth:`ResultSet.__getattr__` has
        a helpful error message if you try to treat a list of results
        as a single result (a common mistake).
        r   )r   N)bs4r   )r   r"   r   r   r   r   _rs[   s    	zCSS._rsr   intr   r   )r   
namespacesflagskwargsr   c                 K  s   | j j|| |||f|S )a  Pre-compile a selector and return the compiled object.

        :param selector: A CSS selector.

        :param namespaces: A dictionary mapping namespace prefixes
           used in the CSS selector to namespace URIs. By default,
           Beautiful Soup will use the prefixes it encountered while
           parsing the document.

        :param flags: Flags to be passed into Soup Sieve's
            `soupsieve.compile() <https://facelessuser.github.io/soupsieve/api/#soupsievecompile>`_ method.

        :param kwargs: Keyword arguments to be passed into Soup Sieve's
           `soupsieve.compile() <https://facelessuser.github.io/soupsieve/api/#soupsievecompile>`_ method.

        :return: A precompiled selector object.
        :rtype: soupsieve.SoupSieve
        )r   compiler!   r   r   r&   r'   r(   r   r   r   r)   h   s    zCSS.compilezelement.Tag | Nonec                 K  s    | j j|| j| |||f|S )a  Perform a CSS selection operation on the current Tag and return the
        first result, if any.

        This uses the Soup Sieve library. For more information, see
        that library's documentation for the `soupsieve.select_one() <https://facelessuser.github.io/soupsieve/api/#soupsieveselect_one>`_ method.

        :param selector: A CSS selector.

        :param namespaces: A dictionary mapping namespace prefixes
           used in the CSS selector to namespace URIs. By default,
           Beautiful Soup will use the prefixes it encountered while
           parsing the document.

        :param flags: Flags to be passed into Soup Sieve's
            `soupsieve.select_one() <https://facelessuser.github.io/soupsieve/api/#soupsieveselect_one>`_ method.

        :param kwargs: Keyword arguments to be passed into Soup Sieve's
           `soupsieve.select_one() <https://facelessuser.github.io/soupsieve/api/#soupsieveselect_one>`_ method.
        )r   
select_oner   r!   r*   r   r   r   r+      s      
 zCSS.select_onezResultSet[element.Tag])r   r&   limitr'   r(   r   c              	   K  s4   |dkrd}|  | jj|| j| ||||f|S )a  Perform a CSS selection operation on the current `element.Tag`.

        This uses the Soup Sieve library. For more information, see
        that library's documentation for the `soupsieve.select() <https://facelessuser.github.io/soupsieve/api/#soupsieveselect>`_ method.

        :param selector: A CSS selector.

        :param namespaces: A dictionary mapping namespace prefixes
            used in the CSS selector to namespace URIs. By default,
            Beautiful Soup will pass in the prefixes it encountered while
            parsing the document.

        :param limit: After finding this number of results, stop looking.

        :param flags: Flags to be passed into Soup Sieve's
            `soupsieve.select() <https://facelessuser.github.io/soupsieve/api/#soupsieveselect>`_ method.

        :param kwargs: Keyword arguments to be passed into Soup Sieve's
           `soupsieve.select() <https://facelessuser.github.io/soupsieve/api/#soupsieveselect>`_ method.
        Nr   )r$   r   r   r   r!   r   r   r&   r,   r'   r(   r   r   r   r      s      
  z
CSS.selectzIterator[element.Tag]c                 K  s"   | j j|| j| ||||f|S )aU  Perform a CSS selection operation on the current `element.Tag`.

        This uses the Soup Sieve library. For more information, see
        that library's documentation for the `soupsieve.iselect()
        <https://facelessuser.github.io/soupsieve/api/#soupsieveiselect>`_
        method. It is the same as select(), but it returns a generator
        instead of a list.

        :param selector: A string containing a CSS selector.

        :param namespaces: A dictionary mapping namespace prefixes
            used in the CSS selector to namespace URIs. By default,
            Beautiful Soup will pass in the prefixes it encountered while
            parsing the document.

        :param limit: After finding this number of results, stop looking.

        :param flags: Flags to be passed into Soup Sieve's
            `soupsieve.iselect() <https://facelessuser.github.io/soupsieve/api/#soupsieveiselect>`_ method.

        :param kwargs: Keyword arguments to be passed into Soup Sieve's
           `soupsieve.iselect() <https://facelessuser.github.io/soupsieve/api/#soupsieveiselect>`_ method.
        )r   iselectr   r!   r-   r   r   r   r.      s      
  zCSS.iselectzOptional[element.Tag]c                 K  s    | j j|| j| |||f|S )a  Find the `element.Tag` closest to this one that matches the given selector.

        This uses the Soup Sieve library. For more information, see
        that library's documentation for the `soupsieve.closest()
        <https://facelessuser.github.io/soupsieve/api/#soupsieveclosest>`_
        method.

        :param selector: A string containing a CSS selector.

        :param namespaces: A dictionary mapping namespace prefixes
            used in the CSS selector to namespace URIs. By default,
            Beautiful Soup will pass in the prefixes it encountered while
            parsing the document.

        :param flags: Flags to be passed into Soup Sieve's
            `soupsieve.closest() <https://facelessuser.github.io/soupsieve/api/#soupsieveclosest>`_ method.

        :param kwargs: Keyword arguments to be passed into Soup Sieve's
           `soupsieve.closest() <https://facelessuser.github.io/soupsieve/api/#soupsieveclosest>`_ method.

        )r   closestr   r!   r*   r   r   r   r/      s      
 zCSS.closestboolc              	   K  s&   t t| jj|| j| |||f|S )a  Check whether or not this `element.Tag` matches the given CSS selector.

        This uses the Soup Sieve library. For more information, see
        that library's documentation for the `soupsieve.match()
        <https://facelessuser.github.io/soupsieve/api/#soupsievematch>`_
        method.

        :param: a CSS selector.

        :param namespaces: A dictionary mapping namespace prefixes
            used in the CSS selector to namespace URIs. By default,
            Beautiful Soup will pass in the prefixes it encountered while
            parsing the document.

        :param flags: Flags to be passed into Soup Sieve's
            `soupsieve.match()
            <https://facelessuser.github.io/soupsieve/api/#soupsievematch>`_
            method.

        :param kwargs: Keyword arguments to be passed into SoupSieve's
            `soupsieve.match()
            <https://facelessuser.github.io/soupsieve/api/#soupsievematch>`_
            method.
        )r   r0   r   matchr   r!   r*   r   r   r   r1   	  s      
 z	CSS.matchc              	   K  s&   |  | jj|| j| |||f|S )aN  Filter this `element.Tag`'s direct children based on the given CSS selector.

        This uses the Soup Sieve library. It works the same way as
        passing a `element.Tag` into that library's `soupsieve.filter()
        <https://facelessuser.github.io/soupsieve/api/#soupsievefilter>`_
        method. For more information, see the documentation for
        `soupsieve.filter()
        <https://facelessuser.github.io/soupsieve/api/#soupsievefilter>`_.

        :param namespaces: A dictionary mapping namespace prefixes
            used in the CSS selector to namespace URIs. By default,
            Beautiful Soup will pass in the prefixes it encountered while
            parsing the document.

        :param flags: Flags to be passed into Soup Sieve's
            `soupsieve.filter()
            <https://facelessuser.github.io/soupsieve/api/#soupsievefilter>`_
            method.

        :param kwargs: Keyword arguments to be passed into SoupSieve's
            `soupsieve.filter()
            <https://facelessuser.github.io/soupsieve/api/#soupsievefilter>`_
            method.
        )r$   r   filterr   r!   r*   r   r   r   r2   /  s      
 z
CSS.filter)N)Nr   )Nr   )Nr   r   )Nr   r   )Nr   )Nr   )Nr   )__name__
__module____qualname____doc__r   r   r!   r$   r)   r+   r   r.   r/   r1   r2   r   r   r   r   r   ,   s8   
    !   (   &  #  )  r   )r6   
__future__r   typesr   typingr   r   r   r   r   r	   warningsZbs4._typingr
   r   r   r#   r   Zbs4.elementr   r   __annotations__ImportErrorwarnobjectr   r   r   r   r   <module>   s$    
