o
    /h,                     @   sb   d Z dgZddlZddlZddlmZ ddlmZmZm	Z	m
Z
 ddlmZ G dd dejjZdS )	z[CSSVariablesDeclaration
http://disruptive-innovations.com/zoo/cssvariables/#mozTocId496530
CSSVariablesDeclaration    N)	normalize)PreDefProd
ProdParserSequence   )PropertyValuec                       s   e Zd ZdZd, f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d Zdd Zdd ZeeeddZdd Zedd  ed!dZd"d# Zd$d% Zd&d' Zd(d) Zed*d  d+dZ  ZS )-r   zbThe CSSVariablesDeclaration interface represents a single block of
    variable declarations.
     NFc                    s*   t    || _i | _|r|| _|| _dS )aP  
        :param cssText:
            Shortcut, sets CSSVariablesDeclaration.cssText
        :param parentRule:
            The CSS rule that contains this declaration block or
            None if this CSSVariablesDeclaration is not attached to a CSSRule.
        :param readonly:
            defaults to False

        Format::

            variableset
                : vardeclaration [ ';' S* vardeclaration ]* S*
                ;

            vardeclaration
                : varname ':' S* term
                ;

            varname
                : IDENT S*
                ;
        N)super__init___parentRule_varscssText	_readonly)selfr   
parentRulereadonly	__class__ X/var/www/html/myenv/lib/python3.10/site-packages/cssutils/css/cssvariablesdeclaration.pyr      s   

z CSSVariablesDeclaration.__init__c                 C   s   d| j j d| jdS )Nzcssutils.css.z	(cssText=))r   __name__r   r   r   r   r   __repr__5   s   z CSSVariablesDeclaration.__repr__c                 C   s$   d| j j d| jdt| ddS )Nz<cssutils.css.z object length=z at 0xx>)r   r   lengthidr   r   r   r   __str__8   s   $zCSSVariablesDeclaration.__str__c                 C   s   t |t|  v S )zqCheck if a variable is in variable declaration block.

        :param variableName:
            a string
        )r   listkeysr   variableNamer   r   r   __contains__;   s   z$CSSVariablesDeclaration.__contains__c                 C   
   |  |S )zWRetrieve the value of variable ``variableName`` from this
        declaration.
        )getVariableValuer#   r   r   r   __getitem__C   s   
z#CSSVariablesDeclaration.__getitem__c                 C   s   |  || d S N)setVariable)r   r$   valuer   r   r   __setitem__I   s   z#CSSVariablesDeclaration.__setitem__c                 C   r&   r)   )removeVariabler#   r   r   r   __delitem__L      
z#CSSVariablesDeclaration.__delitem__c                 c   s    t |  E dH  dS )z#Iterator of names of set variables.N)r!   r"   r   r   r   r   __iter__O   s   z CSSVariablesDeclaration.__iter__c                 C   s   t | j S )z]Analoguous to standard dict returns variable names which are set in
        this declaration.)r!   r   r"   r   r   r   r   r"   S   s   zCSSVariablesDeclaration.keysc                 C   s   t j| S )z#Return serialized property cssText.)cssutilsserdo_css_CSSVariablesDeclarationr   r   r   r   _getCssTextX   s   z#CSSVariablesDeclaration._getCssTextc              
      s      tt tjdddddtddd  fddd	}t|ttjdd
tjdddddtjdd
|dd dtjdd
tjddddd}t j|d|dd\}}}}|rÈ 	 }i }	d}
|D ]S}d|j
krl|}
qbd|j
krt|
j}||	v rt|D ]\}}t|jd |kr|||
j|jfd|
j|
j q~n||
j|jfd|
j|
j |j|	|< qb|| qb | |	 _d _dS dS )aU  Setting this attribute will result in the parsing of the new value
        and resetting of all the properties in the declaration block
        including the removal or addition of properties.

        :exceptions:
            - :exc:`~xml.dom.NoModificationAllowedErr`:
              Raised if this declaration is readonly or a property is readonly.
            - :exc:`~xml.dom.SyntaxErr`:
              Raised if the specified CSS string value has a syntax error and
              is unparsable.

        Format::

            variableset
            : vardeclaration [ ';' S* vardeclaration ]*
            ;

            vardeclaration
            : varname ':' S* term
            ;

            varname
            : IDENT S*
            ;

            expr
            : [ VARCALL | term ] [ operator [ VARCALL | term ] ]*
            ;

        :FT)toSeqoptionaltermc                 S      dS )NTr   )tvr   r   r   <lambda>       z5CSSVariablesDeclaration._setCssText.<locals>.<lambda>c                    s   dt t| g| dfS )Nr+   )parent)r	   	itertoolschain)r:   tokensr   r   r   r<      s   )namematchr6   )r7   ;c                   S   r9   )N)r   Nr   r   r   r   r   r<      r=   )minmaxCSSVariableDeclaration)emptyOkNIDENTr+   r   var)_checkReadonlyr   r   identcharr   Sr   parse_tempSeqtyper   r+   	enumeratereplacelinecolappend
