

_VK9                 @   s   d  Z  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
 m Z d d l m Z d d l m Z m Z d d	 l m Z m Z 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  d d d d d d d d g Z! dd de df dg dh di g Z" d d d  d! d" d# g Z# e j$ d$  Z% e j$ d%  Z& e j$ d& e j'  Z( e j$ d' e j'  Z) e j$ d(  Z* e j$ d)  Z+ e j$ d* e j'  Z, e j$ d+ d, j- d- d.   e# D  e j.  Z/ e j$ d/  Z0 d0 d1   Z1 e e1 e j2 e  Z1 i d2 e3 d3  6d4 e3 d  6d5 e3 d  6d6 e3 d  6d7 e3 d  6d8 e3 d9  6d: e3 d;  6d< e3 d=  6d> e3 d  6d? e3 d@  6dA e3 dB  6Z4 e4 j5 dC d.   e6 dD  D  dE dF   Z7 e e7 e j2 e  Z7 dG dH   Z8 dI dJ   Z9 dK dL   Z: dM dN dO  Z; e e; e j2  Z; GdP dQ   dQ e   Z< dR dS   Z= dT dU   Z> e e>  Z> dV dW   Z? e e? e j2  Z? dX dY   Z@ e e@ e j2  Z@ dZ d[   ZA e eA e j2  ZA d\ d]   ZB d dM dM d^ d_  ZC e eC e j2  ZC d` da   ZD db dc   ZE d S)jz'HTML utilities suitable for global use.    )unicode_literalsN)six)RemovedInDjango110Warning)	force_str
force_text)
allow_lazy)RFC3986_GENDELIMSRFC3986_SUBDELIMS)SafeDataSafeText	mark_safe)	parse_qslquoteunquote	urlencodeurlsplit
urlunsplit)normalize_newlines   )HTMLParseError
HTMLParser.,:;z.)"'!()<>[]&lt;&gt;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)($|/.*)$z^\S+@\S+\.\S+$z(<a [^>]*?)target=[^\s>]+zb(?:<br clear="all">|<i><\/i>|<b><\/b>|<em><\/em>|<strong><\/strong>|<\/?smallcaps>|<\/?uppercase>)z%((?:<p>(?:%s).*?[a-zA-Z].*?</p>\s*)+)|c             c   s   |  ] } t  j |  Vq d  S)N)reescape).0x r,   3/tmp/pip-build-ghmbqnp_/Django/django/utils/html.py	<genexpr>&   s   r.   z((?:<p>(?:&nbsp;|\s|<br \/>)*?</p>\s*)+\Zc             C   sL   t  t |   j d d  j d d  j d d  j d d  j d	 d
   S)a*  
    Returns the given text with ampersands, quotes and angle brackets encoded
    for use in HTML.

    This function always escapes its 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.
    &z&amp;r    z&lt;r!   z&gt;r   z&quot;r   z&#39;)r   r   replace)textr,   r,   r-   r)   +   s    	'r)   z\u005C\z\u0027z\u0022z\u003Ez\u003Cz\u0026r/   z\u003D=z\u002D-z\u003Bz\u2028u    z\u2029u    c             c   s)   |  ] } t  d  |  d | f Vq d S)z%cz\u%04XN)ord)r*   zr,   r,   r-   r.   G   s        c             C   s   t  t |   j t   S)z5Hex encodes characters for use in JavaScript strings.)r   r   	translate_js_escapes)valuer,   r,   r-   escapejsJ   s    r;   c             C   s'   t  |  d  r |  j   St |   Sd 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)hasattrr<   r)   )r1   r,   r,   r-   conditional_escapeP   s    
r>   c             O   sA   t  t |  } d d   t j |  D } t |  j | |    S)z
    Similar to str.format, but passes all arguments through conditional_escape,
    and calls '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 Sr,   )r>   )r*   kvr,   r,   r-   
<dictcomp>d   s   	 zformat_html.<locals>.<dictcomp>)mapr>   r   	iteritemsr   format)format_stringargskwargsZ	args_safeZkwargs_safer,   r,   r-   format_html]   s    rH   c                s,   t  t |   j   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    t |   Vq d  S)N)rH   tuple)r*   rF   )rE   r,   r-   r.   w   s   z#format_html_join.<locals>.<genexpr>)r   r>   join)seprE   Zargs_generatorr,   )rE   r-   format_html_joinh   s    rL   Fc             C   sZ   t  |   }  t j d |   } | r: d d   | D } n d d   | D } d j |  S)z'Converts newlines into <p> and <br />s.z
{2,}c             S   s,   g  |  ]" } d  t  |  j d d   q S)z	<p>%s</p>
z<br />)r)   r0   )r*   pr,   r,   r-   
<listcomp>   s   	 zlinebreaks.<locals>.<listcomp>c             S   s&   g  |  ] } d  | j  d d   q S)z	<p>%s</p>rM   z<br />)r0   )r*   rN   r,   r,   r-   rO      s   	 z

)r   r(   splitrJ   )r:   
autoescapeZparasr,   r,   r-   
linebreaks{   s    rR   c               @   sL   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 d
   Z d S)
