U
    Uc/                     @   s   d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d d	lmZ d
dddddddddddgZddgdgdgdZg ZdddgZG dd deZdd ZG d d! d!e
jZdS )"    )unicode_literalsN)unescape)html5lib)
namespaces)	sanitizer)HTMLSerializer)force_unicode)alphabetize_attributesaabbracronymb
blockquotecodeemiliolstrongulhreftitle)r
   r   r   httphttpsmailtoc                   @   s0   e Zd ZdZeeeedddfddZdd Z	dS )	Cleanera  Cleaner for cleaning HTML fragments of malicious content

    This cleaner is a security-focused function whose sole purpose is to remove
    malicious content from a string such that it can be displayed as content in
    a web page.

    This cleaner is not designed to use to transform content to be used in
    non-web-page contexts.

    To use::

        from bleach.sanitizer import Cleaner

        cleaner = Cleaner()

        for text in all_the_yucky_things:
            sanitized = cleaner.clean(text)

    FTNc                 C   s^   || _ || _|| _|| _|| _|| _|p*g | _tjdd| _	t
d| _tddddd| _dS )a  Initializes a Cleaner

        :arg list tags: allowed list of tags; defaults to
            ``bleach.sanitizer.ALLOWED_TAGS``

        :arg dict attributes: allowed attributes; can be a callable, list or dict;
            defaults to ``bleach.sanitizer.ALLOWED_ATTRIBUTES``

        :arg list styles: allowed list of css styles; defaults to
            ``bleach.sanitizer.ALLOWED_STYLES``

        :arg list protocols: allowed list of protocols for links; defaults
            to ``bleach.sanitizer.ALLOWED_PROTOCOLS``

        :arg bool strip: whether or not to strip disallowed elements

        :arg bool strip_comments: whether or not to strip HTML comments

        :arg list filters: list of html5lib Filter classes to pass streamed content through

            .. seealso:: http://html5lib.readthedocs.io/en/latest/movingparts.html#filters

            .. Warning::

               Using filters changes the output of ``bleach.Cleaner.clean``.
               Make sure the way the filters change the output are secure.

        F)namespaceHTMLElementsetreealways)quote_attr_valuesomit_optional_tagssanitizealphabetical_attributesN)tags
attributesstyles	protocolsstripstrip_commentsfiltersr   
HTMLParserparsergetTreeWalkerwalkerr   
serializer)selfr#   r$   r%   r&   r'   r(   r)    r0   H/tmp/pip-unpacked-wheel-g8kmtpbc/tensorboard/_vendor/bleach/sanitizer.py__init__D   s    
zCleaner.__init__c              
   C   sh   |sdS t |}| j|}t| || j| j| j| j| j	| j
g d}| jD ]}||d}qL| j|S )zCleans text and returns sanitized result as unicode

        :arg str text: text to be cleaned

        :returns: sanitized text as unicode

         )sourcer$   strip_disallowed_elementsstrip_html_commentsallowed_elementsallowed_css_propertiesallowed_protocolsallowed_svg_properties)r4   )r   r+   parseFragmentBleachSanitizerFilterr-   r$   r'   r(   r#   r%   r&   r)   r.   render)r/   textdomfilteredZfilter_classr0   r0   r1   cleanx   s"    
