
    MhJ                         d dl mZ ddlmZmZmZmZ ddlmZ ddZ	d Z
ddZdd	Zd
 ZddZd Zd ZddZddZddZddZddZd Zd Zd Z	 	 ddZdS )    )chain   )AtRuleDeclaration
ParseErrorQualifiedRule)parse_component_value_listFc                 j    t          | t                    rt          | |          } t          |           S )aq  Iterate component values out of string or component values iterable.

    :type input: :obj:`str` or :term:`iterable`
    :param input: A string or an iterable of :term:`component values`.
    :type skip_comments: :obj:`bool`
    :param skip_comments: If the input is a string, ignore all CSS comments.
    :returns: An iterator yielding :term:`component values`.

    )
isinstancestrr	   iter)inputskip_commentss     O/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/tinycss2/parser.py_to_token_iteratorr      s2     % A*5-@@;;    c                 *    | D ]}|j         dvr|c S dS )zReturn the next significant (neither whitespace or comment) token.

    :type tokens: :term:`iterator`
    :param tokens: An iterator yielding :term:`component values`.
    :returns: A :term:`component value`, or :obj:`None`.

    
whitespacecommentN)type)tokenstokens     r   _next_significantr      s9       :666LLL 7 r   c                     t          | |          }t          |          }t          |          }|t          dddd          S |t          |j        |j        dd          S |S )a  Parse a single :diagram:`component value`.

    This is used e.g. for an attribute value
    referred to by ``attr(foo length)``.

    :type input: :obj:`str` or :term:`iterable`
    :param input: A string or an iterable of :term:`component values`.
    :type skip_comments: :obj:`bool`
    :param skip_comments: If the input is a string, ignore all CSS comments.
    :returns:
        A :term:`component value` (that is neither whitespace or comment),
        or a :class:`~tinycss2.ast.ParseError`.

    Nr   emptyInput is emptyextra-inputzGot more than one token)r   r   r   source_linesource_column)r   r   r   firstseconds        r   parse_one_component_valuer#   #   sv      }55Ff%%Ev&&F}!Q)9::: 4m%' ' 	' r   c                     t          | |          }t          |          }|t          dddd          S t          ||          S )a?  Parse a single :diagram:`declaration`.

    This is used e.g. for a declaration in an `@supports
    <https://drafts.csswg.org/css-conditional/#at-supports>`_ test.

    :type input: :obj:`str` or :term:`iterable`
    :param input: A string or an iterable of :term:`component values`.
    :type skip_comments: :obj:`bool`
    :param skip_comments: If the input is a string, ignore all CSS comments.
    :returns:
        A :class:`~tinycss2.ast.Declaration`
        or :class:`~tinycss2.ast.ParseError`.

    Any whitespace or comment before the ``:`` colon is dropped.

    Nr   r   r   )r   r   r   _parse_declaration)r   r   r   first_tokens       r   parse_one_declarationr'   ?   sK    "  }55F#F++K!Q)9:::k6222r   c                 8    | D ]}|dk    r d S |r	|dk    r d S d S )N;} )r   nestedr   s      r   _consume_remnantsr-   W   sC      C<<FF 	FF	 r   Tc           	         | }|j         dk    r5t          ||           t          |j        |j        dd|j          d          S t          |          }|,t          ||           t          |j        |j        dd          S |dk    r,t          ||           t          |j        |j        dd          S g }d	}d
}d
}t          |          D ]j\  }	}
|d	k    r|
dk    rd}|	}n?|dk    r|
j         dk    r|
j        dk    rd}n |
j         dvrd	}|
j         dk    r|rd}nd}nd}|                    |
           k|dk    r||d= |r|rt          |j        |j        dd          S t          |j        |j        |j
        |j        ||dk              S )a  Parse a declaration.

    Consume :obj:`tokens` until the end of the declaration or the first error.

    :type first_token: :term:`component value`
    :param first_token: The first component value of the rule.
    :type tokens: :term:`iterator`
    :param tokens: An iterator yielding :term:`component values`.
    :type nested: :obj:`bool`
    :param nested: Whether the declaration is nested or top-level.
    :returns:
        A :class:`~tinycss2.ast.Declaration`
        or :class:`~tinycss2.ast.ParseError`.

    identinvalidz+Expected <ident> for declaration name, got .Nz,Expected ':' after declaration name, got EOF:z6Expected ':' after declaration name, got {colon.type}.valueF!bang	importantr   {} blockTzDeclaration contains {} block)r   r-   r   r   r    r   	enumeratelower_valueappendr   r3   )r&   r   r,   namecolonr3   statecontains_non_whitespacecontains_simple_blockir   bang_positions               r   r%   r%   _   s,     DyG&&)))d0)F$)FFFH H 	H f%%E}&&)))d0):< < 	< 
