o
    /hB                     @   sf   d Z ddlZddlZddlmZmZmZ G dd dZG dd dZG d	d
 d
eZ	G dd dZ
dS )zA validating CSSParser    N   )errorhandlerhelper	tokenize2c                   @   s6   e Zd ZdZdd ZdddZdddZdd	d
ZdS )ErrorHandleram  Basic class for CSS error handlers.

    This class class provides a default implementation ignoring warnings and
    recoverable errors and throwing a SAXParseException for fatal errors.

    If a CSS application needs to implement customized error handling, it must
    extend this class and then register an instance with the CSS parser
    using the parser's setErrorHandler method. The parser will then report all
    errors and warnings through this interface.

    The parser shall use this class instead of throwing an exception: it is
    up to the application whether to throw an exception for different types of
    errors and warnings. Note, however, that there is no requirement that the
    parser continue to provide useful information after a call to fatalError
    (in other words, a CSS driver class could catch an exception and report a
    fatalError).
    c                 C   s   t  | _d S N)r   r   _logself r   @/var/www/html/myenv/lib/python3.10/site-packages/cssutils/sac.py__init__   s   zErrorHandler.__init__Nc                 C      | j j||dd d S NT)
neverraise)r   errorr
   	exceptiontokenr   r   r   r         zErrorHandler.errorc                 C   s   | j || d S r   )r   fatalr   r   r   r   r   "   s   zErrorHandler.fatalc                 C   r   r   )r   warnr   r   r   r   r   %   r   zErrorHandler.warnr   )__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   	   s    

r   c                   @   sp   e Zd ZdZdd ZdddZdd Zdd	d
ZdddZdddZ	dddZ
dddZdddZdddZdS )DocumentHandlera  
    void     endFontFace()
             Receive notification of the end of a font face statement.
    void     endMedia(SACMediaList media)
             Receive notification of the end of a media statement.
    void     endPage(java.lang.String name, java.lang.String pseudo_page)
             Receive notification of the end of a media statement.
    void     importStyle(java.lang.String uri, SACMediaList media,
             java.lang.String defaultNamespaceURI)
             Receive notification of a import statement in the style sheet.
    void     startFontFace()
             Receive notification of the beginning of a font face statement.
    void     startMedia(SACMediaList media)
             Receive notification of the beginning of a media statement.
    void     startPage(java.lang.String name, java.lang.String pseudo_page)
             Receive notification of the beginning of a page statement.
    c                 C   s   dd }|| _ d S )Nc                 S   s   t jd|   d S )NzINFO	%s
)sysstderrwrite)msgr   r   r   log=      z%DocumentHandler.__init__.<locals>.logr   )r
   r!   r   r   r   r   <   s   
zDocumentHandler.__init__Nc              	   C   "   |  d|d| d| d dS )z"Receive notification of a comment.zcomment  at [, ]Nr#   )r
   textlinecolr   r   r   commentB      "zDocumentHandler.commentc                 C   s   |  d|  dS )z7Receive notification of the beginning of a style sheet.zstartDocument encoding=%sNr#   r
   encodingr   r   r   startDocumentF   s   zDocumentHandler.startDocumentc                 C   s   |  d dS )z.Receive notification of the end of a document.zendDocument EOFNr#   )r
   sourcer)   r*   r   r   r   endDocumentK   s   zDocumentHandler.endDocumentc                 C   s   |  d| d| d dS )>Receive notification of a import statement in the style sheet.zimportStyle at [r&   r'   Nr#   r
   urimedianamer)   r*   r   r   r   importStyleO      zDocumentHandler.importStylec                 C   s   |  d| d| d dS )LReceive notification of an unknown rule t-rule not supported by this parser.znamespaceDeclaration at [r&   r'   Nr#   r
   prefixr4   r)   r*   r   r   r   namespaceDeclarationT   r8   z$DocumentHandler.namespaceDeclarationc                 C      |  d| d| d dS )z:Receive notification of the beginning of a rule statement.zstartSelector at [r&   r'   Nr#   r
   	selectorsr)   r*   r   r   r   startSelectorY   r8   zDocumentHandler.startSelectorc                 C   r=   )z4Receive notification of the end of a rule statement.zendSelector at [r&   r'   Nr#   r>   r   r   r   endSelector^   s   zDocumentHandler.endSelectorTODOFc              	   C   s"   |  d|d| d| d dS )z&Receive notification of a declaration.z	property r%   r&   r'   Nr#   r
   r6   value	importantr)   r*   r   r   r   propertyb   s   "zDocumentHandler.propertyc              	   C   r$   )r9   zignorableAtRule r%   r&   r'   Nr#   )r
   atRuler)   r*   r   r   r   ignorableAtRuleg   r,   zDocumentHandler.ignorableAtRuleNNNNN)rB   FNN)r   r   r   r   r   r+   r/   r1   r7   r<   r@   rA   rF   rH   r   r   r   r   r   )   s    






