a
    Dg!                     @   s   d Z ddlZddlmZmZmZmZ ddlmZ g dZ	e
dZdd Zd	d
 Zei ZeedeieeeeeeeeeeeeeeddddeeeeeeedZG dd dZdS )ze
A generic HTML whitelisting engine, designed to accommodate subclassing to override
specific rules.
    N)BeautifulSoupCommentNavigableStringTag)escape)httphttpsftpmailtotelz^[a-z0-9][-+.a-z0-9]*:c                 C   sp   |   }|dd}|dd}|dd}tdd|}|d	d}t|rl|d
dd }|tvrld S | S )Nz&lt;<z&gt;>z&amp;&z[`\000-\040\177-\240\s]+ u   �:   r   )lowerreplaceresubPROTOCOL_REmatchsplitALLOWED_URL_SCHEMES)Z
url_stringZ	unescapedprotocol r   P/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/whitelist.py	check_url   s    
r   c                    s    fdd}|S )af  
    Generator for functions that can be used as entries in Whitelister.element_rules.
    These functions accept a tag, and modify its attributes by looking each attribute
    up in the 'allowed_attrs' dict defined here:
    * if the lookup fails, drop the attribute
    * if the lookup returns a callable, replace the attribute with the result of calling
      it - for example `{'title': uppercase}` will replace 'title' with the result of
      uppercasing the title. If the callable returns None, the attribute is dropped.
    * if the lookup returns a truthy value, keep the attribute; if falsy, drop it
    c                    s\   t | j D ]H\}} |}|rPt|rV||}|d u rD| |= qN|| |< qVq| |= qd S N)listattrsitemsgetcallable)tagattrvalrulenew_valallowed_attrsr   r   fn-   s    

zattribute_rule.<locals>.fnr   )r*   r+   r   r)   r   attribute_rule!   s    r,   hrefT)srcwidthheightZalt)z
[document]abbrdivemh1h2h3h4h5h6hriimgliolpstrongr   supulc                   @   s8   e Zd ZeZdd Zdd Zdd Zdd Zd	d
 Z	dS )Whitelisterc                 C   s"   t |d}| || |jtdS )zQClean up an HTML string to contain just the allowed elements /
        attributeszhtml.parser)	formatter)r   
clean_nodedecoder   )selfhtmldocr   r   r   cleanc   s    
zWhitelister.cleanc                 C   s@   t |tr| || n$t |tr0| || n| || dS )z'Clean a BeautifulSoup document in-placeN)
isinstancer   clean_string_noder   clean_tag_nodeclean_unknown_noderI   rK   noder   r   r   rG   p   s
    

zWhitelister.clean_nodec                 C   s   t |tr|  d S d S r   )rM   r   extractrQ   r   r   r   rN   |   s    
zWhitelister.clean_string_nodec                 C   sV   t |jD ]}| || q
z| j|j }W n tyH   |  Y d S 0 || d S r   )r   contentsrG   element_rulesnameKeyErrorunwrap)rI   rK   r$   childr'   r   r   r   rO      s    zWhitelister.clean_tag_nodec                 C   s   |   d S r   )Z	decomposerQ   r   r   r   rP      s    zWhitelister.clean_unknown_nodeN)
__name__
__module____qualname__DEFAULT_ELEMENT_RULESrU   rL   rG   rN   rO   rP   r   r   r   r   rE   `   s   	rE   )__doc__r   Zbs4r   r   r   r   Zdjango.utils.htmlr   r   compiler   r   r,   Zallow_without_attributesr]   rE   r   r   r   r   <module>   sD   
 