#&&)))u2IDF F 	F EE#!f%%  5GEMMvoo%*"7"7%44EEZ888EzZ''* 3,0)).2++*.'U-..!  -!8 -u2I+- - 	- $,dj$:Ju#% % %r   c                 T   g }g }| dk    rO| j         dk    rD|D ]A}|dk    r|                    |            n#|                    |           |j         dk    r nBt          | t          |          d          }|j         dk    r|S t	          |||          }t          | |dd          S )z#Consume declaration or nested rule.r)   r7   T)r,   declaration)
stop_tokenr,   )r   r:   r%   r   r   _consume_qualified_rule)r&   r   declaration_tokenssemicolon_tokenr   rC   s         r   _consume_blocks_contentrH      s    Ock.*<< 	 	E||&&u---%%e,,,zZ'' ($T,--d< < <K=(()?FCC&{FsSWXXXXr   c                     g }|D ]}|dk    r n|                     |            t          | t          |                    S )zLike :func:`_parse_declaration`, but stop at the first ``;``.

    Deprecated, use :func:`_consume_blocks_content` instead.

    r)   )r:   r%   r   )r&   r   other_declaration_tokensr   s       r   _consume_declaration_in_listrK      sW      " / /C<<E ''....k40H+I+IJJJr   c                 p   t          | |          }g }|D ]}|j        dk    r|s|                    |           %|j        dk    r|s|                    |           H|j        dk    r$|                    t          ||                     w|dk    r#|                    t	          ||                     |S )u  Parse a block’s contents.

    This is used e.g. for the :attr:`~tinycss2.ast.QualifiedRule.content`
    of a style rule or ``@page`` rule, or for the ``style`` attribute of an
    HTML element.

    In contexts that don’t expect any at-rule and/or qualified rule,
    all :class:`~tinycss2.ast.AtRule` and/or
    :class:`~tinycss2.ast.QualifiedRule` objects should simply be rejected as
    invalid.

    :type input: :obj:`str` or :term:`iterable`
    :param input: A string or an iterable of :term:`component values`.
    :type skip_comments: :obj:`bool`
    :param skip_comments:
        Ignore CSS comments at the top-level of the list.
        If the input is a string, ignore all comments.
    :type skip_whitespace: :obj:`bool`
    :param skip_whitespace:
        Ignore whitespace at the top-level of the list.
        Whitespace is still preserved
        in the :attr:`~tinycss2.ast.Declaration.value` of declarations
        and the :attr:`~tinycss2.ast.AtRule.prelude`
        and :attr:`~tinycss2.ast.AtRule.content` of at-rules.
    :returns:
        A list of
        :class:`~tinycss2.ast.Declaration`,
        :class:`~tinycss2.ast.AtRule`,
        :class:`~tinycss2.ast.QualifiedRule`,
        :class:`~tinycss2.ast.Comment` (if ``skip_comments`` is false),
        :class:`~tinycss2.ast.WhitespaceToken`
        (if ``skip_whitespace`` is false),
        and :class:`~tinycss2.ast.ParseError` objects

    r   r   
at-keywordr)   )r   r   r:   _consume_at_rulerH   r   r   skip_whitespacer   resultr   s         r   parse_blocks_contentsrR      s    H  }55FF 
B 
B:%%" %e$$$Z9$$  %e$$$Z<''MM*5&99::::c\\MM1%@@AAAMr   c                 p   t          | |          }g }|D ]}|j        dk    r|s|                    |           %|j        dk    r|s|                    |           H|j        dk    r$|                    t          ||                     w|dk    r#|                    t	          ||                     |S )u  Parse a :diagram:`declaration list` (which may also contain at-rules).

    Deprecated and removed from CSS Syntax Level 3. Use
    :func:`parse_blocks_contents` instead.

    This is used e.g. for the :attr:`~tinycss2.ast.QualifiedRule.content`
    of a style rule or ``@page`` rule, or for the ``style`` attribute of an
    HTML element.

    In contexts that don’t expect any at-rule, all
    :class:`~tinycss2.ast.AtRule` objects should simply be rejected as invalid.

    :type input: :obj:`str` or :term:`iterable`
    :param input: A string or an iterable of :term:`component values`.
    :type skip_comments: :obj:`bool`
    :param skip_comments:
        Ignore CSS comments at the top-level of the list.
        If the input is a string, ignore all comments.
    :type skip_whitespace: :obj:`bool`
    :param skip_whitespace:
        Ignore whitespace at the top-level of the list.
        Whitespace is still preserved
        in the :attr:`~tinycss2.ast.Declaration.value` of declarations
        and the :attr:`~tinycss2.ast.AtRule.prelude`
        and :attr:`~tinycss2.ast.AtRule.content` of at-rules.
    :returns:
        A list of
        :class:`~tinycss2.ast.Declaration`,
        :class:`~tinycss2.ast.AtRule`,
        :class:`~tinycss2.ast.Comment` (if ``skip_comments`` is false),
        :class:`~tinycss2.ast.WhitespaceToken`
        (if ``skip_whitespace`` is false),
        and :class:`~tinycss2.ast.ParseError` objects

    r   r   rM   r)   )r   r   r:   rN   rK   rO   s         r   parse_declaration_listrT      s    H  }55FF 
