o
    /h                     @   sD   d Z dgZddlZddlZddlZddlmZ G dd dejZ	dS )z9CSSCharsetRule implements DOM Level 2 CSS CSSCharsetRule.CSSCharsetRule    N   )cssrulec                       s   e Zd ZdZ	d fdd	Zdd Zdd	 Zd
d Z fddZe	eeddZ
dd Ze	dd eddZe	dd ddZe	dd Z  ZS )r   a  
    The CSSCharsetRule interface represents an @charset rule in a CSS style
    sheet. The value of the encoding attribute does not affect the encoding
    of text data in the DOM objects; this encoding is always UTF-16
    (also in Python?). After a stylesheet is loaded, the value of the
    encoding attribute is the value found in the @charset rule. If there
    was no @charset in the original document, then no CSSCharsetRule is
    created. The value of the encoding attribute may also be used as a hint
    for the encoding used on serialization of the style sheet.

    The value of the @charset rule (and therefore of the CSSCharsetRule)
    may not correspond to the encoding the document actually came in;
    character encoding information e.g. in an HTTP header, has priority
    (see CSS document representation) but this is not reflected in the
    CSSCharsetRule.

    This rule is not really needed anymore as setting
    :attr:`CSSStyleSheet.encoding` is much easier.

    Format::

        charsetrule:
            CHARSET_SYM S* STRING S* ';'

    BUT: Only valid format is (single space, double quotes!)::

        @charset "ENCODING";
    NFc                    s2   t  j||d d| _|r|| _nd| _|| _dS )z
        :param encoding:
            a valid character encoding
        :param readonly:
            defaults to False, not used yet
        )
parentRuleparentStyleSheetz@charsetN)super__init__
_atkeywordencoding	_encoding	_readonly)selfr
   r   r   readonly	__class__ O/var/www/html/myenv/lib/python3.10/site-packages/cssutils/css/csscharsetrule.pyr   +   s   	
zCSSCharsetRule.__init__c                 C   s   d| j j d| jdS )Nzcssutils.css.z
(encoding=))r   __name__r
   r   r   r   r   __repr__>   s   zCSSCharsetRule.__repr__c                 C   s$   d| j j d| jdt| ddS )Nz<cssutils.css.z object encoding=z at 0xx>)r   r   r
   idr   r   r   r   __str__A   s   $zCSSCharsetRule.__str__c                 C   s   t j| S )z$The parsable textual representation.)cssutilsserdo_CSSCharsetRuler   r   r   r   _getCssTextD   s   zCSSCharsetRule._getCssTextc           	         s   t  | d}| |}| | || jjkr%d}| jjdt	j
jd | |}| |}| |}| jj|ks<|sId}| jd| |  | | |}| | |}d|ksa|dvrnd}| jd| |  |ru|| _d	S d	S )
a  
        :param cssText:
            A parsable DOMString.
        :exceptions:
            - :exc:`~xml.dom.SyntaxErr`:
              Raised if the specified CSS string value has a syntax error and
              is unparsable.
            - :exc:`~xml.dom.InvalidModificationErr`:
              Raised if the specified CSS string value represents a different
              type of rule than the current one.
            - :exc:`~xml.dom.HierarchyRequestErr`:
              Raised if the rule cannot be inserted at this point in the
              style sheet.
            - :exc:`~xml.dom.NoModificationAllowedErr`:
              Raised if the rule is readonly.
        TFz*CSSCharsetRule must start with "@charset ")errorz&CSSCharsetRule: no encoding found; %r.;)EOFNz!CSSCharsetRule: Syntax Error: %r.N)r   _setCssText
_tokenize2_type
_nexttoken_prodsCHARSET_SYM_logr   xmldomInvalidModificationErr_stringtokenvalueSTRING	_valuestr_tokenvaluer
   )	r   cssText
wellformed	tokenizerencodingtokenencodingtyper
   	semicolonEOFtyper   r   r   r"   H   s6   




zCSSCharsetRule._setCssTextz*(DOM) The parsable textual representation.)fgetfsetdocc                 C   s   |    | |}| |}| |}|r |s | jj| |kr*| jd|  dS zt	| W n t
yC   | jd|  Y dS w | | _dS )a  
        :param encoding:
            a valid encoding to be used. Currently only valid Python encodings
            are allowed.
        :exceptions:
            - :exc:`~xml.dom.NoModificationAllowedErr`:
              Raised if this encoding rule is readonly.
            - :exc:`~xml.dom.SyntaxErr`:
              Raised if the specified encoding value has a syntax error and
              is unparsable.
        z2CSSCharsetRule: Syntax Error in encoding value %r.z-CSSCharsetRule: Unknown (Python) encoding %r.N)_checkReadonlyr#   r%   r&   IDENTr$   r(   r   codecslookupLookupErrorlowerr   )r   r
   r2   r3   
unexpectedr   r   r   _setEncoding   s(   



zCSSCharsetRule._setEncodingc                 C      | j S N)r   r   r   r   r   <lambda>       zCSSCharsetRule.<lambda>z9(DOM)The encoding information used in this @charset rule.)r9   c                 C   rB   rC   )CHARSET_RULEr   r   r   r   rD      rE   z=The type of this rule, as defined by a CSSRule type constant.c                 C   s
   t | jS rC   )boolr
   r   r   r   r   rD      s   
 )NNNF)r   
__module____qualname____doc__r   r   r   r   r"   propertyr0   rA   r
   typer1   __classcell__r   r   r   r   r      s0    1#)
rJ   __all__r<   xml.domr)   r    r   CSSRuler   r   r   r   r   <module>   s    