
    Mh7                     T   d Z ddlZddlZddlZddlZddlmZ ddlZddlm	Z	m
Z
mZmZmZmZ de	eef         defdZde	eef         defdZde
defd	Zde	eef         de
fd
ZdedefdZd-de	eef         dedefdZej        d-de	eef         dddedefd            Zej        	 d.de	eef         dededefd            Z	 d.de	eef         dee         dede	eef         fdZ	 d/de	eef         dededeeee         f         fdZe ed          fZej        dedefd            Zej        dedefd            Zej        d0d            Zde	deef         dee         fdZe ed          fZej        dedefd            Zej        dedefd            Zej        d0d            Zde	deef         dee         fd ZeZ eZ!eZ"d!e
de
fd"Z# ej$         ed#                    Z%dd$dd%d&gfd'e	eef         d(ed)e	eeegef         f         d*ed+ee         defd,Z&dS )1a  Escaping/unescaping methods for HTML, JSON, URLs, and others.

Also includes a few other miscellaneous string manipulation functions that
have crept in over time.

Many functions in this module have near-equivalents in the standard library
(the differences mainly relate to handling of bytes and unicode strings,
and were more relevant in Python 2). In new code, the standard library
functions are encouraged instead of this module where applicable. See the
docstrings on each function for details.
    N)unicode_type)UnionAnyOptionalDictListCallablevaluereturnc                 D    t          j        t          |                     S )a  Escapes a string so it is valid within HTML or XML.

    Escapes the characters ``<``, ``>``, ``"``, ``'``, and ``&``.
    When used in attribute values the escaped strings must be enclosed
    in quotes.

    Equivalent to `html.escape` except that this function always returns
    type `str` while `html.escape` returns `bytes` if its input is `bytes`.

    .. versionchanged:: 3.2

       Added the single quote to the list of escaped characters.

    .. versionchanged:: 6.4

       Now simply wraps `html.escape`. This is equivalent to the old behavior
       except that single quotes are now escaped as ``&#x27;`` instead of
       ``&#39;`` and performance may be different.
    )htmlescape
to_unicoder
   s    N/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/tornado/escape.pyxhtml_escaper   '   s    ( ;z%(()))    c                 D    t          j        t          |                     S )aZ  Un-escapes an XML-escaped string.

    Equivalent to `html.unescape` except that this function always returns
    type `str` while `html.unescape` returns `bytes` if its input is `bytes`.

    .. versionchanged:: 6.4

       Now simply wraps `html.unescape`. This changes behavior for some inputs
       as required by the HTML 5 specification
       https://html.spec.whatwg.org/multipage/parsing.html#numeric-character-reference-end-state

       Some invalid inputs such as surrogates now raise an error, and numeric
       references to certain ISO-8859-1 characters are now handled correctly.
    )r   unescaper   r   s    r   xhtml_unescaper   >   s     =E**+++r   c                 R    t          j        |                               dd          S )zJSON-encodes the given Python object.

    Equivalent to `json.dumps` with the additional guarantee that the output
    will never contain the character sequence ``</`` which can be problematic
    when JSON is embedded in an HTML ``<script>`` tag.
    z</z<\/)jsondumpsreplacer   s    r   json_encoder   S   s$     :e$$T6222r   c                 *    t          j        |           S )z~Returns Python objects for the given JSON string.

    Supports both `str` and `bytes` inputs. Equvalent to `json.loads`.
    )r   loadsr   s    r   json_decoder   c   s    
 :er   c                 R    t          j        dd|                                           S )z>Replace all sequences of whitespace chars with a single space.z[\x00-\x20]+ )resubstripr   s    r   squeezer$   k   s"    6/3..44666r   Tplusc                 `    |rt           j        j        nt           j        j        } ||           S )a}  Returns a URL-encoded version of the given value.

    Equivalent to either `urllib.parse.quote_plus` or `urllib.parse.quote` depending on the ``plus``
    argument.

    If ``plus`` is true (the default), spaces will be represented as ``+`` and slashes will be
    represented as ``%2F``.  This is appropriate for query strings. If ``plus`` is false, spaces
    will be represented as ``%20`` and slashes are left as-is. This is appropriate for the path
    component of a URL. Note that the default of ``plus=True`` is effectively the
    reverse of Python's urllib module.

    .. versionadded:: 3.1
        The ``plus`` argument
    )urllibparse
quote_plusquote)r
   r%   r*   s      r   
