3
OXPmq                 @   s   d Z ddlmZ ddlmZmZ ddlm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mZ G d	d
 d
eZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdd ZG dd deZdS )z
    tinycss.css21
    -------------

    Parser for CSS 2.1
    http://www.w3.org/TR/CSS21/syndata.html

    :copyright: (c) 2012 by Simon Sapin.
    :license: BSD, see LICENSE for more details.
    )unicode_literals)chainislice   )decode)	TokenList)tokenize_grouped)strip_whitespaceremove_whitespacesplit_on_commavalidate_valuevalidate_blockvalidate_any
ParseErrorc               @   s    e Zd ZdZdd Zdd ZdS )
Stylesheetal  
    A parsed CSS stylesheet.

    .. attribute:: rules

        A mixed list, in source order, of :class:`RuleSet` and various
        at-rules such as :class:`ImportRule`, :class:`MediaRule`
        and :class:`PageRule`.
        Use their :obj:`at_keyword` attribute to distinguish them.

    .. attribute:: errors

        A list of :class:`~.parsing.ParseError`. Invalid rules and declarations
        are ignored, with the details logged in this list.

    .. attribute:: encoding

        The character encoding that was used to decode the stylesheet
        from bytes, or ``None`` for Unicode stylesheets.

    c             C   s   || _ || _|| _d S )N)ruleserrorsencoding)selfr   r   r    r   M/var/www/html/enquirykeeper_venv/lib/python3.6/site-packages/tinycss/css21.py__init__>   s    zStylesheet.__init__c             C   s   dj | t| jt| jS )Nz-<{0.__class__.__name__} {1} rules {2} errors>)formatlenr   r   )r   r   r   r   __repr__C   s    zStylesheet.__repr__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   (   s   r   c               @   s    e Zd ZdZdd Zdd ZdS )AtRulea  
    An unparsed at-rule.

    .. attribute:: at_keyword

        The normalized (lower-case) at-keyword as a string. Eg: ``'@page'``

    .. attribute:: head

        The part of the at-rule between the at-keyword and the ``{``
        marking the body, or the ``;`` marking the end of an at-rule without
        a body.  A :class:`~.token_data.TokenList`.

    .. attribute:: body

        The content of the body between ``{`` and ``}`` as a
        :class:`~.token_data.TokenList`, or ``None`` if there is no body
        (ie. if the rule ends with ``;``).

    The head was validated against the core grammar but **not** the body,
    as the body might contain declarations. In case of an error in a
    declaration, parsing should continue from the next declaration.
    The whole rule should not be ignored as it would be for an error
    in the head.

    These at-rules are expected to be parsed further before reaching
    the user API.

    c             C   s6   || _ t|| _|d k	r t|n|| _|| _|| _d S )N)
at_keywordr   headbodylinecolumn)r   r    r!   r"   r#   r$   r   r   r   r   f   s
    
zAtRule.__init__c             C   s
   dj | S )Nz;<{0.__class__.__name__} {0.line}:{0.column} {0.at_keyword}>)r   )r   r   r   r   r   m   s    zAtRule.__repr__N)r   r   r   r   r   r   r   r   r   r   r   H   s   r   c               @   s$   e Zd ZdZdZdd Zdd ZdS )RuleSeta  A ruleset.

    .. attribute:: at_keyword

        Always ``None``. Helps to tell rulesets apart from at-rules.

    .. attribute:: selector

        The selector as a :class:`~.token_data.TokenList`.
        In CSS 3, this is actually called a selector group.

        ``rule.selector.as_css()`` gives the selector as a string.
        This string can be used with *cssselect*, see :ref:`selectors3`.

    .. attribute:: declarations

        The list of :class:`Declaration`, in source order.

    Nc             C   s    t || _|| _|| _|| _d S )N)r   selectordeclarationsr#   r$   )r   r&   r'   r#   r$   r   r   r   r      s    
