o
    /hi                     @   sF   d Z ddgZddlZddlmZ ddlmZ G dd deejjZ	dS )	aF  CSSStyleDeclaration implements DOM Level 2 CSS CSSStyleDeclaration and
extends CSS2Properties

see
    http://www.w3.org/TR/1998/REC-CSS2-19980512/syndata.html#parsing-errors

Unknown properties
------------------
User agents must ignore a declaration with an unknown property.
For example, if the style sheet is::

    H1 { color: red; rotation: 70minutes }

the user agent will treat this as if the style sheet had been::

    H1 { color: red }

Cssutils gives a message about any unknown properties but
keeps any property (if syntactically correct).

Illegal values
--------------
User agents must ignore a declaration with an illegal value. For example::

    IMG { float: left }       /* correct CSS2 */
    IMG { float: left here }  /* "here" is not a value of 'float' */
    IMG { background: "red" } /* keywords cannot be quoted in CSS2 */
    IMG { border-width: 3 }   /* a unit must be specified for length values */

A CSS2 parser would honor the first rule and ignore the rest, as if the
style sheet had been::

    IMG { float: left }
    IMG { }
    IMG { }
    IMG { }

Cssutils again will issue a message (WARNING in this case) about invalid
CSS2 property values.

TODO:
    This interface is also used to provide a read-only access to the
    computed values of an element. See also the ViewCSS interface.

    - return computed values and not literal values
    - simplify unit pairs/triples/quadruples
      2px 2px 2px 2px -> 2px for border/padding...
    - normalize compound properties like:
      background: no-repeat left url()  #fff
      -> background: #fff url() no-repeat left
CSSStyleDeclarationProperty    N   )CSS2Properties)r   c                       sh  e Zd ZdZdK fdd	Zdd Zd	d
 Zdd Zdd Zdd Z	dd Z
 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eeed'd(ZdLd)d*Zd+d, Zed-d. ed/d(ZdMd0d1ZdNd3d4ZdNd5d6ZdOd7d8ZdNd9d:ZdNd;d<ZdPd=d>Z d?d@ Z!edAd. dBd(Z"dCdD Z#dEdF Z$ee#e$dGd(Z%dHdI Z&ee&dJd(Z'  Z(S )Qr   aJ  The CSSStyleDeclaration class represents a single CSS declaration
    block. This class may be used to determine the style properties
    currently set in a block or to set style properties explicitly
    within the block.

    While an implementation may not recognize all CSS properties within
    a CSS declaration block, it is expected to provide access to all
    specified properties in the style sheet through the
    CSSStyleDeclaration interface.
    Furthermore, implementations that support a specific level of CSS
    should correctly handle CSS shorthand properties for that level. For
    a further discussion of shorthand properties, see the CSS2Properties
    interface.

    Additionally the CSS2Properties interface is implemented.

    $css2propertyname
        All properties defined in the CSS2Properties class are available
        as direct properties of CSSStyleDeclaration with their respective
        DOM name, so e.g. ``fontStyle`` for property 'font-style'.

        These may be used as::

            >>> style = CSSStyleDeclaration(cssText='color: red')
            >>> style.color = 'green'
            >>> print(style.color)
            green
            >>> del style.color
            >>> print(style.color)
            <BLANKLINE>

    Format::

        [Property: Value Priority?;]* [Property: Value Priority?]?
     NFc                    s&   t    || _|| _|| _|| _dS )a  
        :param cssText:
            Shortcut, sets CSSStyleDeclaration.cssText
        :param parentRule:
            The CSS rule that contains this declaration block or
            None if this CSSStyleDeclaration is not attached to a CSSRule.
        :param readonly:
            defaults to False
        :param validating:
            a flag defining if this sheet should be validated on change.
            Defaults to None, which means defer to the parent stylesheet.
        N)super__init___parentRule
validatingcssText	_readonly)selfr   
parentRulereadonlyr
   	__class__ T/var/www/html/myenv/lib/python3.10/site-packages/cssutils/css/cssstyledeclaration.pyr   b   s
   

zCSSStyleDeclaration.__init__c                 C   s(   t |tr	|j}n| |}||  v S )zCheck if a property (or a property with given name) is in style.

        :param name:
            a string or Property, uses normalized name and not literalname
        )
isinstancer   name
_normalize_CSSStyleDeclaration__nnames)r   nameOrPropertyr   r   r   r   __contains__u   s   