appendItem_setSeqr   
wellformed)r   r   vardeclarationprodsrX   seqstorenotusednewseqnewvarsnameitemitemnnameiitr   r   r   _setCssText\   st   










z#CSSVariablesDeclaration._setCssTextzh(DOM) A parsable textual representation of the declaration block excluding the surrounding curly braces.)docc                 C   s
   || _ d S r)   r   )r   r   r   r   r   _setParentRule   r/   z&CSSVariablesDeclaration._setParentRulec                 C   s   | j S r)   rg   r   r   r   r   r<      s    z CSSVariablesDeclaration.<lambda>zk(DOM) The CSS rule that contains this declaration block or None if this block is not attached to a CSSRule.c                 C   s(   z	| j t| jW S  ty   Y dS w )a  Used to retrieve the value of a variable if it has been explicitly
        set within this variable declaration block.

        :param variableName:
            The name of the variable.
        :returns:
            the value of the variable if it has been explicitly set in this
            variable declaration block. Returns the empty string if the
            variable has not been set.
        r
   )r   r   r   KeyErrorr#   r   r   r   r'      s
   z(CSSVariablesDeclaration.getVariableValuec                 C   sz   |}z| j | }W n
 ty   Y dS w d| j_|| j v r2t| jD ]\}}|jd |kr1| j|= q"d| j_| j |= |jS )a  Used to remove a variable if it has been explicitly set within this
        variable declaration block.

        :param variableName:
            The name of the variable.
        :returns:
            the value of the variable if it has been explicitly set for this
            variable declaration block. Returns the empty string if the
            variable has not been set.

        :exceptions:
            - :exc:`~xml.dom.NoModificationAllowedErr`:
              Raised if this declaration is readonly is readonly.
        r
   Fr   T)r   ri   r[   r   rQ   r+   r   )r   r$   
normalnamerrc   r   r   r   r   r-      s   
z&CSSVariablesDeclaration.removeVariablec           
      C   s  |    t t|dtt \}}}}|s&| jd|d| d
S t	|t
r.|}nt
|| d}|jsE| jd|d| d
S d| j_t|}|| jv rut| jD ]\}}	|	jd |krs| j|||g|	j|	j|	j  nqWn	| j||gd d	| j_|| j|< d
S )a1  Used to set a variable value within this variable declaration block.

        :param variableName:
            The name of the CSS variable.
        :param value:
            The new value of the variable, may also be a PropertyValue object.

        :exceptions:
            - :exc:`~xml.dom.SyntaxErr`:
              Raised if the specified value has a syntax error and is
              unparsable.
            - :exc:`~xml.dom.NoModificationAllowedErr`:
              Raised if this declaration is readonly or the property is
              readonly.
        r$   zInvalid variableName: z: )r   r>   zInvalid variable value: Fr   rI   TN)rJ   r   rN   r   r   r   rK   _logerror
isinstancer	   rX   r[   r   r   rQ   r+   rR   rP   rS   rT   rU   )
r   r$   r+   rX   r[   r\   unusedr;   rc   r   r   r   r   r*     s2   

z#CSSVariablesDeclaration.setVariablec                 C   s(   z	t |  | W S  ty   Y dS w )a  Used to retrieve the variables that have been explicitly set in
        this variable declaration block. The order of the variables
        retrieved using this method does not have to be the order in which
        they were set. This method can be used to iterate over all variables
        in this variable declaration block.

        :param index:
            of the variable name to retrieve, negative values behave like
            negative indexes on Python lists, so -1 is the last element

        :returns:
            The name of the variable at this ordinal position. The empty
            string if no variable exists at this position.
        r
   )r!   r"   
IndexError)r   indexr   r   r   ra   :  s
   zCSSVariablesDeclaration.itemc                 C   s
   t | jS r)   )lenr   r   r   r   r   r<   O  s   
 zThe number of variables that have been explicitly set in this variable declaration block. The range of valid indices is 0 to length-1 inclusive.)r
   NF)r   
__module____qualname____doc__r   r   r    r%   r(   r,   r.   r0   r"   r4   re   propertyr   rh   r   r'   r-   r*   ra   r   __classcell__r   r   r   r   r      s@     j3)ru   __all__r?   r1   cssutils.helperr   cssutils.prodparserr   r   r   r   r+   r	   util_NewBaser   r   r   r   r   <module>   s    