a
    ø£Be;  ã                   @   sö   d Z ddlZddlmZmZmZ ddlmZmZm	Z	m
Z
mZ ddlmZ dZdZz$ddlmZ dd	lmZ d
ZdZW n^ eyÒ   z(ddlmZ dZd
Zejdedd� W n" eyÌ   ejdedd� Y n0 Y n0 dgZG dd„ deƒZdd„ ZdS )zD
NBConvert Preprocessor for sanitizing HTML rendering of notebooks.
é    N)ÚALLOWED_ATTRIBUTESÚALLOWED_TAGSÚclean)ÚAnyÚBoolÚListÚSetÚUnicodeé   )ÚPreprocessorF)ÚALLOWED_CSS_PROPERTIES)ÚCSSSanitizerT)ÚALLOWED_STYLESzFSupport for bleach <5 will be removed in a future version of nbconverté   )Ú
stacklevelz[The installed bleach/tinycss2 do not provide CSS sanitization, please upgrade to bleach >=5ÚSanitizeHTMLc                   @   sš   e Zd ZdZededd�Zeeƒ de	dd�Z
eeƒ dedd�Zedddd�Zeddd	d�Zedh d
£dd�Zedddhdd�Zdd„ Zdd„ Zdd„ ZdS )r   z A preprocessor to sanitize html.TzAllowed HTML tag attributes)ÚconfigÚdefault_valueÚhelpzList of HTML tags to allowz,Allowed CSS styles if <style> tag is allowedFz:If True, remove unsafe markup entirely instead of escapingz)If True, strip comments from escaped HTML>   z	image/pngz
text/latexzapplication/jsonz
image/jpegz
text/plainÚmetadataz4Cell output mimetypes to render without modificationz	text/htmlztext/markdownz8Cell output types to display after escaping with Bleach.c                 C   sd   |j dkr |  |j¡|_||fS |j dkr@|  |j¡|_||fS |j dkr`|  |j¡|_||fS dS )a  
        Sanitize potentially-dangerous contents of the cell.

        Cell Types:
          raw:
            Sanitize literal HTML
          markdown:
            Sanitize literal HTML
          code:
            Sanitize outputs that could result in code execution
        ÚrawÚmarkdownÚcodeN)Z	cell_typeÚsanitize_html_tagsÚsourceÚsanitize_code_outputsÚoutputs)ÚselfÚcellÚ	resourcesZ
cell_index© r    úc/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/nbconvert/preprocessors/sanitize.pyÚpreprocess_cellf   s    


zSanitizeHTML.preprocess_cellc                 C   s–   |D ]Œ}|d dv rq|j }g }|D ]J}|| jv r6q$q$|| jv rd| j d| ¡ |  || ¡||< q$| |¡ q$|D ]}| j d| ¡ ||= qtq|S )zª
        Sanitize code cell outputs.

        Removes 'text/javascript' fields from display_data outputs, and
        runs `sanitize_html_tags` over 'text/html'.
        Úoutput_type)ÚstreamÚerrorzSanitizing %szRemoving %s)ÚdataÚsafe_output_keysÚsanitized_output_typesÚlogÚinfor   Úappend)r   r   Úoutputr&   Z	to_removeÚkeyr    r    r!   r      s     


z"SanitizeHTML.sanitize_code_outputsc                 C   sV   | j | j| j| jdœ}tr4t| jd�}|j|d� ntrF|j| jd� t	|fi |¤ŽS )z=
        Sanitize a string containing raw HTML tags.
        )ÚtagsÚ
attributesÚstripÚstrip_comments©Zallowed_css_properties)Úcss_sanitizer)Ústyles)
r.   r/   r0   r1   Ú_USE_BLEACH_CSS_SANITIZERr   r4   ÚupdateÚ_USE_BLEACH_STYLESr   )r   Zhtml_strÚkwargsr3   r    r    r!   r   ›   s    üzSanitizeHTML.sanitize_html_tagsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r/   r   r	   r   r.   r   r4   r   r0   r1   r   r'   r(   r"   r   r   r    r    r    r!   r   0   sV   ýüüýýöþú	c                   C   s   t rttd�S d S )Nr2   )r5   r   r   r    r    r    r!   Ú_get_default_css_sanitizer¯   s    r=   )r<   ÚwarningsZbleachr   r   r   Z	traitletsr   r   r   r   r	   Úbaser   r5   r7   Zbleach.css_sanitizerr   r   r   ÚImportErrorÚwarnÚDeprecationWarningÚUserWarningÚ__all__r   r=   r    r    r    r!   Ú<module>   s<   ý
ü