url_escaper+   p   s+     (,CFL##1CE5<<r   encodingc                     d S N r
   r,   r%   s      r   url_unescaper1          Dr   utf-8c                     d S r.   r/   r0   s      r   r1   r1      s	     	Dr   c                 
   |D|r#t          |                               dd          } t          j                            |           S |rt          j        j        nt          j        j        } |t          |           |          S )aO  Decodes the given value from a URL.

    The argument may be either a byte or unicode string.

    If encoding is None, the result will be a byte string and this function is equivalent to
    `urllib.parse.unquote_to_bytes` if ``plus=False``.  Otherwise, the result is a unicode string in
    the specified encoding and this function is equivalent to either `urllib.parse.unquote_plus` or
    `urllib.parse.unquote` except that this function also accepts `bytes` as input.

    If ``plus`` is true (the default), plus signs will be interpreted as spaces (literal plus signs
    must be represented as "%2B").  This is appropriate for query strings and form-encoded values
    but not for the path component of a URL.  Note that this default is the reverse of Python's
    urllib module.

    .. versionadded:: 3.1
       The ``plus`` argument
    N+r    )r,   )to_basestringr   r'   r(   unquote_to_bytesunquote_plusunquote)r
   r,   r%   r:   s       r   r1   r1      s{    (  	;!%((00c::E|,,U333/3M&,++9Mw}U++h????r   Fqskeep_blank_valuesstrict_parsingc                     t          | t                    r|                     d          } t          j                            | ||dd          }i }|                                D ]\  }}d |D             ||<   |S )a/  Parses a query string like urlparse.parse_qs,
    but takes bytes and returns the values as byte strings.

    Keys still become type str (interpreted as latin1 in python3!)
    because it's too painful to keep them as byte strings in
    python3 and in practice they're nearly always ascii anyway.
    latin1strict)r,   errorsc                 8    g | ]}|                     d           S )r?   )encode.0is     r   
<listcomp>z"parse_qs_bytes.<locals>.<listcomp>   s$    444Qahhx((444r   )
isinstancebytesdecoder'   r(   parse_qsitems)r;   r<   r=   resultencodedkvs          r   parse_qs_bytesrQ      s     "e !YYx  \""
~ #  F G 5 5144!444

Nr   c                     d S r.   r/   r   s    r   utf8rS      r2   r   c                     d S r.   r/   r   s    r   rS   rS      r2   r   c                     d S r.   r/   r   s    r   rS   rS      r2   r   c                     t          | t                    r| S t          | t                    st          dt	          |           z            |                     d          S )zConverts a string argument to a byte string.

    If the argument is already a byte string or None, it is returned unchanged.
    Otherwise it must be a unicode string and is encoded as utf8.
    (Expected bytes, unicode, or None; got %rr3   )rH   _UTF8_TYPESr   	TypeErrortyperC   r   s    r   rS   rS      sZ     %%% e\** RBT%[[PQQQ<<   r   c                     d S r.   r/   r   s    r   r   r      r2   r   c                     d S r.   r/   r   s    r   r   r      r2   r   c                     d S r.   r/   r   s    r   r   r      r2   r   c                     t          | t                    r| S t          | t                    st          dt	          |           z            |                     d          S )zConverts a string argument to a unicode string.

    If the argument is already a unicode string or None, it is returned
    unchanged.  Otherwise it must be a byte string and is decoded as utf8.
    rW   r3   )rH   _TO_UNICODE_TYPESrI   rY   rZ   rJ   r   s    r   r   r      s[     %*++ eU## RBT%[[PQQQ<<   r   objc                 l   t          | t                    rd |                                 D             S t          | t                    rt          d | D                       S t          | t                    rt	          d | D                       S t          | t
                    rt          |           S | S )zvWalks a simple data structure, converting byte strings to unicode.

    Supports lists, tuples, and dictionaries.
    c                 N    i | ]"\  }}t          |          t          |          #S r/   recursive_unicode)rE   rO   rP   s      r   