MLStripperc             C   s$   t  j |   |  j   g  |  _ d  S)N)r   __init__resetfed)selfr,   r,   r-   rT      s    
zMLStripper.__init__c             C   s   |  j  j |  d  S)N)rV   append)rW   dr,   r,   r-   handle_data   s    zMLStripper.handle_datac             C   s   |  j  j d |  d  S)Nz&%s;)rV   rX   )rW   namer,   r,   r-   handle_entityref   s    zMLStripper.handle_entityrefc             C   s   |  j  j d |  d  S)Nz&#%s;)rV   rX   )rW   r[   r,   r,   r-   handle_charref   s    zMLStripper.handle_charrefc             C   s   d j  |  j  S)N )rJ   rV   )rW   r,   r,   r-   get_data   s    zMLStripper.get_dataN)__name__
__module____qualname__rT   rZ   r\   r]   r_   r,   r,   r,   r-   rS      s
   rS   c             C   su   t    } y | j |   Wn t k
 r2 |  SYn Xy | j   Wn# t k
 rf | j   | j SYn X| j   Sd S)z<
    Internal tag stripping utility used by strip_tags.
    N)rS   feedr   closer_   rawdata)r:   sr,   r,   r-   _strip_once   s    		rg   c             C   sQ   xJ d |  k rL d |  k rL t  |   } t |  t |   k rC Pn  | }  q W|  S)z.Returns the given HTML with all tags stripped.r    r!   )rg   len)r:   Z	new_valuer,   r,   r-   
strip_tags   s    
ri   c             C   s   t  j d t d d d d   | j   D } d d j |  } t j d | t j  } t j d	 |  } | j d
 |   }  | j d
 |   }  |  S)z/Returns the given HTML with given tags removed.z}django.utils.html.remove_tags() and the removetags template filter are deprecated. Consider using the bleach library instead.
