U
    aF3                     @   s(  d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZmZ ddlmZ ddlmZmZmZ ddlmZmZ ddlmZmZmZ dd	lmZ d
ZddgZddddddgZedZ edZ!edej"Z#edej"Z$ee%edd Z&e'dde'dde'dde'dd e'd!d"e'd#d$e'd%d&e'd'd(e'd)d*e'd+d,e'd-d.e'd/d0iZ(e()d1d2 e*d3D  ee%ed4d5 Z+e'dd e'd!d"e'd#d$iZ,d6d7 Z-d8d9 Z.d:d; Z/d<d= Z0edQd?d@Z1G dAdB dBeZ2dCdD Z3edEdF Z4edGdH Z5dIdJ Z6edRdKdLZ7dMdN Z8dOdP Z9dS )Sz'HTML utilities suitable for global use.    N)
HTMLParser)	parse_qslquoteunquote	urlencodeurlsplit
urlunsplit)punycode)Promise	keep_lazykeep_lazy_text)RFC3986_GENDELIMSRFC3986_SUBDELIMS)SafeData
SafeString	mark_safe)normalize_newlinesz.,:;!)())[]z&middot;*u   •z&#149;z&bull;z&#8226;z&(?!(\w+|#\d+);)z([\s<>"']+)z^https?://\[?\wz>^www\.|^(?!http)\w[^@]+\.(com|edu|gov|int|mil|net|org)($|/.*)$c                 C   s   t tt| S )a  
    Return the given text with ampersands, quotes and angle brackets encoded
    for use in HTML.

    Always escape input, even if it's already escaped and marked as such.
    This may result in double-escaping. If this is a concern, use
    conditional_escape() instead.
    )r   htmlescapestrtext r   /home/adriano.carvalho/ftp/files/BrinquedotecaVirtual/brinquedotecavirtual/venv/lib/python3.8/site-packages/django/utils/html.pyr      s    
r   \z\u005C'z\u0027"z\u0022>z\u003E<z\u003C&z\u0026=z\u003D-z\u002D;z\u003B`z\u0060u    z\u2028u    z\u2029c                 c   s"   | ]}t d | d| fV  qdS )z%cz\u%04XN)ord).0zr   r   r   	<genexpr>;   s     r,       c                 C   s   t t| tS )z4Hex encode characters for use in JavaScript strings.)r   r   	translate_js_escapesvaluer   r   r   escapejs>   s    r2   c                 C   s0   ddl m} tj| |dt}td|t|S )z
    Escape all the HTML/XML special characters with their unicode escapes, so
    value is safe to be output anywhere except for inside a tag attribute. Wrap
    the escaped JSON in a script tag.
    r   )DjangoJSONEncoder)clsz3<script id="{}" type="application/json">{}</script>)Zdjango.core.serializers.jsonr3   jsondumpsr.   _json_script_escapesformat_htmlr   )r1   Z
element_idr3   Zjson_strr   r   r   json_scriptK   s     r9   c                 C   s0   t | trt| } t| dr$|  S t| S dS )z
    Similar to escape(), except that it doesn't operate on pre-escaped strings.

    This function relies on the __html__ convention used both by Django's
    SafeData class and by third-party libraries like markupsafe.
    __html__N)
isinstancer
   r   hasattrr:   r   r   r   r   r   conditional_escapeY   s
    

r=   c                 O   s,   t t|}dd | D }t| j||S )z
    Similar to str.format, but pass all arguments through conditional_escape(),
    and call mark_safe() on the result. This function should be used instead
    of str.format or % interpolation to build up small HTML fragments.
    c                 S   s   i | ]\}}|t |qS r   )r=   )r*   kvr   r   r   