zRuleSet.__init__c             C   s   dj | | jj S )Nz3<{0.__class__.__name__} at {0.line}:{0.column} {1}>)r   r&   as_css)r   r   r   r   r      s    zRuleSet.__repr__)r   r   r   r   r    r   r   r   r   r   r   r%   r   s   r%   c               @   s    e Zd ZdZdd Zdd ZdS )Declarationa  A property declaration.

    .. attribute:: name

        The property name as a normalized (lower-case) string.

    .. attribute:: value

        The property value as a :class:`~.token_data.TokenList`.

        The value is not parsed. UAs using tinycss may only support
        some properties or some values and tinycss does not know which.
        They need to parse values themselves and ignore declarations with
        unknown or unsupported properties or values, and fall back
        on any previous declaration.

        :mod:`tinycss.color3` parses color values, but other values
        will need specific parsing/validation code.

    .. attribute:: priority

        Either the string ``'important'`` or ``None``.

    c             C   s&   || _ t|| _|| _|| _|| _d S )N)namer   valuepriorityr#   r$   )r   r*   r+   r,   r#   r$   r   r   r   r      s
    
zDeclaration.__init__c             C   s(   | j rd| j  nd}dj| | jj |S )Nz ! z=<{0.__class__.__name__} {0.line}:{0.column} {0.name}: {1}{2}>)r,   r   r+   r(   )r   r,   r   r   r   r      s    zDeclaration.__repr__N)r   r   r   r   r   r   r   r   r   r   r)      s   r)   c               @   s$   e Zd ZdZdZdd Zdd ZdS )PageRulea  A parsed CSS 2.1 @page rule.

    .. attribute:: at_keyword

        Always ``'@page'``

    .. attribute:: selector

        The page selector.
        In CSS 2.1 this is either ``None`` (no selector), or the string
        ``'first'``, ``'left'`` or ``'right'`` for the pseudo class
        of the same name.

    .. attribute:: specificity

        Specificity of the page selector. This is a tuple of four integers,
        but these tuples are mostly meant to be compared to each other.

    .. attribute:: declarations

        A list of :class:`Declaration`, in source order.

    .. attribute:: at_rules

        The list of parsed at-rules inside the @page block, in source order.
        Always empty for CSS 2.1.

    z@pagec             C   s(   || _ || _|| _|| _|| _|| _d S )N)r&   specificityr'   at_rulesr#   r$   )r   r&   r/   r'   r0   r#   r$   r   r   r   r      s    zPageRule.__init__c             C   s
   dj | S )Nz9<{0.__class__.__name__} {0.line}:{0.column} {0.selector}>)r   )r   r   r   r   r      s    zPageRule.__repr__N)r   r   r   r   r    r   r   r   r   r   r   r.      s   	r.   c               @   s$   e Zd ZdZdZdd Zdd ZdS )	MediaRuleaO  A parsed @media rule.

    .. attribute:: at_keyword

        Always ``'@media'``

    .. attribute:: media

        For CSS 2.1 without media queries: the media types
        as a list of strings.

    .. attribute:: rules

        The list :class:`RuleSet` and various at-rules inside the @media
        block, in source order.

    z@mediac             C   s   || _ || _|| _|| _d S )N)mediar   r#   r$   )r   r2   r   r#   r$   r   r   r   r      s    zMediaRule.__init__c             C   s
   dj | S )Nz6<{0.__class__.__name__} {0.line}:{0.column} {0.media}>)r   )r   r   r   r   r     s    zMediaRule.__repr__N)r   r   r   r   r    r   r   r   r   r   r   r1      s   r1   c               @   s$   e Zd ZdZdZdd Zdd ZdS )