G 
G:%%" %e$$$Z9$$  %e$$$Z<''MM*5&99::::c\\MM6ufEEFFFMr   c                     t          | |          }t          |          }|t          dddd          S t          ||          }t          |          }|$t          |j        |j        dd|j        z            S |S )a  Parse a single :diagram:`qualified rule` or :diagram:`at-rule`.

    This would be used e.g. by `insertRule()
    <https://drafts.csswg.org/cssom/#dom-cssstylesheet-insertrule>`_
    in an implementation of CSSOM.

    :type input: :obj:`str` or :term:`iterable`
    :param input: A string or an iterable of :term:`component values`.
    :type skip_comments: :obj:`bool`
    :param skip_comments:
        If the input is a string, ignore all CSS comments.
    :returns:
        A :class:`~tinycss2.ast.QualifiedRule`,
        :class:`~tinycss2.ast.AtRule`,
        or :class:`~tinycss2.ast.ParseError` objects.

    Any whitespace or comment before or after the rule is dropped.

    Nr   r   r   r   z4Expected a single rule, got %s after the first rule.)r   r   r   _consume_ruler   r    r   )r   r   r   r!   rulenexts         r   parse_one_rulerY   4  s    (  }55Ff%%E}!Q)9:::''DV$$Dd0-BTYNP P 	P Kr   c                    t          | |          }g }|D ]k}|j        dk    r|s|                    |           %|j        dk    r|s|                    |           H|                    t          ||                     l|S )a  Parse a non-top-level :diagram:`rule list`.

    Deprecated and removed from CSS Syntax. Use :func:`parse_blocks_contents`
    instead.

    This is used for parsing the :attr:`~tinycss2.ast.AtRule.content`
    of nested rules like ``@media``.
    This differs from :func:`parse_stylesheet` in that
    top-level ``<!--`` and ``-->`` tokens are not ignored.

    :type input: :obj:`str` or :term:`iterable`
    :param input: A string or an iterable of :term:`component values`.
    :type skip_comments: :obj:`bool`
    :param skip_comments:
        Ignore CSS comments at the top-level of the list.
        If the input is a string, ignore all comments.
    :type skip_whitespace: :obj:`bool`
    :param skip_whitespace:
        Ignore whitespace at the top-level of the list.
        Whitespace is still preserved
        in the :attr:`~tinycss2.ast.QualifiedRule.prelude`
        and the :attr:`~tinycss2.ast.QualifiedRule.content` of rules.
    :returns:
        A list of
        :class:`~tinycss2.ast.QualifiedRule`,
        :class:`~tinycss2.ast.AtRule`,
        :class:`~tinycss2.ast.Comment` (if ``skip_comments`` is false),
        :class:`~tinycss2.ast.WhitespaceToken`
        (if ``skip_whitespace`` is false),
        and :class:`~tinycss2.ast.ParseError` objects.

    r   r   r   r   r:   rV   rO   s         r   parse_rule_listr\   V  s    B  }55FF 8 8:%%" %e$$$Z9$$  %e$$$MM-v667777Mr   c                    t          | |          }g }|D ]o}|j        dk    r|s|                    |           %|j        dk    r|s|                    |           H|dvr#|                    t          ||                     p|S )a  Parse :diagram:`stylesheet` from text.

    This is used e.g. for a ``<style>`` HTML element.

    This differs from :func:`parse_rule_list` in that
    top-level ``<!--`` and ``-->`` tokens are ignored.
    This is a legacy quirk for the ``<style>`` HTML element.

    :type input: :obj:`str` or :term:`iterable`
    :param input: A string or an iterable of :term:`component values`.
    :type skip_comments: :obj:`bool`
    :param skip_comments:
        Ignore CSS comments at the top-level of the stylesheet.
        If the input is a string, ignore all comments.
    :type skip_whitespace: :obj:`bool`
    :param skip_whitespace:
        Ignore whitespace at the top-level of the stylesheet.
        Whitespace is still preserved
        in the :attr:`~tinycss2.ast.QualifiedRule.prelude`
        and the :attr:`~tinycss2.ast.QualifiedRule.content` of rules.
    :returns:
        A list of
        :class:`~tinycss2.ast.QualifiedRule`,
        :class:`~tinycss2.ast.AtRule`,
        :class:`~tinycss2.ast.Comment` (if ``skip_comments`` is false),
        :class:`~tinycss2.ast.WhitespaceToken`
        (if ``skip_whitespace`` is false),
        and :class:`~tinycss2.ast.ParseError` objects.

    r   r   )z<!--z-->r[   rO   s         r   parse_stylesheetr^     s    >  }55FF 8 8:%%" %e$$$Z9$$  %e$$$/))MM-v66777Mr   c                 X    | j         dk    rt          | |          S t          | |          S )a  Parse a qualified rule or at-rule.

    Consume just enough of :obj:`tokens` for this rule.

    :type first_token: :term:`component value`
    :param first_token: The first component value of the rule.
    :type tokens: :term:`iterator`
    :param tokens: An iterator yielding :term:`component values`.
    :returns:
        A :class:`~tinycss2.ast.QualifiedRule`,
        :class:`~tinycss2.ast.AtRule`,
        or :class:`~tinycss2.ast.ParseError`.

    rM   )r   rN   rE   )r&   r   s     r   rV   rV     s2     <''V444";777r   c                     g }d}|D ]3}|j         dk    r	|j        } n|dk    r n|                    |           4t          | j        | j        | j        | j        ||          S )a  Parse an at-rule.

    Consume just enough of :obj:`tokens` for this rule.

    :type at_keyword: :class:`AtKeywordToken`
    :param at_keyword: The at-rule keyword token starting this rule.
    :type tokens: :term:`iterator`
    :param tokens: An iterator yielding :term:`component values`.
    :type nested: :obj:`bool`
    :param nested: Whether the at-rule is nested or top-level.
    :returns:
        A :class:`~tinycss2.ast.QualifiedRule`,
        or :class:`~tinycss2.ast.ParseError`.

    Nr7   r)   )r   contentr:   r   r   r    r3   r9   )