<dictcomp>o   s      zformat_html.<locals>.<dictcomp>)mapr=   itemsr   format)format_stringargskwargsZ	args_safeZkwargs_safer   r   r   r8   h   s    
r8   c                    s    t t|  fdd|D S )a  
    A wrapper of format_html, for the common case of a group of arguments that
    need to be formatted using the same format string, and then joined using
    'sep'. 'sep' is also passed through conditional_escape.

    'args_generator' should be an iterator that returns the sequence of 'args'
    that will be passed to format_html.

    Example:

      format_html_join('
', "<li>{} {}</li>", ((u.first_name, u.last_name)
                                                  for u in users))
    c                 3   s   | ]}t  f| V  qd S N)r8   )r*   rE   rD   r   r   r,      s   z#format_html_join.<locals>.<genexpr>)r   r=   join)seprD   Zargs_generatorr   rH   r   format_html_joins   s    rK   Fc                 C   sD   t | } tdt| }|r,dd |D }ndd |D }d|S )z$Convert newlines into <p> and <br>s.z
{2,}c                 S   s    g | ]}d t |dd qS z	<p>%s</p>
z<br>)r   replacer*   pr   r   r   
<listcomp>   s     zlinebreaks.<locals>.<listcomp>c                 S   s   g | ]}d | dd qS rL   rN   rO   r   r   r   rQ      s     z

)r   resplitr   rI   )r1   
autoescapeZparasr   r   r   
linebreaks   s    rV   c                       s<   e Zd Z fddZdd Zdd Zdd Zd	d
 Z  ZS )
MLStripperc                    s    t  jdd |   g | _d S )NF)convert_charrefs)super__init__resetfedself	__class__r   r   rZ      s    zMLStripper.__init__c                 C   s   | j | d S rG   r\   append)r^   dr   r   r   handle_data   s    zMLStripper.handle_datac                 C   s   | j d|  d S )Nz&%s;ra   r^   namer   r   r   handle_entityref   s    zMLStripper.handle_entityrefc                 C   s   | j d|  d S )Nz&#%s;ra   re   r   r   r   handle_charref   s    zMLStripper.handle_charrefc                 C   s   d | jS )N )rI   r\   r]   r   r   r   get_data   s    zMLStripper.get_data)	__name__
__module____qualname__rZ   rd   rg   rh   rj   __classcell__r   r   r_   r   rW      s
   rW   c                 C   s    t  }||  |  | S )z<
    Internal tag stripping utility used by strip_tags.
    )rW   feedcloserj   )r1   sr   r   r   _strip_once   s    
rr   c                 C   s@   t | } d| kr<d| kr<t| }| d|dkr6q<|} q| S )z-Return the given HTML with all tags stripped.r#   r"   )r   rr   count)r1   	new_valuer   r   r   
strip_tags   s    ru   c                 C   s   t ddt| S )z7Return the given HTML with spaces between tags removed.z>\s+<z><)rS   subr   r0   r   r   r   strip_spaces_between_tags   s    rw   c                 C   s   dd }zt | \}}}}}W n tk
r:   ||  Y S X zt|}W n tk
rd   ||  Y S X |rdd t|ddD }t|}||}||}t|||||fS )z'Quote a URL if it isn't already quoted.c                 S   s   t | } t| tt d dS )N~)safe)r   r   r   r   )segmentr   r   r   unquote_quote   s    z%smart_urlquote.<locals>.unquote_quotec                 S   s$   g | ]}t |d  t |d fqS )r      )r   )r*   qr   r   r   rQ      s   z"smart_urlquote.<locals>.<listcomp>T)keep_blank_values)r   
ValueErrorr	   UnicodeErrorr   r   r   )urlr{   schemenetlocpathqueryfragmentZquery_partsr   r   r   smart_urlquote   s"    
r   c              	   C   s  t | t}|fdd}dd }dd }tt| }t|D ]\}	}
d|
ks`d|
ks`d	|
krd
|
d
  }}}||||\}}}d}|rdnd
}t|rtt	
|}nzt|rtdt	
| }n\d	|kr&||r&|dd\}}zt|}W n tk
r   Y q<Y nX d||f }d
}|r||}|rZ|sZt|t| }}t|}dt|||f }td|||f ||	< n&|rt|
||	< n|rt|
||	< q<|rt|
||	< q<|r<t|
||	< q<d
|S )a  
    Convert any URLs in text into clickable links.

    Works on http://, https://, www. links, and also on links ending in one of
    the original seven gTLDs (.com, .edu, .gov, .int, .mil, .net, and .org).
    Links can have trailing punctuation (periods, commas, close-parens) and
    leading punctuation (opening parens) and it'll still do the right thing.

    If trim_url_limit is not None, truncate the URLs in the link text longer
    than this limit to trim_url_limit - 1 characters and append an ellipsis.

    If nofollow is True, give the links a rel="nofollow" attribute.

    If autoescape is True, autoescape the link text and URLs.
    c                 S   s2   |d kst | |kr| S d| d td|d   S )Nu   %s…r   r|   )lenmax)xlimitr   r   r   trim_url   s    zurlize.<locals>.trim_urlc                 S   s   d}|rd}t D ]n\}}||r>|t|d }| |7 } d}||r||||d kr|dt|  }|| }d}qt|}|t}||kr|t|d | }|dt|t|  }d}q| ||fS )zr
        Trim trailing and wrapping punctuation from `middle`. Return the items
        of the new state.
        TFNr|   )	WRAPPING_PUNCTUATION
