U
    luf0                  &   @   s  d dl Z d dlZd dlmZmZmZmZ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mZmZ di dfdi dfd	i d
fdi dfdi dfdi dfdi dfdi dfdi dfdi dfdi dfdi dfdi dfdi d fd!i d"fd#i d$fd%i d&fd'd(d)d*gid+fd,d(d*gid-fd.i d/fd0i d1fd2i d3fd4i d5fd6d(d)d7gid8fd9i d:fd;i d<fd=i d>fd=d?d@id=fdAd@d@dBdCfdDdEd@idFfdGi dHfdGd(dIgidJfdKdLdMidNfdOdLdPdQ idRfdKdLdSdQ idNfg#ZG dTdU dUe jZdS )V    N)
utf8xhtml_escapexhtml_unescape
url_escapeurl_unescape
to_unicodejson_decodejson_encodesqueezerecursive_unicode)unicode_type)ListTupleUnionDictAnyzhello http://world.com/!z8hello <a href="http://world.com/">http://world.com/</a>!z0hello http://world.com/with?param=true&stuff=yeszqhello <a href="http://world.com/with?param=true&amp;stuff=yes">http://world.com/with?param=true&amp;stuff=yes</a>z>http://url.com/w(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz]<a href="http://url.com/w">http://url.com/w</a>(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz>http://url.com/withmany.......................................zd<a href="http://url.com/withmany">http://url.com/withmany</a>.......................................z;http://url.com/withmany((((((((((((((((((((((((((((((((((a)za<a href="http://url.com/withmany">http://url.com/withmany</a>((((((((((((((((((((((((((((((((((a)zhttp://foo.com/blah_blahz?<a href="http://foo.com/blah_blah">http://foo.com/blah_blah</a>zhttp://foo.com/blah_blah/zA<a href="http://foo.com/blah_blah/">http://foo.com/blah_blah/</a>z)(Something like http://foo.com/blah_blah)zP(Something like <a href="http://foo.com/blah_blah">http://foo.com/blah_blah</a>)z$http://foo.com/blah_blah_(wikipedia)zW<a href="http://foo.com/blah_blah_(wikipedia)">http://foo.com/blah_blah_(wikipedia)</a>z+http://foo.com/blah_(blah)_(wikipedia)_blahze<a href="http://foo.com/blah_(blah)_(wikipedia)_blah">http://foo.com/blah_(blah)_(wikipedia)_blah</a>z5(Something like http://foo.com/blah_blah_(wikipedia))zh(Something like <a href="http://foo.com/blah_blah_(wikipedia)">http://foo.com/blah_blah_(wikipedia)</a>)zhttp://foo.com/blah_blah.z@<a href="http://foo.com/blah_blah">http://foo.com/blah_blah</a>.zhttp://foo.com/blah_blah/.zB<a href="http://foo.com/blah_blah/">http://foo.com/blah_blah/</a>.z<http://foo.com/blah_blah>zG&lt;<a href="http://foo.com/blah_blah">http://foo.com/blah_blah</a>&gt;z<http://foo.com/blah_blah/>zI&lt;<a href="http://foo.com/blah_blah/">http://foo.com/blah_blah/</a>&gt;zhttp://foo.com/blah_blah,z@<a href="http://foo.com/blah_blah">http://foo.com/blah_blah</a>,z&http://www.example.com/wpstyle/?p=364.zZ<a href="http://www.example.com/wpstyle/?p=364">http://www.example.com/wpstyle/?p=364</a>.zrdar://1234Zpermitted_protocolshttpZrdarz%<a href="rdar://1234">rdar://1234</a>z
rdar:/1234z#<a href="rdar:/1234">rdar:/1234</a>z'http://userid:password@example.com:8080z]<a href="http://userid:password@example.com:8080">http://userid:password@example.com:8080</a>zhttp://userid@example.comzA<a href="http://userid@example.com">http://userid@example.com</a>zhttp://userid@example.com:8080zK<a href="http://userid@example.com:8080">http://userid@example.com:8080</a>z"http://userid:password@example.comzS<a href="http://userid:password@example.com">http://userid:password@example.com</a>zFmessage://%3c330e7f8409726r6a4ba78dkf1fd71420c1bf6ff@mail.gmail.com%3emessagez<a href="message://%3c330e7f8409726r6a4ba78dkf1fd71420c1bf6ff@mail.gmail.com%3e">message://%3c330e7f8409726r6a4ba78dkf1fd71420c1bf6ff@mail.gmail.com%3e</a>u   http://➡.ws/䨹u1   <a href="http://➡.ws/䨹">http://➡.ws/䨹</a>z<tag>http://example.com</tag>zJ&lt;tag&gt;<a href="http://example.com">http://example.com</a>&lt;/tag&gt;zJust a www.example.com link.zAJust a <a href="http://www.example.com">www.example.com</a> link.require_protocolTz:A http://reallylong.com/link/that/exceedsthelenglimit.html)r   shortenzA <a href="http://reallylong.com/link/that/exceedsthelenglimit.html" title="http://reallylong.com/link/that/exceedsthelenglimit.html">http://reallylong.com/link...</a>z6A http://reallylongdomainnamethatwillbetoolong.com/hi!r   zA <a href="http://reallylongdomainnamethatwillbetoolong.com/hi" title="http://reallylongdomainnamethatwillbetoolong.com/hi">http://reallylongdomainnametha...</a>!z/A file:///passwords.txt and http://web.com linkzLA file:///passwords.txt and <a href="http://web.com">http://web.com</a> linkfilezSA <a href="file:///passwords.txt">file:///passwords.txt</a> and http://web.com linkzwww.external-link.comZextra_paramsrel="nofollow" class="external"z`<a href="http://www.external-link.com" rel="nofollow" class="external">www.external-link.com</a>z;www.external-link.com and www.internal-link.com/blogs extrac                 C   s   |  drdS dS )Nzhttp://www.internal-link.comzclass="internal"r   )
startswithhref r   </tmp/pip-unpacked-wheel-bmg6zs32/tornado/test/escape_test.py<lambda>   s    r   z<a href="http://www.external-link.com" rel="nofollow" class="external">www.external-link.com</a> and <a href="http://www.internal-link.com/blogs" class="internal">www.internal-link.com/blogs</a> extrac                 C   s   dS )Nz%    rel="nofollow" class="external"  r   r   r   r   r   r          c                   @   sd   e Z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d Zdd Zdd ZdS )EscapeTestCasec                 C   s0   t D ]&\}}}tjj|f|}| || qd S )N)linkify_teststornadoescapeZlinkifyassertEqual)selftextkwargshtmlZlinkedr   r   r   test_linkify   s    zEscapeTestCase.test_linkifyc                 C   sT   dddddddg}|D ]8\}}|  tt|t| |  t|tt| qd S )N)z<foo>z&lt;foo&gt;)s   <foo>s   &lt;foo&gt;)z<>&"'z&lt;&gt;&amp;&quot;&#x27;)z&amp;z	&amp;amp;)u   <é>u
   &lt;é&gt;)s   <é>s
   &lt;é&gt;)r#   r   r   r   r$   tests	unescapedescapedr   r   r   test_xhtml_escape   s    	z EscapeTestCase.test_xhtml_escapec                 C   s4   dddddddg}|D ]\}}|  |t| qd S )N)zfoo&#32;barfoo bar)zfoo&#x20;barr.   )zfoo&#X20;barr.   )zfoo&#xabc;baru	   foo઼bar)foo&#xyz;barr/   )	foo&#;barr0   )
