U
    nufW                     @   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 nb ek
r   z(ddlmZ dZd
Zejdedd W n$ ek
r   ejdedd Y nX Y nX 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d
dddddh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metadataz
text/plainz
text/latexzapplication/jsonz	image/pngz
image/jpegz4Cell 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
        rawZmarkdowncodeN)Z	cell_typesanitize_html_tagssourcesanitize_code_outputsoutputs)selfcell	resourcesZ
cell_index r   D/tmp/pip-unpacked-wheel-nxpdnluh/nbconvert/preprocessors/sanitize.pypreprocess_cellf   s    


zSanitizeHTML.preprocess_cellc                 C   s   |D ]}|d dkrq|j }g }|D ]F}|| jkr4q$|| jkr`| jd| | || ||< q$|| q$|D ]}| jd| ||= qpq|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   sR   | j | j| j| jd}tr4t| jd}|j|d ntrF|j| jd t	|f|S )z=
        Sanitize a string containing raw HTML tags.
        )tags
attributesstripstrip_commentsZallowed_css_properties)css_sanitizer)styles)
r-   r.   r/   r0   _USE_BLEACH_CSS_SANITIZERr   r3   update_USE_BLEACH_STYLESr   )r   Zhtml_strkwargsr2   r   r   r    r      s    zSanitizeHTML.sanitize_html_tagsN)__name__
__module____qualname____doc__r   r   r.   r   r	   r   r-   r   r3   r   r/   r0   r   r&   r'   r!   r   r   r   r   r   r    r   0   sb   	c                   C   s   t rttdS d S )Nr1   )r4   r   r   r   r   r   r    _get_default_css_sanitizer   s    
r<   )r;   warningsZbleachr   r   r   Z	traitletsr   r   r   r   r	   baser   r4   r6   Zbleach.css_sanitizerr   r   r   ImportErrorwarnDeprecationWarningUserWarning__all__r   r<   r   r   r   r    <module>   s>   
 