o
    7?e/                     @   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 g d
ZddgdgdgdZg Zg d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bZ
blockquotecodeZemiZliolstrongZ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)ZnamespaceHTMLElementsetreealways)Zquote_attr_valuesZomit_optional_tagssanitizeZalphabetical_attributesN)tags
attributesstyles	protocolsstripstrip_commentsfiltersr   
HTMLParserparserZgetTreeWalkerwalkerr   
serializer)selfr   r   r   r   r   r    r!    r'   e/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/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}q&| 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)r+   )r   r#   ZparseFragmentBleachSanitizerFilterr$   r   r   r    r   r   r   r!   r%   render)r&   textdomfilteredZfilter_classr'   r'   r(   cleanx   s"   
zCleaner.clean)
__name__
__module____qualname____doc__ALLOWED_TAGSALLOWED_ATTRIBUTESALLOWED_STYLESALLOWED_PROTOCOLSr)   r7   r'   r'   r'   r(   r   /   s    
4r   c                    sH   t  r S t tr fdd}|S t tr  fdd}|S td)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`   |  v r |  }t |r|| ||S ||v rdS d v r. d }t |r*|| ||S ||v S dS )NT*F)callable)tagattrvalueZattr_valr   r'   r(   _attr_filter   s   z.attribute_filter_factory.<locals>._attr_filterc                    s   | v S )Nr'   )rB   rC   rD   rE   r'   r(   rF      s   z3attributes needs to be a callable, a list or a dict)rA   
isinstancedictlist
ValueError)r   rF   r'   rE   r(   attribute_filter_factory   s   

rK   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 )r2   zmhtml5lib Filter that sanitizes text

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

    FTc                    s.   t || _|| _|| _tt| j|fi |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

        )rK   attr_filterr,   r-   superr2   r)   )r&   r+   r   r,   r-   kwargs	__class__r'   r(   r)      s   
zBleachSanitizerFilter.__init__c                 C   sn   |d }|dv r*|d | j v r| |S | jrdS d|v r%t|d |d< | |S |dkr5| js3|S dS |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)ZStartTagZEndTagZEmptyTagnamedataCommentN)r.   allow_tokenr,   r	   Zdisallowed_tokenr-   )r&   token
token_typer'   r'   r(   sanitize_token   s   

z$BleachSanitizerFilter.sanitize_tokenc           	      C   s  d|v ri }|d   D ]w\}}|\}}| |d ||sq|| jv rEtddt| }|dd}td|rE|	dd | j
vrEq|| jv r\td	d
t|}| }|sZq|}d|d f| jv rv|dtd dffv rvtd|rvq|dkr| |}|||< qt||d< |S )z-Handles the case where we're allowing the tagrS   rR   u   [` - - \s]+r*   u   �z^[a-z0-9][-+.a-z0-9]*::r   zurl\s*\(\s*[^#\s][^)]+?\) N)Nr   Zxlinkr   z
^\s*[^#\s])Nstyle)itemsrL   Zattr_val_is_uriresubr   lowerreplacematchsplitr0   Zsvg_attr_val_allows_refr   Zsvg_allow_local_hrefr   searchsanitize_cssr	   )	r&   rV   attrsZnamespaced_nameval	namespacerR   Zval_unescapednew_valr'   r'   r(   rU     sJ   



z!BleachSanitizerFilter.allow_tokenc                 C   s   t dd|}|d}t d}|D ]
}||s dS qt d|s(dS g }t d|D ],\}}|s7q0| | jv rJ||d | d  q0| | j	v r\||d | d  q0d
|S )	zSanitizes css in style tagszurl\s*\(\s*[^\s)]+?\s*\)\s*rZ   ;zI^([-/:,#%.'"\sa-zA-Z0-9!]|\w-\w|'[\s\w]+'\s*|"[\s\w]+"|\([\d,%\.\s]+\))*$r*   z ^\s*([-\w]+\s*:[^:;]*(;\s*|$))*$z([-\w]+)\s*:\s*([^:;]*)z: )r]   compiler^   rb   ra   findallr_   r/   appendr1   join)r&   r[   partsZgauntletpartr7   proprD   r'   r'   r(   rd   P  s*   


z"BleachSanitizerFilter.sanitize_css)
r8   r9   r:   r;   r=   r)   rX   rU   rd   __classcell__r'   r'   rO   r(   r2      s     "Cr2   )
__future__r   r]   Z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	   r<   r=   r>   r?   objectr   rK   Filterr2   r'   r'   r'   r(   <module>   s&    m)