r   c                       sz   e Zd ZdZ fddZedd Z fddZd fd	d
	Zd fdd	Z	d fdd	Z
dddZd fdd	Z  ZS )EchoHandlerz!Echos all input to property `out`c                    s   t    g | _d S r   )superr   _outr	   	__class__r   r   r   o   s   

zEchoHandler.__init__c                 C   s   d | jS )N )joinrM   r	   r   r   r   <lambda>s   s    zEchoHandler.<lambda>c                    s,   t  | d|kr| jd|  d S d S )Nutf-8z@charset "%s";
)rL   r/   rM   appendr-   rN   r   r   r/   u   s   zEchoHandler.startDocumentNc                    sL   t  ||||| | jdt||rd| nd|rd| ndf  dS )r2   z@import %s%s%s;
%s rP   N)rL   r7   rM   rT   r   stringr3   rN   r   r   r7   z   s   zEchoHandler.importStylec                    s<   t  |||| | jd|rd| ndt|f  d S )Nz@namespace %s%s;
rU   rP   )rL   r<   rM   rT   r   rV   r:   rN   r   r   r<      s   z EchoHandler.namespaceDeclarationc                    s6   t  ||| |r| jd| | jd d S )Nr&   z {
)rL   r@   rM   rT   rQ   r>   rN   r   r   r@      s   zEchoHandler.startSelectorc                 C   s   | j d d S )Nz    })rM   rT   r>   r   r   r   rA      s   zEchoHandler.endSelectorFc                    s4   t  |||| | jd|||rdnd d S )Nz    {}: {}{};
z !importantrP   )rL   rF   rM   rT   formatrC   rN   r   r   rF      s   zEchoHandler.propertyrI   rJ   )FNN)r   r   r   r   r   rF   outr/   r7   r<   r@   rA   __classcell__r   r   rN   r   rK   l   s    
rK   c                   @   s4   e Zd ZdZdddZdddZdd Zd	d
 ZdS )Parsera  
    java.lang.String     getParserVersion()
        Returns a string about which CSS language is supported by this parser.
     boolean     parsePriority(InputSource source)
          Parse a CSS priority value (e.g.
     LexicalUnit     parsePropertyValue(InputSource source)
          Parse a CSS property value.
     void     parseRule(InputSource source)
          Parse a CSS rule.
     SelectorList     parseSelectors(InputSource source)
          Parse a comma separated list of selectors.
     void     parseStyleDeclaration(InputSource source)
          Parse a CSS style declaration (without '{' and '}').
     void     parseStyleSheet(InputSource source)
          Parse a CSS document.
     void     parseStyleSheet(java.lang.String uri)
          Parse a CSS document from a URI.
     void     setConditionFactory(ConditionFactory conditionFactory)

     void     setDocumentHandler(DocumentHandler handler)
          Allow an application to register a document event handler.
     void     setErrorHandler(ErrorHandler handler)
          Allow an application to register an error event handler.
     void     setLocale(java.util.Locale locale)
          Allow an application to request a locale for errors and warnings.
     void     setSelectorFactory(SelectorFactory selectorFactory)
    Nc                 C   sH   t  | _|r| | n| t  |r| | d S | t  d S r   )r   	Tokenizer