at_keywordr   preludera   r   s        r   rN   rN     s      GG  :## mGEc\\Eu
 8*:J2 2 2r   c                 @    t          | j        | j        d| d          S )z6Create rule parse error raised because of given token.r0   z. reached before {} block for a qualified rule.)r   r   r    )r   r;   s     r   _rule_errorre     s0    5.	AAAC C Cr   Nc                 D   | |k    rt          | d          S | j        dk    rg }| }nZ| g}|D ]>}||k    rt          |d          c S |j        dk    r|} n,|                    |           ?t          |d         d          S t          | j        | j        ||j                  S )a  Consume a qualified rule.

    Consume just enough of :obj:`tokens` for this rule.

    :type first_token: :term:`component value`
    :param first_token: The first component value of the rule.
    :type tokens: :term:`iterator`
    :param tokens: An iterator yielding :term:`component values`.
    :type nested: :obj:`bool`
    :param nested: Whether the rule is nested or top-level.
    :type stop_token: :class:`~tinycss2.ast.Node`
    :param stop_token: A token that ends rule parsing when met.

    z
Stop tokenr7   EOF)re   r   r:   r   r   r    ra   )r&   r   r,   rD   rc   blockr   s          r   rE   rE     s      j  ;555:%%- 
	3 
	3E
"""5,77777zZ'' NN5!!!!wr{E222!:GU]T T Tr   )F)T)FF)FN)	itertoolsr   astr   r   r   r   	tokenizerr	   r   r   r#   r'   r-   r%   rH   rK   rR   rT   rY   r\   r^   rV   rN   re   rE   r+   r   r   <module>rm      s         ? ? ? ? ? ? ? ? ? ? ? ? 1 1 1 1 1 1   
 
 
   83 3 3 30  G% G% G% G%TY Y Y*K K K1 1 1 1h1 1 1 1h   D, , , ,^* * * *Z8 8 8(2 2 2@C C C 9>'+#T #T #T #T #T #Tr   