B
    KZ                 @   sX   d Z ddlmZmZmZmZ ddlmZmZm	Z	m
Z
mZ ddlmZ G dd deZdS )	zD
NBConvert Preprocessor for sanitizing HTML rendering of notebooks.
    )ALLOWED_ATTRIBUTESALLOWED_STYLESALLOWED_TAGSclean)AnyBoolListSetUnicode   )Preprocessorc               @   s   e 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 )SanitizeHTMLTzAllowed HTML tag attributes)ZconfigZdefault_valuehelpzList of HTML tags to allowz0Allowed CSS styles if <style> tag is whitelistedFz:If True, remove unsafe markup entirely instead of escapingz)If True, strip comments from escaped HTMLZ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)selfZcellZ	resourcesZ
cell_index r   ?lib/python3.7/site-packages/nbconvert/preprocessors/sanitize.pypreprocess_cellJ   s    


zSanitizeHTML.preprocess_cellc             C   s   x|D ]}|d dkrq|j }g }xR|D ]J}|| jkr:q(q(|| jkrh| jd|  | || ||< q(|| q(W x"|D ]}| jd|  ||= q|W qW |S )z
        Sanitize code cell outputs.

        Removes 'text/javascript' fields from display_data outputs, and
        runs `sanitize_html_tags` over 'text/html'.
        Z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   c   s     




z"SanitizeHTML.sanitize_code_outputsc             C   s   t || j| j| j| j| jdS )z=
        Sanitize a string containing raw HTML tags.
        )tags
attributesstylesstripstrip_comments)r   r#   r$   r%   r&   r'   )r   Zhtml_strr   r   r   r      s    zSanitizeHTML.sanitize_html_tagsN)__name__
__module____qualname__r   r   r$   r   r
   r   r#   r   r%   r   r&   r'   r	   r   r   r   r   r   r   r   r   r   r      sN   r   N)__doc__Zbleachr   r   r   r   Z	traitletsr   r   r   r	   r
   baser   r   r   r   r   r   <module>   s   