_tokenizersetDocumentHandlerr   setErrorHandlerr   )r
   documentHandlererrorHandlerr   r   r   r      s   
zParser.__init__c                    s  t |trtd||dd }jj|dd}fdd fdd	 fd
d}t|}|\}}}}	d}d|krlt|}
t|}d|krLt|}d|
d kr\|r\t|
d }nj	
d t|}|\}}}}	j| 	 ||	f}zn||rn\d|ks|dv r|g}d}	 t|}|\}}}}	|| d|kr|snd|kr|d7 }nd|kr|d8 }|dkrnqjjd|g|R   nd|krd\}}}	 t|}|\}}}}	d|krt|}nd|krt|}nd|krnq|rj||| nֈj	d|f  nd|kred\}}	 t|}|\}}}}	d|kr,|}nd|kr7t|}nd|krBt|}nd|krHnq|rZjj||g|R   nj	d|f  nvg }g }	 d|kru|d  n7||r{n1d!|kr|d|  g }nd|kr|d|  jj|g|R   n|| t|}|\}}}}	qjd }	 d g d"}}}	 t|}|\}}}}	d|krnS||rnMd|kr|rj	d#| n=|}n:d$|kr|sj	d%| n,d|krj	d&| |}nd|kr|rj	d&| |}nj	d'| | qd|ksd|kst|}|\}}}}	d|krH|d  n.||rNn(d(|ks]d|ks]d|krqd| }|snj	d)| |}n|| d|ksd|kr3d(|krt|}|\}}}}	||rn d|kr|sd}nd|ksd|kr|}nj	d*|  d(|ks|r|rȈj||| d|kr׈jj|||	d+ nd }qt|}|\}}}}	W n ty   Y d S w qs),Ncss)r.   r   T)	fullsheetc                    s    j | dd || d S )N   )_handlerr+   valr)   r*   r	   r   r   COMMENT   s   z#Parser.parseString.<locals>.COMMENTc                    s    j | || d S r   )re   r1   rf   r	   r   r   EOF   r"   zParser.parseString.<locals>.EOFc                    s<    dd d}| \}}}}||v r|| ||| dS dS )Nc                 S   s   d S r   r   rf   r   r   r   rR      s    z4Parser.parseString.<locals>.simple.<locals>.<lambda>)rh   Sri   TFr   )tmaptype_rg   r)   r*   )rh   ri   r   r   simple   s   z"Parser.parseString.<locals>.simplerS   CHARSET_SYMSTRINGr   zInvalid @charset	ATKEYWORD)PAGE_SYM	MEDIA_SYMFONT_FACE_SYM;{}rP   
IMPORT_SYMrJ   URIz!Invalid @import declaration at %rNAMESPACE_SYMrI   IDENTz$Invalid @namespace declaration at %rrj    ,Fzmore than one property name:zno property namezpremature end of propertyz!unexpected property name token %r!z$premature end of property (no value)zunexpected priority token %r)r)   r*   )
isinstancestrcodecs
getdecoderr\   tokenizenextr   stringvalue_errorHandlerr   re   r/   rT   rH   rQ   urivaluer7   r   r<   stripr@   rF   rA   StopIteration)r
   cssTextr.   tokensrn   rk   rm   rg   r)   r*   encodingtokensemicolontokenstartrG   bracesr4   r5   r6   r;   selectorr?   endrD   rE   r   )rh   ri   r
   r   parseString   s`  























#






Y zParser.parseStringc                 C   
   || _ dS )z<Allow an application to register a document event `handler`.N)re   r
   handlerr   r   r   r]        
zParser.setDocumentHandlerc                 C   r   )rB   N)r   r   r   r   r   r^     r   zParser.setErrorHandlerrI   r   )r   r   r   r   r   r   r]   r^   r   r   r   r   rZ      s    

 jrZ   )r   r   r   rP   r   r   r   r   r   rK   rZ   r   r   r   r   <module>   s     C2