stacklevel   c             S   s   g  |  ] } t  j |   q Sr,   )r(   r)   )r*   tagr,   r,   r-   rO      s   	 zremove_tags.<locals>.<listcomp>z(%s)r'   z<%s(/?>|(\s+[^>]*>))z</%s>r^   )	warningswarnr   rP   rJ   r(   compileUsub)htmltagsZtags_reZstarttag_reZ	endtag_rer,   r,   r-   remove_tags   s    rt   c             C   s   t  j d d t |    S)z8Returns the given HTML with spaces between tags removed.z>\s+<z><)r(   rq   r   )r:   r,   r,   r-   strip_spaces_between_tags   s    ru   c             C   s/   t  j d t d d t j d d t |    S)z@Returns the given HTML with all entities (&something;) stripped.z1django.utils.html.strip_entities() is deprecated.rj      z&(?:\w+|#\d+);r^   )rm   rn   r   r(   rq   r   )r:   r,   r,   r-   strip_entities   s    rw   c             C   s   d d   } y t  |   \ } } } } } Wn t k
 rI | |   SYn Xy | j d  j d  } Wn t k
 r | |   SYn X| r d d   t | d d D } t |  } n  | |  } | |  } t | | | | | f  S)	z(Quotes a URL if it isn't already quoted.c             S   s<   t  t |    }  t |  d t t t d  }  t |   S)Nsafe~)r   r   r   r	   r   strr   )Zsegmentr,   r,   r-   unquote_quote   s     z%smart_urlquote.<locals>.unquote_quoteidnaasciic             S   s<   g  |  ]2 } t  t | d    t  t | d   f  q S)r   r   )r   r   )r*   qr,   r,   r-   rO      s   	z"smart_urlquote.<locals>.<listcomp>keep_blank_valuesT)r   
ValueErrorencodedecodeUnicodeErrorr   r   r   )urlr{   schemenetlocpathqueryfragmentZquery_partsr,   r,   r-   smart_urlquote   s     		r   c             C   sr  t  |  t  } | d d  } d d   } t j t |    } x#t |  D]\ } }	 d |	 k s| d |	 k s| d |	 k r/d |	 d }
 } } xA t D]9 } | j |  r | d	 t |   } | | } q q Wx t	 D] \ } } | j
 |  r| t |  d	  } |
 | }
 n  | j |  r | j |  | j |  d
 k r | d	 t |   } | | } q q Wd	 } | rd n d } t j |  r| | |  \ } } } t |  } n t j |  r| | |  \ } } } t d |  } n d | k rzt j |  rz| j d d
  \ } } y | j d  j d  } Wn t k
 r`wL Yn Xd | | f } d } n  | r| |  } | r| rt |
  t |  }
 } t |  } n  d t |  | | f } t d |
 | | f  | | <qa| rt |	  | | <qa| rat |	  | | <qaqL | rHt |	  | | <qL | rL t |	  | | <qL qL Wd j |  S)a  
    Converts 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, the URLs in the link text longer than this
    limit will be truncated to trim_url_limit-3 characters and appended with
    an ellipsis.

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

    If autoescape is True, the link text and URLs will be autoescaped.
    c             S   sA   | d  k s t  |   | k r" |  Sd |  d  t d | d   S)Nz%s...r   rk   )rh   max)r+   limitr,   r,   r-   trim_url  s    zurlize.<locals>.trim_urlc             S   s   |  | j  d d  j  d d  j  d d  j  d d  j  d	 d
  } | ru | j |  ru | d t |   } n | d k r |  | 7}  d } n  |  | | f S)z
        If input URL is HTML-escaped, unescape it so as we can safely feed it to
        smart_urlquote. For example:
        http://example.com?x=1&amp;y=&lt;2&gt; => http://example.com?x=1&y=<2>
        z&amp;r/   z&lt;r    z&gt;r!   z&quot;r   z&#39;r   Nr   r^   )r0   endswithrh   )r1   trailZ	unescapedr,   r,   r-   unescape  s    
$
	zurlize.<locals>.unescaper   @r   r^   Nr   z rel="nofollow"z	http://%sr|   r}   zmailto:%s@%sz<a href="%s"%s>%s</a>z%s%s%s)
isinstancer
   word_split_rerP   r   	enumerateTRAILING_PUNCTUATIONr   rh   WRAPPING_PUNCTUATION
startswithcountsimple_url_rematchr   simple_url_2_resimple_email_rersplitr   r   r   r)   r   rJ   )r1   Ztrim_url_limitZnofollowrQ   Z
safe_inputr   r   wordsiwordZleadZmiddler   punctuationZopeningclosingr   Znofollow_attrZmiddle_unescapedlocaldomainZtrimmedr,   r,   r-   urlize   sf    $"	 r   c             C   s   |  j  d d  S)z
    Avoid text wrapping in the middle of a phrase by adding non-breaking
    spaces where there previously were normal spaces.
         )r0   )r:   r,   r,   r-   avoid_wrappingd  s    r   c                s   d |  j  k r% t d |  j   n  t j r d |  j  k rS t d |  j   n  |  j   f d d   |  _ d d   |  _ nR d |  j  k r t d	 |  j   n  |  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 SafeText.
    r<   z;can't apply @html_safe to %s because it defines __html__().__unicode__zEcan't apply @html_safe to %s because it doesn't define __unicode__().c                s   t    |    S)N)r   )rW   )klass_unicoder,   r-   <lambda>}  s    zhtml_safe.<locals>.<lambda>c             S   s
   t  |   S)N)unicode)rW   r,   r,   r-   r   ~  s    __str__zAcan't apply @html_safe to %s because it doesn't define __str__().c                s   t    |    S)N)r   )rW   )	klass_strr,   r-   r     s    c             S   s
   t  |   S)N)rz   )rW   r,   r,   r-   r     s    )__dict__r   r`   r   PY2r   r<   r   )klassr,   )r   r   r-   	html_safel  s(    			r   )r   r   )r    r!   )r"   r#   )r$   r%   )r   r   )r   r   )F__doc__
__future__r   r(   rm   Zdjango.utilsr   Zdjango.utils.deprecationr   Zdjango.utils.encodingr   r   Zdjango.utils.functionalr   Zdjango.utils.httpr   r	   Zdjango.utils.safestringr
   r   r   Z#django.utils.six.moves.urllib.parser   r   r   r   r   r   Zdjango.utils.textr   html_parserr   r   r   r   ZDOTSro   Zunencoded_ampersands_rer   
IGNORECASEr   r   r   Zlink_target_attribute_reZhtml_gunk_rerJ   DOTALLZhard_coded_bullets_reZtrailing_empty_content_rer)   	text_typer5   r9   updateranger;   r>   rH   rL   rR   rS   rg   ri   rt   ru   rw   r   r   r   r   r,   r,   r,   r-   <module>   s   . 	$a