<dictcomp>z%recursive_unicode.<locals>.<dictcomp>  s0    UUUv1!!$$&7&:&:UUUr   c              3   4   K   | ]}t          |          V  d S r.   rc   rD   s     r   	<genexpr>z$recursive_unicode.<locals>.<genexpr>  s+      66Q%a((666666r   c              3   4   K   | ]}t          |          V  d S r.   rc   rD   s     r   rg   z$recursive_unicode.<locals>.<genexpr>  s+      77a&q))777777r   )rH   dictrL   listtuplerI   r   )r`   s    r   rd   rd     s    
 #t 	UUUUUU	C		 66#666666	C		 773777777	C		 #
r   z\b((?:([\w-]+):(/{1,3})|www[.])(?:(?:(?:[^\s&()]|&amp;|&quot;)*(?:[^!"#$%&'()*+,.:;<=>?@\[\]^`{|}~\s]))|(?:\((?:[^\s&()]|&amp;|&quot;)*\)))+) httphttpstextshortenextra_paramsrequire_protocolpermitted_protocolsc                     r&t                    sd                                z   dt          j        dt          ffd}t          t          |                     } t                              ||           S )a  Converts plain text into HTML with links.

    For example: ``linkify("Hello http://tornadoweb.org!")`` would return
    ``Hello <a href="http://tornadoweb.org">http://tornadoweb.org</a>!``

    Parameters:

    * ``shorten``: Long urls will be shortened for display.

    * ``extra_params``: Extra text to include in the link tag, or a callable
      taking the link as an argument and returning the extra text
      e.g. ``linkify(text, extra_params='rel="nofollow" class="external"')``,
      or::

          def extra_params_cb(url):
              if url.startswith("http://example.com"):
                  return 'class="internal"'
              else:
                  return 'class="external" rel="nofollow"'
          linkify(text, extra_params=extra_params_cb)

    * ``require_protocol``: Only linkify urls which include a protocol. If
      this is False, urls such as www.facebook.com will also be linkified.

    * ``permitted_protocols``: List (or set) of protocols which should be
      linkified, e.g. ``linkify(text, permitted_protocols=["http", "ftp",
      "mailto"])``. It is very unsafe to include protocols such as
      ``javascript``.
    r    mr   c                    |                      d          }|                      d          }r|s|S |r|vr|S |                      d          }|sd|z   }t          
          r!d 
|                                          z   }n
}d}rYt          |          |k    rE|}|r8t          |          dz   t          |                      d          pd          z   }nd}||d                              d	          }t          |          dk    rY|d |         |d         z   d	z   |d         d d
                             d          d                             d          d         z   }t          |          |dz  k    r
|d |         }||k    rX|                    d          }	|	|dz
  k    r
|d |	         }|dz  }t          |          t          |          k    r|}n|d|z  z  }d| d| d| dS )N      zhttp://r          rl   r   /   ?.g      ?&   z...z title="%s"z	<a href="">z</a>)groupcallabler#   lensplitrfind)ru   urlprotohrefparamsmax_lenbefore_clip	proto_lenpartsamprq   rs   rr   rp   s             r   	make_linkzlinkify.<locals>.make_linkR  sD   ggajj

 	E 	J 	U"555Jwwqzz 	$t#DL!! 	"<<--33555FF!F  #	3s3xx'))K JJNS1Ar-B-BB				

O))#..E5zzA~~ 

OAh Ahrrl((--a066s;;A>?  3xx'C-''(7(mk!!iinn1$$dsd)Cus88s;////%CC md22F4444&4434444r   )	r   r#   typingMatchstr_unicoder   _URL_REr"   )ro   rp   rq   rr   rs   r   s    ```` r   linkifyr   +  s    H  2H\22 2\//11195V\ 95c 95 95 95 95 95 95 95 95 95| L&&''D;;y$'''r   )T)r3   T)FF)r
   Nr   N)'__doc__r   r   r!   urllib.parser'   tornado.utilr   r   r   r   r   r   r   r	   r   rI   r   r   r   r   r$   boolr+   overloadr1   rQ   rZ   rX   rS   r_   r   r   
native_strr7   rd   compiler   r   r/   r   r   <module>r      sb   
 
   				     % % % % % %  = = = = = = = = = = = = = = = =*c5j) *c * * * *.,%U
+ , , , , ,*3s 3s 3 3 3 3 uS%Z( S    73 73 7 7 7 7
 eCJ' t s    & 	 	c5j) 	T 	 	QV 	 	 	 	 DH	 	e	(+	=A		 	 	 	 OS@ @e@(0@GK@
3:@ @ @ @> TY c5j.2LP	#tE{
   . dd4jj! 	 	% 	 	 	 	 	 	 	 	 	 	 	 	 	 	
!dC&' 
!HUO 
! 
! 
! 
! "44::.  	c 	c 	 	 	 	 	e 	 	 	 	 	 	 	 	 	
!eD#u,- 
!(3- 
! 
! 
! 
!  
3 3    0 "*J 	]   57"&,g%6f( f(
U

f(f( XseSj112f( 	f(
 cf( 	f( f( f( f( f( f(r   