foo&#x;barr1   )r#   r   )r$   r*   r,   r+   r   r   r   test_xhtml_unescape_numeric   s    	z*EscapeTestCase.test_xhtml_unescape_numericc                 C   s@   d ddfd ddfdg}|D ]\}}| t|| q"d S )N   ér   %C3%A9latin1z%E9)r3   r4   )encoder#   r   r)   r   r   r   test_url_escape_unicode   s    z&EscapeTestCase.test_url_escape_unicodec                 C   sT   dddt dd fg}|D ]6\}}}| tt||| | tt ||| qd S )N)r4   r3   r   )r4   u   Ã©r5   r4   r3   )r   r#   r   r   )r$   r*   r,   r+   encodingr   r   r   test_url_unescape_unicode   s    z(EscapeTestCase.test_url_unescape_unicodec                 C   s   d}d}d}|  t|| |  t|dd| |  t|| |  t|dd| |  t|d dt| |  t|d ddt| d S )Nz+ #%z
%2B+%23%25z%2B%20%23%25F)plus)r8   )r8   r:   )r#   r   r   r   )r$   r+   Zplus_escapedr,   r   r   r   test_url_escape_quote_plus  s     z)EscapeTestCase.test_url_escape_quote_plusc                 C   s,   |  ttdt |  ttdt d S )Nfoo)r#   typer   strr   r$   r   r   r   test_escape_return_types  s    z'EscapeTestCase.test_escape_return_typesc                 C   s8   |  tdd |  tdd |  ttdd d S )Ns   "foo"r<   z"foo"u   "é"r3   )r#   r   r   r?   r   r   r   test_json_decode   s    zEscapeTestCase.test_json_decodec                 C   sF   |  ttdd ttkrB|  tttdd | ttd d S )Nr3      )r#   r   r	   bytesr>   r   assertRaisesUnicodeDecodeErrorr?   r   r   r   test_json_encode)  s    zEscapeTestCase.test_json_encodec                 C   s   |  tdd d S )Nz&sequences     of    whitespace   charszsequences of whitespace chars)r#   r
   r?   r   r   r   test_squeeze2  s    zEscapeTestCase.test_squeezec                 C   sr   ddiddgddd}|  t|d ddi |  t|d ddg |  t|d	 d
 |  t|d d d S )N   foo   bar)rH   rI   )dictlisttuplerC   rJ   r<   barrK   rL   )r<   rM   rC   )r#   r   )r$   r*   r   r   r   test_recursive_unicode8  s    z%EscapeTestCase.test_recursive_unicodeN)__name__
__module____qualname__r(   r-   r2   r7   r9   r;   r@   rA   rF   rG   rN   r   r   r   r   r      s   		r   )Zunittestr!   Ztornado.escaper   r   r   r   r   r   r   r	   r
   r   Ztornado.utilr   typingr   r   r   r   r   r    ZTestCaser   r   r   r   r   <module>   s,  0

 
  A