z CSSStyleDeclaration.__contains__c                    s    fdd}| S )zAIterator of set Property objects with different normalized names.c                  3   s        D ]}  | V  qd S N)r   getProperty)r   r   r   r   
properties   s   z0CSSStyleDeclaration.__iter__.<locals>.propertiesr   )r   r   r   r   r   __iter__   s   zCSSStyleDeclaration.__iter__c                 C   s   t |  S )z]Analoguous to standard dict returns property names which are set in
        this declaration.)listr   r   r   r   r   keys   s   zCSSStyleDeclaration.keysc                 C   
   |  |S )zzRetrieve the value of property ``CSSName`` from this declaration.

        ``CSSName`` will be always normalized.
        getPropertyValuer   CSSNamer   r   r   __getitem__   s   
zCSSStyleDeclaration.__getitem__c                 C   s$   d}t |tr|\}}| |||S )zSet value of property ``CSSName``. ``value`` may also be a tuple of
        (value, priority), e.g. style['color'] = ('red', 'important')

        ``CSSName`` will be always normalized.
        N)r   tuplesetProperty)r   r%   valuepriorityr   r   r   __setitem__   s   
zCSSStyleDeclaration.__setitem__c                 C   r!   )zDelete property ``CSSName`` from this declaration.
        If property is not in this declaration return u'' just like
        removeProperty.

        ``CSSName`` will be always normalized.
        removePropertyr$   r   r   r   __delitem__   s   
zCSSStyleDeclaration.__delitem__c                    s:   g d}| tj ||v rt || dS td| )a  Prevent setting of unknown properties on CSSStyleDeclaration
        which would not work anyway. For these
        ``CSSStyleDeclaration.setProperty`` MUST be called explicitly!

        TODO:
            implementation of known is not really nice, any alternative?
        )
_tokenizer_log_ttypes_seqseqr   r	   r   valid
wellformedr
   r   	_profiles_validatingzRUnknown CSS Property, ``CSSStyleDeclaration.setProperty("%s", ...)`` MUST be used.N)extendr   _propertiesr   __setattr__AttributeError)r   nvknownr   r   r   r:      s   zCSSStyleDeclaration.__setattr__c                 C   s   d | jj| jddS )Nzcssutils.css.{}(cssText={!r}) )	separator)formatr   __name__
getCssTextr   r   r   r   __repr__   s   
zCSSStyleDeclaration.__repr__c              	   C   s6   d| j j d| jdt| jdddt| dd	S )	Nz<cssutils.css.z object length=z (all: Tallz) at 0xx>)r   rB   lengthlengetPropertiesidr   r   r   r   __str__   s   6zCSSStyleDeclaration.__str__c                 C   sB   g }t | jD ]}|j}t|tr|j|vr||j qt |S )zReturn iterator for all different names in order as set
        if names are set twice the last one is used (double reverse!)
        )reversedr3   r)   r   r   r   append)r   namesitemvalr   r   r   __nnames   s   zCSSStyleDeclaration.__nnamesc                 C   r!   )aZ  (DOM CSS2Properties) Overwritten here and effectively the same as
        ``self.getPropertyValue(CSSname)``.

        Parameter is in CSSname format ('font-style'), see CSS2Properties.

        Example::

            >>> style = CSSStyleDeclaration(cssText='font-style:italic;')
            >>> print(style.fontStyle)
            italic
        r"   r$   r   r   r   _getP   s   
zCSSStyleDeclaration._getPc                 C   s   |  || dS )a  (DOM CSS2Properties) Overwritten here and effectively the same as
        ``self.setProperty(CSSname, value)``.

        Only known CSS2Properties may be set this way, otherwise an
        AttributeError is raised.
        For these unknown properties ``setPropertyValue(CSSname, value)``
        has to be called explicitly.
        Also setting the priority of properties needs to be done with a
        call like ``setPropertyValue(CSSname, value, priority)``.

        Example::

            >>> style = CSSStyleDeclaration()
            >>> style.fontStyle = 'italic'
            >>> # or
            >>> style.setProperty('font-style', 'italic', '!important')

        N)r(   )r   r%   r)   r   r   r   _setP   s   zCSSStyleDeclaration._setPc                 C   s   |  | dS )a1  (cssutils only) Overwritten here and effectively the same as
        ``self.removeProperty(CSSname)``.

        Example::

            >>> style = CSSStyleDeclaration(cssText='font-style:italic;')
            >>> del style.fontStyle
            >>> print(style.fontStyle)
            <BLANKLINE>

        Nr,   r$   r   r   r   _delP  s   zCSSStyleDeclaration._delPc                 c   s    | j D ]}|jV  qdS )zGenerator yielding any known child in this declaration including
        *all* properties, comments or CSSUnknownrules.
        N)r2   r)   )r   rQ   r   r   r   children  s   

