a
    ÷£Be®U  ã                	   @   sè   d dl mZ d dlZd dlZd dlmZ d dlmZ d dlmZ e	dƒZ
ddgdgdgd	œZe	d
ƒZd dd„ eed dƒeddƒeddƒƒD ƒ¡Ze de d ej¡ZdZG dd„ deƒZG dd„ dƒZdd„ ZG dd„ dejƒZdS )é    )ÚchainN)Úunescape)Úhtml5lib_shim)Ú
parse_shim)ÚaÚabbrÚacronymÚbZ
blockquoteÚcodeZemÚiZliÚolÚstrongZulÚhrefÚtitle)r   r   r   )ÚhttpÚhttpsÚmailtoÚ c                 C   s   g | ]}t |ƒ‘qS © )Úchr)Ú.0Úcr   r   úS/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/bleach/sanitizer.pyÚ
<listcomp>*   ó    r   é	   é   é   é   é    ú[ú]ú?c                   @   s   e Zd ZdS )ÚNoCssSanitizerWarningN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r#   5   s   r#   c                   @   s0   e Zd ZdZeeed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.

    To use::

        from bleach.sanitizer import Cleaner

        cleaner = Cleaner()

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

    .. Note::

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

    .. Warning::

       This cleaner is not thread-safe--the html parser has internal state.
       Create a separate cleaner per thread!


    FTNc           
      C   sÔ   || _ || _|| _|| _|| _|p$g | _|| _tj| j | jddd�| _	t 
d¡| _tjddddddd�| _|du rÐg }t|tƒr†|}n4t|tƒrºg }| ¡ D ]}	t|	ttfƒrœ| |	¡ qœd|v rÐtjd	td
� dS )a:  Initializes a Cleaner

        :arg set tags: set of allowed 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 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.

        :arg CSSSanitizer css_sanitizer: instance with a "sanitize_css" method for
            sanitizing style attribute values and style text; defaults to None

        F)ÚtagsÚstripZconsume_entitiesZnamespaceHTMLElementsÚetreeÚalwaysT)Zquote_attr_valuesZomit_optional_tagsZescape_lt_in_attrsZresolve_entitiesÚsanitizeZalphabetical_attributesNÚstylez7'style' attribute specified, but css_sanitizer not set.)Úcategory)r(   Ú
attributesÚ	protocolsr)   Ústrip_commentsÚfiltersÚcss_sanitizerr   ZBleachHTMLParserÚparserZgetTreeWalkerÚwalkerZBleachHTMLSerializerÚ
serializerÚ
isinstanceÚlistÚdictÚvaluesÚtupleÚextendÚwarningsÚwarnr#   )
Úselfr(   r/   r0   r)   r1   r2   r3   Zattributes_valuesr:   r   r   r   Ú__init__V   sH    &
üö

þzCleaner.__init__c              	   C   s„   t |tƒs&d|jj›d�d }t|ƒ‚|s.dS | j |¡}t|  |¡| j	| j
| j| j| j| jd�}| jD ]}||d�}qh| j |¡S )zÐCleans text and returns sanitized result as unicode

        :arg str text: text to be cleaned

        :returns: sanitized text as unicode

        :raises TypeError: if ``text`` is not a text type

        zargument cannot be of z type, zmust be of text typer   )ÚsourceÚallowed_tagsr/   Ústrip_disallowed_tagsÚstrip_html_commentsr3   Úallowed_protocols)rA   )r7   ÚstrÚ	__class__r$   Ú	TypeErrorr4   ZparseFragmentÚBleachSanitizerFilterr5   r(   r/   r)   r1   r3   r0   r2   r6   Úrender)r?   ÚtextÚmessageÚdomÚfilteredZfilter_classr   r   r   Úclean«   s*    

ÿÿù
zCleaner.clean)	r$   r%   r&   Ú__doc__ÚALLOWED_TAGSÚALLOWED_ATTRIBUTESÚALLOWED_PROTOCOLSr@   rO   r   r   r   r   r'   9   s   ø
Ur'   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`   | ˆ v r0ˆ |  }t |ƒr$|| ||ƒS ||v r0dS dˆ v r\ˆ d }t |ƒrT|| ||ƒ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   )rV   rW   rX   rY   r   r   rZ   ô   s    z3attributes needs to be a callable, a list or a dictN)rU   r7   r9   r8   Ú
ValueError)r/   rZ   r   rY   r   Úattribute_filter_factoryÑ   s    