startswithr   endswithrs   r   unescaperstripTRAILING_PUNCTUATION_CHARS)leadmiddletrailZtrimmed_somethingZopeningclosingZmiddle_unescapedstrippedr   r   r   trim_punctuation   s*    



z urlize.<locals>.trim_punctuationc                 S   sd   d| ks|  ds| dr dS z| d\}}W n tk
rH   Y dS X d|ks\| dr`dS dS )z1Return True if value looks like an email address.@F.T)r   r   rT   r   )r1   p1p2r   r   r   is_email_simple   s    zurlize.<locals>.is_email_simpler   r   :ri   Nz rel="nofollow"z	http://%sr|   zmailto:%s@%sz<a href="%s"%s>%s</a>z%s%s%s)r;   r   word_split_rerT   r   	enumeratesimple_url_rematchr   r   r   simple_url_2_rersplitr	   r   r   r   rI   )r   Ztrim_url_limitZnofollowrU   Z
safe_inputr   r   r   wordsiwordr   r   r   r   Znofollow_attrlocaldomainZtrimmedr   r   r   urlize   sN    
 


r   c                 C   s   |  ddS )z
    Avoid text wrapping in the middle of a phrase by adding non-breaking
    spaces where there previously were normal spaces.
         rR   r0   r   r   r   avoid_wrapping]  s    r   c                    sR   d| j krtd| j d| j kr0td| j | j  fdd| _dd | _| S )z
    A decorator that defines the __html__ method. This helps non-Django
    templates to detect classes whose __str__ methods return SafeString.
    r:   z;can't apply @html_safe to %s because it defines __html__().__str__zAcan't apply @html_safe to %s because it doesn't define __str__().c                    s   t  | S rG   )r   r]   Z	klass_strr   r   <lambda>u      zhtml_safe.<locals>.<lambda>c                 S   s   t | S rG   )r   r]   r   r   r   r   v  r   )__dict__r   rk   r   r:   )klassr   r   r   	html_safee  s     


r   )F)NFF):__doc__r   r5   rS   html.parserr   urllib.parser   r   r   r   r   r   Zdjango.utils.encodingr	   Zdjango.utils.functionalr
   r   r   Zdjango.utils.httpr   r   Zdjango.utils.safestringr   r   r   Zdjango.utils.textr   r   r   ZDOTScompileZunencoded_ampersands_rer   
IGNORECASEr   r   r   r   r)   r/   updateranger2   r7   r9   r=   r8   rK   rV   rW   rr   ru   rw   r   r   r   r   r   r   r   r   <module>   s    


            
   


#t