zCSSStyleDeclaration.childrenc                 C   s   t j| S )z#Return serialized property cssText.cssutilsserdo_css_CSSStyleDeclarationr   r   r   r   _getCssText  s   zCSSStyleDeclaration._getCssTextc           	         s        |}d
 fdd	}d
 fdd	d
 fdd	}  } jd||||dd	\}}|D ]} |j_q4 | dS )a  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.
        Nc                    sj    j ||dd} |d dkr|  t d}||_|jr(||d | S  jd 	|  | S )NT)
starttoken	semicolon;parentr   z1CSSStyleDeclaration: Syntax Error in Property: %s)
_tokensupto2_tokenvaluepopr   r   r5   rO   r0   error	_valuestr)expectedr3   token	tokenizertokenspropertyr   r   r   ident.  s   
z.CSSStyleDeclaration._setCssText.<locals>.identc                    s4     |  j|dd } jd| | | S )NT)propertyvalueendonlyz8CSSStyleDeclaration: Unexpected token, ignoring upto %r.)rd   rg   rc   r0   rf   )rh   r3   ri   rj   ignoredr   r   r   
unexpected@  s   z3CSSStyleDeclaration._setCssText.<locals>.unexpectedc                    s<     |dkr jjd |g dd | S | |||S )Nr`   z6CSSStyleDeclaration: Stripped standalone semicolon: %sT
neverraise)rd   r0   inforg   )rh   r3   ri   rj   r   rp   r   r   charL  s   
z-CSSStyleDeclaration._setCssText.<locals>.char)IDENTCHAR)rh   r3   rj   productionsdefaultr   )_checkReadonly
_tokenize2_tempSeq_parser)   _parent_setSeq)	r   r   rj   rm   ru   newseqr5   rh   rQ   r   rt   r   _setCssText  s    

	
zCSSStyleDeclaration._setCssTextzh(DOM) A parsable textual representation of the declaration block excluding the surrounding curly braces.)docc                 C   s   t j| |S )aU  
        :returns:
            serialized property cssText, each property separated by
            given `separator` which may e.g. be ``u''`` to be able to use
            cssText directly in an HTML style attribute. ``;`` is part of
            each property (except the last one) and **cannot** be set with
            separator!
        rX   )r   r@   r   r   r   rC   q  s   	zCSSStyleDeclaration.getCssTextc                 C   
   || _ d S r   r	   )r   r   r   r   r   _setParentRule|     
z"CSSStyleDeclaration._setParentRulec                 C   s   | j S r   r   r   r   r   r   <lambda>  s    zCSSStyleDeclaration.<lambda>zy(DOM) The CSS rule that contains this declaration block or None if this CSSStyleDeclaration is not attached to a CSSRule.c                    s   |r|s  |}|r|gS g S |s fdd  D S  |}g } jD ]}|j}t|tr=|r8|j|kr=|| q'|S )aX  
        :param name:
            optional `name` of properties which are requested.
            Only properties with this **always normalized** `name` are returned.
            If `name` is ``None`` all properties are returned (at least one for
            each set name depending on parameter `all`).
        :param all:
            if ``False`` (DEFAULT) only the effective properties are returned.
            If name is given a list with only one property is returned.

            if ``True`` all properties including properties set multiple times
            with different values or priorities for different UAs are returned.
            The order of the properties is fully kept as in the original
            stylesheet.
        :returns:
            a list of :class:`~cssutils.css.Property` objects set in
            this declaration.
        c                    s   g | ]}  |qS r   )r   ).0r   r   r   r   
<listcomp>  s    z5CSSStyleDeclaration.getProperties.<locals>.<listcomp>)	r   r   r   r3   r)   r   r   r   rO   )r   r   rF   pnnamer   rQ   rR   r   r   r   rK     s   



z!CSSStyleDeclaration.getPropertiesTc                 C   s`   |  |}d}t| jD ]!}|j}t|tr-|r||jks"||jkr-|jr)|  S |s-|}q|S )a  
        :param name:
            of the CSS property, always lowercase (even if not normalized)
        :param normalize:
            if ``True`` (DEFAULT) name will be normalized (lowercase, no simple
            escapes) so "color", "COLOR" or "C\olor" will all be equivalent

            If ``False`` may return **NOT** the effective value but the
            effective for the unnormalized name.
        :returns:
            the effective :class:`~cssutils.css.Property` object.
        N)	r   rN   r3   r)   r   r   r   literalnamer*   )r   r   	normalizer   foundrQ   rR   r   r   r   r     s   