ImportRulea  A parsed @import rule.

    .. attribute:: at_keyword

        Always ``'@import'``

    .. attribute:: uri

        The URI to be imported, as read from the stylesheet.
        (URIs are not made absolute.)

    .. attribute:: media

        For CSS 2.1 without media queries: the media types
        as a list of strings.
        This attribute is explicitly ``['all']`` if the media was omitted
        in the source.

    z@importc             C   s   || _ || _|| _|| _d S )N)urir2   r#   r$   )r   r4   r2   r#   r$   r   r   r   r     s    zImportRule.__init__c             C   s
   dj | S )Nz4<{0.__class__.__name__} {0.line}:{0.column} {0.uri}>)r   )r   r   r   r   r   #  s    zImportRule.__repr__N)r   r   r   r   r    r   r   r   r   r   r   r3     s   r3   c             C   sj   t | } tt| d}dd |D ddddgkr`|\}}}}|j|jfdkr`|j d
 dkr`| S t|| S )zRemove any valid @charset at the beggining of a token stream.

    :param tokens:
        An iterable of tokens
    :returns:
        A possibly truncated iterable of tokens

       c             S   s   g | ]
}|j qS r   )type).0tr   r   r   
<listcomp>4  s    z&_remove_at_charset.<locals>.<listcomp>	ATKEYWORDSSTRING;@charset r   ")r>   r?   )iterlistr   r+   r(   r   )tokensheaderZatkwspacestringZ	semicolonr   r   r   _remove_at_charset)  s    	rG   c               @   s   e Zd ZdZd ddZd!ddZd"ddZd	d
 Zdd Zdd Z	dgZ
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )#CSS21Parserar  Parser for CSS 2.1

    This parser supports the core CSS syntax as well as @import, @media,
    @page and !important.

    Note that property values are still not parsed, as UAs using this
    parser may only support some properties or some values.

    Currently the parser holds no state. It being a class only allows
    subclassing and overriding its methods.

    Nc             C   sB   t |dr|j }nt|d}|j }W dQ R X | j||||S )a]  Parse a stylesheet from a file or filename.

        Character encoding-related parameters and behavior are the same
        as in :meth:`parse_stylesheet_bytes`.

        :param css_file:
            Either a file (any object with a :meth:`~file.read` method)
            or a filename.
        :return:
            A :class:`Stylesheet`.

        readrbN)hasattrrI   openparse_stylesheet_bytes)r   Zcss_fileprotocol_encodinglinking_encodingdocument_encoding	css_bytesfdr   r   r   parse_stylesheet_fileM  s    

z!CSS21Parser.parse_stylesheet_filec             C   s    t ||||\}}| j||dS )aN  Parse a stylesheet from a byte string.

        The character encoding is determined from the passed metadata and the
        ``@charset`` rule in the stylesheet (if any).
        If no encoding information is available or decoding fails,
        decoding defaults to UTF-8 and then fall back on ISO-8859-1.

        :param css_bytes:
            A CSS stylesheet as a byte string.
        :param protocol_encoding:
            The "charset" parameter of a "Content-Type" HTTP header (if any),
            or similar metadata for other protocols.
        :param linking_encoding:
            ``<link charset="">`` or other metadata from the linking mechanism
            (if any)
        :param document_encoding:
            Encoding of the referring style sheet or document (if any)
        :return:
            A :class:`Stylesheet`.

        )r   )r   parse_stylesheet)r   rQ   rN   rO   rP   css_unicoder   r   r   r   rM   c  s    z"CSS21Parser.parse_stylesheet_bytesc             C   s2   t |}|rt|}| j|dd\}}t|||S )a2  Parse a stylesheet from an Unicode string.

        :param css_unicode:
            A CSS stylesheet as an unicode string.
        :param encoding:
            The character encoding used to decode the stylesheet from bytes,
            if any.
        :return:
            A :class:`Stylesheet`.

        
stylesheet)context)r   rG   parse_rulesr   )r   rU   r   rC   r   r   r   r   r   rT   ~  s
    zCSS21Parser.parse_stylesheetc             C   s   | j t|S )a  Parse a "style" attribute (eg. of an HTML element).

        This method only accepts Unicode as the source (HTML) document
        is supposed to handle the character encoding.

        :param css_source:
            The attribute value, as an unicode string.
        :return:
            A tuple of the list of valid :class:`Declaration` and
            a list of :class:`~.parsing.ParseError`.
        )parse_declaration_listr   )r   Z
css_sourcer   r   r   parse_style_attr  s    zCSS21Parser.parse_style_attrc       
      C   s   g }g }t |}x|D ]}|jdkryZ|jdkrX| j||}| j||||}|j| n$| j||\}}|j| |j| W q tk
r }	 z|j|	 W Y dd}	~	X qX qW ||fS )a  Parse a sequence of rules (rulesets and at-rules).

        :param tokens:
            An iterable of tokens.
        :param context:
            Either ``'stylesheet'`` or an at-keyword such as ``'@media'``.
            (Most at-rules are only allowed in some contexts.)
        :return:
            A tuple of a list of parsed rules and a list of
            :class:`~.parsing.ParseError`.

        r;   CDOCDCr:   N)r;   r[   r\   )rA   r6   read_at_ruleparse_at_ruleappendparse_rulesetextendr   )
r   rC   rW   r   r   tokenruleresultrule_errorsexcr   r   r   rX     s"    



 zCSS21Parser.parse_rulesc             C   s   |j j }g }|}x$|D ]}|jdkr*P q|j| qW t|}x|D ]}t|d qFW |jdkrh|jnd}t||||j|j	S )aG  Read an at-rule from a token stream.

        :param at_keyword_token:
            The ATKEYWORD token that starts this at-rule
            You may have read it already to distinguish the rule
            from a ruleset.
        :param tokens:
            An iterator of subsequent tokens. Will be consumed just enough
            for one at-rule.
        :return:
            An unparsed :class:`AtRule`.
        :raises:
            :class:`~.parsing.ParseError` if the head is invalid for the core
            grammar. The body is **not** validated. See :class:`AtRule`.

        z{;zat-rule head{N)
r+   lowerr6   r_   r	   r   contentr   r#   r$   )r   Zat_keyword_tokenrC   r    r!   rb   Z
head_tokenr"   r   r   r   r]     s    



zCSS21Parser.read_at_rulerV   c             C   s(  |j dkr|| jkr"t|d| | j|j\}}|jdkrNt|dj|j | j|jd\}}}	|j|	 t	|||||j
|jS |j dkr
|dkrt|d| |jst|d| j|j}
|jdkrt|dj|j | j|jd\}}	|j|	 t|
||j
|jS |j d	kr|dkr.t|d
| xB|D ]:}|j dkr4|j rZdj|j }nd}t|d| q4W |j}|st|d|d jdkrt|d|d j |d j}| jt|dd }
|jdk	rt|d dt||
|j
|jS |j dkrt|dnt|dj||j dS )a  Parse an at-rule.

        Subclasses that override this method must use ``super()`` and
        pass its return value for at-rules they do not know.

        In CSS 2.1, this method handles @charset, @import, @media and @page
        rules.

        :param rule:
            An unparsed :class:`AtRule`.
        :param previous_rules:
            The list of at-rules and rulesets that have been parsed so far
            in this context. This list can be used to decide if the current
            rule is valid. (For example, @import rules are only allowed
            before anything but a @charset rule.)
        :param context:
            Either ``'stylesheet'`` or an at-keyword such as ``'@media'``.
            (Most at-rules are only allowed in some contexts.)
        :raises:
            :class:`~.parsing.ParseError` if the rule is invalid.
        :return:
            A parsed at-rule

        z@pagez@page rule not allowed in Nzinvalid {0} rule: missing blockz@mediarV   z@media rule not allowed in zexpected media types for @media@importz@import rule not allowed in @charsetzan {0} rulez	a rulesetz@import rule not allowed after z'expected URI or STRING for @import ruler   URIr<   z-expected URI or STRING for @import rule, got r   zexpected ';', got a blockz%mis-placed or malformed @charset rulez#unknown at-rule in {0} context: {1})rk   rj   )rl   r<   )r    at_page_allowed_contextsr   parse_page_selectorr!   r"   r   parse_declarations_and_at_rulesra   r.   r#   r$   parse_mediarX   r1   r6   r+   r	   r3   )r   rc   Zprevious_rulesr   rW   r&   r/   r'   r0   re   r2   r   Zprevious_ruletype_r!   r4   r   r   r   r^     sf    











zCSS21Parser.parse_at_rulec             C   sv   |s
dgS g }xbt t|D ]R}dd |D }|dgkrJ|j|d j qt|d d|rfddj| nd	 qW |S )
aC  For CSS 2.1, parse a list of media types.

        Media Queries are expected to override this.

        :param tokens:
            A list of tokens
        :raises:
            :class:`~.parsing.ParseError` on invalid media types/queries
        :returns:
            For CSS 2.1, a list of media types as strings
        allc             S   s   g | ]
}|j qS r   )r6   )r7   rb   r   r   r   r9   M  s    z+CSS21Parser.parse_media.<locals>.<listcomp>IDENTr   zexpected a media typez, got z, r-   )r   r
   r_   r+   r   join)r   rC   Zmedia_typesparttypesr   r   r   rq   =  s    

zCSS21Parser.parse_mediac             C   sj   |sd
S t |dkrX|d jdkrX|d jdkrX|d j}ddddj|}|rX||fS t|d ddS )a  Parse an @page selector.

        :param tokens:
            An iterable of token, typically from the  ``head`` attribute of
            an unparsed :class:`AtRule`.
        :returns:
            A page selector. For CSS 2.1, this is ``'first'``, ``'left'``,
            ``'right'`` or ``None``.
        :raises:
            :class:`~.parsing.ParseError` on invalid selectors

        Nr      :r   rt   )firstleftrightzinvalid @page selectorr   r   )Nr}   )r   r   )r   r   )r   r   )r   r6   r+   getr   )r   rC   Zpseudo_classr/   r   r   r   ro   U  s    
zCSS21Parser.parse_page_selectorc             C   s  g }g }g }t |}x|D ]}|jdkry*| j||}| j||||}|j| W q tk
r~ }	 z|j|	 W Y dd}	~	X qX q|jdkrg }
x&|r|jdkr|
j| t|d}qW |
ry|j| j|
 W q tk
r }	 z|j|	 W Y dd}	~	X qX qW |||fS )a  Parse a mixed list of declarations and at rules, as found eg.
        in the body of an @page rule.

        Note that to add supported at-rules inside @page,
        :class:`~.page3.CSSPage3Parser` extends :meth:`parse_at_rule`,
        not this method.

        :param tokens:
            An iterable of token, typically from the  ``body`` attribute of
            an unparsed :class:`AtRule`.
        :param context:
            An at-keyword such as ``'@page'``.
            (Most at-rules are only allowed in some contexts.)
        :returns:
            A tuple of:

            * A list of :class:`Declaration`
            * A list of parsed at-rules (empty for CSS 2.1)
            * A list of :class:`~.parsing.ParseError`

        r:   Nr;   r=   )rA   r6   r]   r^   r_   r   nextparse_declaration)r   rC   rW   r0   r'   r   rb   rc   rd   errZdeclaration_tokensr   r   r   rp   n  s2    



 z+CSS21Parser.parse_declarations_and_at_rulesc       	      C   s   g }xt |g|D ]p}|jdkrxt|}|s6t|dx|D ]}t|d q<W | j|j\}}t|||j|j	}||fS |j
| qW t|ddS )a`  Parse a ruleset: a selector followed by declaration block.

        :param first_token:
            The first token of the ruleset (probably of the selector).
            You may have read it already to distinguish the rule
            from an at-rule.
        :param tokens:
            an iterator of subsequent tokens. Will be consumed just enough
            for one ruleset.
        :return:
            a tuple of a :class:`RuleSet` and an error list.
            The errors are recovered :class:`~.parsing.ParseError` in declarations.
            (Parsing continues from the next declaration on such errors.)
        :raises:
            :class:`~.parsing.ParseError` if the selector is invalid for the
            core grammar.
            Note a that a selector can be valid for the core grammar but
            not for CSS 2.1 or another level.

        rg   zempty selectorr&   z&no declaration block found for rulesetN)r   r6   r	   r   r   rY   ri   r%   r#   r$   r_   )	r   Zfirst_tokenrC   r&   rb   Zselector_tokenr'   r   Zrulesetr   r   r   r`     s    