zCleaner.clean)
__name__
__module____qualname____doc__ALLOWED_TAGSALLOWED_ATTRIBUTESALLOWED_STYLESALLOWED_PROTOCOLSr2   rA   r0   r0   r0   r1   r   /   s      
4r   c                    sL   t  r S t tr& fdd}|S t tr@ fdd}|S tddS )a0  Generates attribute filter function for the given attributes value

    The attributes value can take one of several shapes. This returns a filter
    function appropriate to the attributes value. One nice thing about this is
    that there's less if/then shenanigans in the ``allow_token`` method.

    c                    s`   |  kr0 |  }t |r$|| ||S ||kr0dS d kr\ d }t |rT|| ||S ||kS dS )NT*F)callable)tagattrvalueZattr_valr$   r0   r1   _attr_filter   s    z.attribute_filter_factory.<locals>._attr_filterc                    s   | kS )Nr0   )rL   rM   rN   rO   r0   r1   rP      s    z3attributes needs to be a callable, a list or a dictN)rK   
isinstancedictlist
ValueError)r$   rP   r0   rO   r1   attribute_filter_factory   s    

rU   c                       s@   e Zd ZdZeddf fdd	Zdd Zdd	 Zd
d Z  Z	S )r<   zmhtml5lib Filter that sanitizes text

    This filter can be used anywhere html5lib filters can be used.

    FTc                    s*   t || _|| _|| _tt| j|f|S )a   Creates a BleachSanitizerFilter instance

        :arg Treewalker source: stream

        :arg list tags: allowed list of tags; defaults to
            ``bleach.sanitizer.ALLOWED_TAGS``

        :arg dict attributes: allowed attributes; can be a callable, list or dict;
            defaults to ``bleach.sanitizer.ALLOWED_ATTRIBUTES``

        :arg list styles: allowed list of css styles; defaults to
            ``bleach.sanitizer.ALLOWED_STYLES``

        :arg list protocols: allowed list of protocols for links; defaults
            to ``bleach.sanitizer.ALLOWED_PROTOCOLS``

        :arg bool strip_disallowed_elements: whether or not to strip disallowed
            elements

        :arg bool strip_html_comments: whether or not to strip HTML comments

        )rU   attr_filterr5   r6   superr<   r2   )r/   r4   r$   r5   r6   kwargs	__class__r0   r1   r2      s    
zBleachSanitizerFilter.__init__c                 C   sp   |d }|dkrT|d | j kr(| |S | jr0qld|krHt|d |d< | |S n|dkrh| jsl|S n|S dS )a~  Sanitize a token either by HTML-encoding or dropping.

        Unlike sanitizer.Filter, allowed_attributes can be a dict of {'tag':
        ['attribute', 'pairs'], 'tag': callable}.

        Here callable is a function with two arguments of attribute name and
        value. It should return true of false.

        Also gives the option to strip tags instead of encoding.

        type)StartTagEndTagEmptyTagnamedataCommentN)r7   allow_tokenr5   r	   Zdisallowed_tokenr6   )r/   token
token_typer0   r0   r1   sanitize_token   s    
z$BleachSanitizerFilter.sanitize_tokenc           	      C   s  d|kri }|d   D ]\}}|\}}| |d ||s>q|| jkrtddt| }|dd}td|r|	dd | j
krq|| jkrtd	d
t|}| }|sqn|}d|d f| jkr|dtd dffkrtd|rq|dkr| |}|||< qt||d< |S )z-Handles the case where we're allowing the tagr`   r_   u   [` - - \s]+r3   u   �z^[a-z0-9][-+.a-z0-9]*::r   zurl\s*\(\s*[^#\s][^)]+?\) N)Nr   xlinkr   z
^\s*[^#\s])Nstyle)itemsrV   Zattr_val_is_uriresubr   lowerreplacematchsplitr9   Zsvg_attr_val_allows_refr'   Zsvg_allow_local_hrefr   searchsanitize_cssr	   )	r/   rc   attrsZnamespaced_nameval	namespacer_   Zval_unescapednew_valr0   r0   r1   rb     sF    





z!BleachSanitizerFilter.allow_tokenc                 C   s   t dd|}|d}t d}|D ]}||s* dS q*t d|sPdS g }t d|D ]X\}}|snq`| | jkr||d | d  q`| | j	kr`||d | d  q`d
|S )	zSanitizes css in style tagszurl\s*\(\s*[^\s)]+?\s*\)\s*rg   ;zI^([-/:,#%.'"\sa-zA-Z0-9!]|\w-\w|'[\s\w]+'\s*|"[\s\w]+"|\([\d,%\.\s]+\))*$r3   z ^\s*([-\w]+\s*:[^:;]*(;\s*|$))*$z([-\w]+)\s*:\s*([^:;]*)z: )rk   compilerl   rp   ro   findallrm   r8   appendr:   join)r/   ri   partsZgauntletpartrA   proprN   r0   r0   r1   rr   P  s&    

z"BleachSanitizerFilter.sanitize_css)
rB   rC   rD   rE   rG   r2   re   rb   rr   __classcell__r0   r0   rY   r1   r<      s     "Cr<   )
__future__r   rk   xml.sax.saxutilsr   Ztensorboard._vendorr   Z&tensorboard._vendor.html5lib.constantsr   Z$tensorboard._vendor.html5lib.filtersr   Z'tensorboard._vendor.html5lib.serializerr   Z#tensorboard._vendor.bleach.encodingr   Z tensorboard._vendor.bleach.utilsr	   rF   rG   rH   rI   objectr   rU   Filterr<   r0   r0   r0   r1   <module>   s<   
m)