zCSSStyleDeclaration.getPropertyc                 C   sB   |  |}|| jv r| jjd| dd | ||}|r|jS dS )a  
        :param name:
            of the CSS property, always lowercase (even if not normalized)
        :param normalize:
            if ``True`` (DEFAULT) name will be normalized (lowercase, no simple
            escapes) so "color", "COLOR" or "C\olor" will all be equivalent

            If ``False`` may return **NOT** the effective value but the
            effective for the unnormalized name.
        :returns:
            ``~cssutils.css.CSSValue``, the value of the effective
            property if it has been explicitly set for this declaration block.

        (DOM)
        Used to retrieve the object representation of the value of a CSS
        property if it has been explicitly set within this declaration
        block. Returns None if the property has not been set.

        (This method returns None if the property is a shorthand
        property. Shorthand property values can only be accessed and
        modified as strings, using the getPropertyValue and setProperty
        methods.)

        **cssutils currently always returns a CSSValue if the property is
        set.**

        for more on shorthand properties see
            http://www.dustindiaz.com/css-shorthand/
        zSCSSValue for shorthand property "%s" should be None, this may be implemented later.Trq   N)r   _SHORTHANDPROPERTIESr0   rs   r   propertyValue)r   r   r   r   r   r   r   r   getPropertyCSSValue  s   

z'CSSStyleDeclaration.getPropertyCSSValuec                 C   s   |  ||}|r|jS |S )a  
        :param name:
            of the CSS property, always lowercase (even if not normalized)
        :param normalize:
            if ``True`` (DEFAULT) name will be normalized (lowercase, no simple
            escapes) so "color", "COLOR" or "C\olor" will all be equivalent

            If ``False`` may return **NOT** the effective value but the
            effective for the unnormalized name.
        :param default:
            value to be returned if the property has not been set.
        :returns:
            the value of the effective property if it has been explicitly set
            for this declaration block. Returns `default` if the
            property has not been set.
        )r   r)   )r   r   r   ry   r   r   r   r   r#     s   z$CSSStyleDeclaration.getPropertyValuec                 C   s   |  ||}|r|jS dS )a  
        :param name:
            of the CSS property, always lowercase (even if not normalized)
        :param normalize:
            if ``True`` (DEFAULT) name will be normalized (lowercase, no simple
            escapes) so "color", "COLOR" or "C\olor" will all be equivalent

            If ``False`` may return **NOT** the effective value but the
            effective for the unnormalized name.
        :returns:
            the priority of the effective CSS property (e.g. the
            "important" qualifier) if the property has been explicitly set in
            this declaration block. The empty string if none exists.
        r   )r   r*   )r   r   r   r   r   r   r   getPropertyPriority  s   z'CSSStyleDeclaration.getPropertyPriorityc                 C   s   |    | j||d}|  }|r.| |}| jD ]}t|jtr'|jj|ks,|	| qn| jD ]}t|jtr?|jj
|ksD|	| q1| | |S )a  
        (DOM)
        Used to remove a CSS property if it has been explicitly set within
        this declaration block.

        :param name:
            of the CSS property
        :param normalize:
            if ``True`` (DEFAULT) name will be normalized (lowercase, no simple
            escapes) so "color", "COLOR" or "C\olor" will all be equivalent.
            The effective Property value is returned and *all* Properties
            with ``Property.name == name`` are removed.

            If ``False`` may return **NOT** the effective value but the
            effective for the unnormalized `name` only. Also only the
            Properties with the literal name `name` are removed.
        :returns:
            the value of the property if it has been explicitly set for
            this declaration block. Returns the empty string if the property
            has not been set or the property name does not correspond to a
            known CSS property


        :exceptions:
            - :exc:`~xml.dom.NoModificationAllowedErr`:
              Raised if this declaration is readonly or the property is
              readonly.
        )r   )rz   r#   r|   r   r3   r   r)   r   r   