zCSS21Parser.parse_rulesetc             C   s   g }g }x0|D ](}|j dkr,|j| g }q|j| qW |j| g }g }xZ|D ]R}t|}|rRy|j| j| W qR tk
r } z|j| W Y dd}~X qRX qRW ||fS )a  Parse a ``;`` separated declaration list.

        You may want to use :meth:`parse_declarations_and_at_rules` (or
        some other method that uses :func:`parse_declaration` directly)
        instead if you have not just declarations in the same context.

        :param tokens:
            an iterable of tokens. Should stop at (before) the end
            of the block, as marked by ``}``.
        :return:
            a tuple of the list of valid :class:`Declaration` and a list
            of :class:`~.parsing.ParseError`

        r=   N)r6   r_   r	   r   r   )r   rC   partsZ	this_partrb   r'   r   rf   r   r   r   rY     s$    




 z"CSS21Parser.parse_declaration_listc             C   s   t |}t|}|jdkr&|jj }nt|dj|j|}x@|D ].}|jdkrTP qB|jdkrBt|dj|jqBW t|dtt|}|st|dt	| | j
|\}}t||||j|jS )a=  Parse a single declaration.

        :param tokens:
            an iterable of at least one token. Should stop at (before)
            the end of the declaration, as marked by a ``;`` or ``}``.
            Empty declarations (ie. consecutive ``;`` with only white space
            in-between) should be skipped earlier and not passed to
            this method.
        :returns:
            a :class:`Declaration`
        :raises:
            :class:`~.parsing.ParseError` if the tokens do not match the
            'declaration' production of the core grammar.

        rt   z!expected a property name, got {0}ry   r;   zexpected ':', got {0}zexpected ':'zexpected a property value)rA   r   r6   r+   rh   r   r   r	   rB   r   parse_value_priorityr)   r#   r$   )r   rC   Z