r]   c                	   @   sr   e Zd ZdZeeeejej	ej
dddf	dd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )rI   zmhtml5lib Filter that sanitizes text

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

    FTNc                 C   sT   t j | |¡ t|ƒ| _t|ƒ| _t|ƒ| _|| _|	| _	|| _
|| _|
| _|| _dS )a_  Creates a BleachSanitizerFilter instance

        :arg source: html5lib TreeWalker stream as an html5lib TreeWalker

        :arg set allowed_tags: set of allowed 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 allowed_protocols: allowed list of protocols for links; defaults
            to ``bleach.sanitizer.ALLOWED_PROTOCOLS``

        :arg attr_val_is_uri: set of attributes that have URI values

        :arg svg_attr_val_allows_ref: set of SVG attributes that can have
            references

        :arg svg_allow_local_href: set of SVG elements that can have local
            hrefs

        :arg bool strip_disallowed_tags: whether or not to strip disallowed
            tags

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

        :arg CSSSanitizer css_sanitizer: instance with a "sanitize_css" method for
            sanitizing style attribute values and style text; defaults to None

        N)r   ÚFilterr@   Ú	frozensetrB   rE   r]   Úattr_filterrC   rD   Úattr_val_is_uriÚsvg_attr_val_allows_refr3   Úsvg_allow_local_href)r?   rA   rB   r/   rE   ra   rb   rc   rC   rD   r3   r   r   r   r@     s    0


zBleachSanitizerFilter.__init__c                 c   s:   |D ]0}|   |¡}|sqt|tƒr.|E d H  q|V  qd S r[   )Úsanitize_tokenr7   r8   )r?   Útoken_iteratorÚtokenÚretr   r   r   Úsanitize_streamA  s    

z%BleachSanitizerFilter.sanitize_streamc                 c   s”   g }|D ]f}|rP|d dkr*|  |¡ qqhd dd„ |D ƒ¡ddœ}g }|V  n|d dkrh|  |¡ q|V  qd dd„ |D ƒ¡ddœ}|V  dS )	z/Merge consecutive Characters tokens in a streamÚtypeÚ
Charactersr   c                 S   s   g | ]}|d  ‘qS ©Údatar   ©r   Z
char_tokenr   r   r   r   [  r   z:BleachSanitizerFilter.merge_characters.<locals>.<listcomp>)rl   ri   c                 S   s   g | ]}|d  ‘qS rk   r   rm   r   r   r   r   i  r   N)ÚappendÚjoin)r?   re   Zcharacters_bufferrf   Z	new_tokenr   r   r   Úmerge_charactersM  s*    
ÿü
þz&BleachSanitizerFilter.merge_charactersc                 C   s   |   |  tj | ¡¡¡S r[   )rp   rh   r   r^   Ú__iter__)r?   r   r   r   rq   n  s    ÿzBleachSanitizerFilter.__iter__c                 C   sŒ   |d }|dv r>|d | j v r(|  |¡S | jr2dS |  |¡S nJ|dkrr| jsltj|d ddd	œd
�|d< |S dS n|dkr„|  |¡S |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.

        :arg dict token: token to sanitize

        :returns: token or list of tokens

        ri   )ÚStartTagÚEndTagÚEmptyTagÚnameNÚCommentrl   z&quot;z&#x27;)ú"ú')Úentitiesrj   )rB   Úallow_tokenrC   Údisallowed_tokenrD   r   ÚescapeÚsanitize_characters)r?   rf   Ú
token_typer   r   r   rd   s  s"    
ÿ