appendItemr   r   )r   r   r   rr   r   rQ   r   r   r   r-   !  s$   






z"CSSStyleDeclaration.removePropertyc           
      C   s   |    t|tr|}|j}n|s| |S t|||| d}|jro|r[| |}| j|| d}t|D ]&}	|rI|	j	|krI|j
j|	_
|j|	_ dS |	j|krZ|j
j|	_
|j|	_ dS q4| |_d| j_| j|d d| j_dS | jd| d| d	|  dS )
a  (DOM) Set a property value and priority within this declaration
        block.

        :param name:
            of the CSS property to set (in W3C DOM the parameter is called
            "propertyName"), always lowercase (even if not normalized)

            If a property with this `name` is present it will be reset.

            cssutils also allowed `name` to be a
            :class:`~cssutils.css.Property` object, all other
            parameter are ignored in this case

        :param value:
            the new value of the property, ignored if `name` is a Property.
        :param priority:
            the optional priority of the property (e.g. "important"),
            ignored if `name` is a Property.
        :param normalize:
            if True (DEFAULT) `name` will be normalized (lowercase, no simple
            escapes) so "color", "COLOR" or "C\olor" will all be equivalent
        :param replace:
            if True (DEFAULT) the given property will replace a present
            property. If False a new property will be added always.
            The difference to `normalize` is that two or more properties with
            the same name may be set, useful for e.g. stuff like::

                background: red;
                background: rgba(255, 0, 0, 0.5);

            which defines the same property but only capable UAs use the last
            property value, older ones use the first value.

        :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.
        ra   rE   NFr   TzInvalid Property: z: r?   )rz   r   r   r   r-   r5   r   rK   rN   r   r   r   r*   rb   r3   r   rO   r0   warn)
r   r   r)   r*   r   replacenewpr   r   rl   r   r   r   r(   Q  s4   *





"zCSSStyleDeclaration.setPropertyc                 C   s,   t |  }z|| W S  ty   Y dS w )a  (DOM) Retrieve the properties that have been explicitly set in
        this declaration block. The order of the properties 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 properties in this
        declaration block.

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

        :returns:
            the name of the property at this ordinal position. The
            empty string if no property exists at this position.

        **ATTENTION:**
        Only properties with different names are counted. If two
        properties with the same name are present in this declaration
        only the effective one is included.

        :meth:`item` and :attr:`length` work on the same set here.
        r   )r   r   
IndexError)r   indexrP   r   r   r   rQ     s   
zCSSStyleDeclaration.itemc                 C   s   t t|  S r   )rJ   r   r   r   r   r   r   r     s    a  (DOM) The number of distinct properties that have been explicitly in this declaration block. The range of valid indices is 0 to length-1 inclusive. These are properties with a different ``name`` only. :meth:`item` and :attr:`length` work on the same set here.c                 C   s6   z| j jjW S  ty   | jd ur| j Y S Y dS w )NT)r   parentStyleSheetr
   r;   r7   r   r   r   r   _getValidating  s   

z"CSSStyleDeclaration._getValidatingc                 C   r   r   )r7   )r   r
   r   r   r   _setValidating  r   z"CSSStyleDeclaration._setValidatingzIf ``True`` this declaration validates contained properties. The parent StyleSheet validation setting does *always* win though so even if validating is True it may not validate if the StyleSheet defines else!c                 C   s   t dd |  D S )z+Check each contained property for validity.c                 s   s    | ]}|j V  qd S r   )r4   )r   propr   r   r   	<genexpr>  s    z0CSSStyleDeclaration._getValid.<locals>.<genexpr>)rF   rK   r   r   r   r   	_getValid  s   zCSSStyleDeclaration._getValidz#``True`` if each property is valid.)r   NFNr   )NF)T)Tr   )Nr   TT))rB   
__module____qualname____doc__r   r   r   r    r&   r+   r.   r:   rD   rM   r   rT   rU   rV   rW   r\   r   rl   r   rC   r   r   rK   r   r   r#   r   r-   r(   rQ   rI   r   r   r
   r   r4   __classcell__r   r   r   r   r   =   sh    $		"J


'

,


0M

)
r   __all__rY   csspropertiesr   rl   r   utilBase2r   r   r   r   r   <module>   s    4