name_tokenZproperty_namerb   r+   r,   r   r   r   r     s*    





zCSS21Parser.parse_declarationc             C   s   t |}|j }|jdkr|jj dkrxf|r|j }|jdkr|jdkrx|rh|d	 jdkrh|j  qLW |sxt|d|dfS |jdkr*P q*W |dfS )
a!  Separate any ``!important`` marker at the end of a property value.

        :param tokens:
            A list of tokens for the property value.
        :returns:
            A tuple of the actual property value (a list of tokens)
            and the :attr:`~Declaration.priority`.
        rt   Z	importantZDELIM!r   r;   z"expected a value before !importantNrm   )rB   popr6   r+   rh   r   )r   rC   r+   rb   r   r   r   r     s    	
z CSS21Parser.parse_value_priority)NNN)NNN)N)r   r   r   r   rS   rM   rT   rZ   rX   r]   rn   r^   rq   ro   rp   r`   rY   r   r   r   r   r   r   rH   =  s$    
 

!$V0'&,rH   N)r   
__future__r   	itertoolsr   r   Zdecodingr   Z
token_datar   Z	tokenizerr   Zparsingr	   r
   r   r   r   r   r   objectr   r   r%   r)   r.   r1   r3   rG   rH   r   r   r   r   <module>   s   $ *"'-"