z$BleachSanitizerFilter.sanitize_tokenc                 C   sÒ   |  dd¡}|s|S t t|¡}||d< d|vr4|S g }t |¡D ]Š}|sLqB| d¡r¼t |¡}|dur¼|dkr‚| dddœ¡ n| d|d	œ¡ |t	|ƒd
 d… }|rB| d|dœ¡ qB| d|dœ¡ qB|S )a½  Handles Characters tokens

        Our overridden tokenizer doesn't do anything with entities. However,
        that means that the serializer will convert all ``&`` in Characters
        tokens to ``&amp;``.

        Since we don't want that, we extract entities here and convert them to
        Entity tokens so the serializer will let them be.

        :arg token: the Characters token to work on

        :returns: a list of tokens

        rl   r   ú&NÚamprj   )ri   rl   ÚEntity)ri   ru   é   )
ÚgetÚINVISIBLE_CHARACTERS_REÚsubÚINVISIBLE_REPLACEMENT_CHARr   Znext_possible_entityÚ
startswithZmatch_entityrn   Úlen)r?   rf   rl   Z
new_tokensÚpartÚentityÚ	remainderr   r   r   r}   ž  s.    

z)BleachSanitizerFilter.sanitize_charactersc                 C   sª   t  |¡}t dd|¡}| dd¡}| ¡ }zt |¡}W n tyN   Y dS 0 |j	rf|j	|v r¦|S n@| 
d¡rt|S d|v r’| d¡d |v r’|S d|v s¢d	|v r¦|S dS )
zÅChecks a uri value to see if it's allowed

        :arg value: the uri value to sanitize
        :arg allowed_protocols: list of allowed protocols

        :returns: allowed value or None

        z[`\000-\040\177-\240\s]+r   u   ï¿½Nú#ú:r   r   r   )r   Zconvert_entitiesÚrer…   ÚreplaceÚlowerr   Úurlparser\   Úschemer‡   Úsplit)r?   rX   rE   Znormalized_uriÚparsedr   r   r   Úsanitize_uri_valueÛ  s*    


ÿþz(BleachSanitizerFilter.sanitize_uri_valuec           	      C   sþ   d|v rúi }|d   ¡ D ]Ø\}}|\}}|  |d ||¡s<q|| jv rb|  || j¡}|du r^q|}|| jv r’t ddt|ƒ¡}| 	¡ }|sŽqn|}d|d f| j
v rÈ|dtjd dffv rÈt d	|¡rÈq|d
krè| jrä| j |¡}nd}|||< q||d< |S )z-Handles the case where we're allowing the tagrl   ru   Nzurl\s*\(\s*[^#\s][^)]+?\)ú )Nr   Zxlinkr   z
^\s*[^#\s])Nr-   r   )Úitemsr`   ra   r•   rE   rb   rŽ   r…   r   r)   rc   r   Ú
namespacesÚsearchr3   Zsanitize_css)	r?   rf   ÚattrsÚnamespaced_nameÚvalÚ	namespaceru   Ú	new_valueÚnew_valr   r   r   rz     s>    

þ
z!BleachSanitizerFilter.allow_tokenc                 C   s  |d }|dkr&d|d › d�|d< n¼|d rÎ|dv s:J ‚g }|d   ¡ D ]b\\}}}|rh|sh|| }}|d u sz|tjvr€|}ntj| › d|› �}| d	|› d
|› d�¡ qJd|d › d |¡› d�|d< nd|d › d�|d< | d¡�r|d d d… › d�|d< d|d< |d= |S )Nri   rs   z</ru   ú>rl   )rr   rt   r�   r–   z="rw   ú<r   ZselfClosingéÿÿÿÿz/>rj   )r—   r   Úprefixesrn   ro   rƒ   )r?   rf   r~   rš   Únsru   Úvr›   r   r   r   r{   Z  s(    
 z&BleachSanitizerFilter.disallowed_token)r$   r%   r&   rP   rQ   rR   rS   r   ra   rb   rc   r@   rh   rp   rq   rd   r}   r•   rz   r{   r   r   r   r   rI   ü   s&   	õ
>!+=:ErI   )Ú	itertoolsr   rŽ   r=   Zxml.sax.saxutilsr   Zbleachr   r   r_   rQ   rR   rS   ro   ÚrangeZINVISIBLE_CHARACTERSÚcompileÚUNICODEr„   r†   ÚUserWarningr#   r'   r]   ZSanitizerFilterrI   r   r   r   r   Ú<module>   s.   ÿý&ÿ +