
    1Vh/                         d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d d	lmZ g d
ZddgdgdgdZg Zg dZ G d de      Zd Z G d de
j0                        Zy)    )unicode_literalsN)unescape)html5lib)
namespaces)	sanitizer)HTMLSerializer)force_unicode)alphabetize_attributes)aabbracronymb
blockquotecodeemiliolstrongulhreftitle)r   r   r   )httphttpsmailtoc                   ,    e Zd ZdZeeeedddfdZd Z	y)Cleanera  Cleaner for cleaning HTML fragments of malicious content

    This cleaner is a security-focused function whose sole purpose is to remove
    malicious content from a string such that it can be displayed as content in
    a web page.

    This cleaner is not designed to use to transform content to be used in
    non-web-page contexts.

    To use::

        from bleach.sanitizer import Cleaner

        cleaner = Cleaner()

        for text in all_the_yucky_things:
            sanitized = cleaner.clean(text)

    FTNc                     || _         || _        || _        || _        || _        || _        |xs g | _        t        j                  d      | _	        t        j                  d      | _        t        dddd      | _        y)a  Initializes a Cleaner

        :arg list tags: allowed list of tags; defaults to
            ``bleach.sanitizer.ALLOWED_TAGS``

        :arg dict attributes: allowed attributes; can be a callable, list or dict;
            defaults to ``bleach.sanitizer.ALLOWED_ATTRIBUTES``

        :arg list styles: allowed list of css styles; defaults to
            ``bleach.sanitizer.ALLOWED_STYLES``

        :arg list protocols: allowed list of protocols for links; defaults
            to ``bleach.sanitizer.ALLOWED_PROTOCOLS``

        :arg bool strip: whether or not to strip disallowed elements

        :arg bool strip_comments: whether or not to strip HTML comments

        :arg list filters: list of html5lib Filter classes to pass streamed content through

            .. seealso:: http://html5lib.readthedocs.io/en/latest/movingparts.html#filters

            .. Warning::

               Using filters changes the output of ``bleach.Cleaner.clean``.
               Make sure the way the filters change the output are secure.

        F)namespaceHTMLElementsetreealways)quote_attr_valuesomit_optional_tagssanitizealphabetical_attributesN)tags
attributesstyles	protocolsstripstrip_commentsfiltersr   
HTMLParserparsergetTreeWalkerwalkerr   
serializer)selfr&   r'   r(   r)   r*   r+   r,   s           T/home/dcms/DCMS/lib/python3.12/site-packages/tensorboard/_vendor/bleach/sanitizer.py__init__zCleaner.__init__D   sz    > 	$"
,}"))F,,W5(&$  %*	
    c           
      z   |syt        |      }| j                  j                  |      }t        | j	                  |      | j
                  | j                  | j                  | j                  | j                  | j                  g       }| j                  D ]  } ||      } | j                  j                  |      S )zCleans text and returns sanitized result as unicode

        :arg str text: text to be cleaned

        :returns: sanitized text as unicode

         )sourcer'   strip_disallowed_elementsstrip_html_commentsallowed_elementsallowed_css_propertiesallowed_protocolsallowed_svg_properties)r8   )r	   r.   parseFragmentBleachSanitizerFilterr0   r'   r*   r+   r&   r(   r)   r,   r1   render)r2   textdomfilteredfilter_classs        r3   cleanzCleaner.cleanx   s     T"kk''-(;;s# &*jj $ 3 3 "YY#';;"nn#%
  !LL 	5L#84H	5 %%h//r5   )
__name__
__module____qualname____doc__ALLOWED_TAGSALLOWED_ATTRIBUTESALLOWED_STYLESALLOWED_PROTOCOLSr4   rF    r5   r3   r   r   /   s'    ( )5G&2C5 $d2
h!0r5   r   c                      t               r S t         t              r fd}|S t         t              r fd}|S t	        d      )a0  Generates attribute filter function for the given attributes value

    The attributes value can take one of several shapes. This returns a filter
    function appropriate to the attributes value. One nice thing about this is
    that there's less if/then shenanigans in the ``allow_token`` method.

    c                     | v r|    }t        |      r
 || ||      S ||v rydv rd   }t        |      r
 || ||      S ||v S y)NT*F)callable)tagattrvalueattr_valr'   s       r3   _attr_filterz.attribute_filter_factory.<locals>._attr_filter   sl    j %c?H%#Cu558#j %c?H%#Cu55x''r5   c                     |v S )NrO   )rT   rU   rV   r'   s      r3   rX   z.attribute_filter_factory.<locals>._attr_filter   s    :%%r5   z3attributes needs to be a callable, a list or a dict)rS   
isinstancedictlist
ValueError)r'   rX   s   ` r3   attribute_filter_factoryr^      sM     
*d#	$ *d#	& 
J
KKr5   c                   <     e Zd ZdZeddf fd	Zd Zd Zd Z xZ	S )r@   zmhtml5lib Filter that sanitizes text

    This filter can be used anywhere html5lib filters can be used.

    FTc                 h    t        |      | _        || _        || _        t	        t
        |   |fi |S )a   Creates a BleachSanitizerFilter instance

        :arg Treewalker source: stream

        :arg list tags: allowed list of tags; defaults to
            ``bleach.sanitizer.ALLOWED_TAGS``

        :arg dict attributes: allowed attributes; can be a callable, list or dict;
            defaults to ``bleach.sanitizer.ALLOWED_ATTRIBUTES``

        :arg list styles: allowed list of css styles; defaults to
            ``bleach.sanitizer.ALLOWED_STYLES``

        :arg list protocols: allowed list of protocols for links; defaults
            to ``bleach.sanitizer.ALLOWED_PROTOCOLS``

        :arg bool strip_disallowed_elements: whether or not to strip disallowed
            elements

        :arg bool strip_html_comments: whether or not to strip HTML comments

        )r^   attr_filterr9   r:   superr@   r4   )r2   r8   r'   r9   r:   kwargs	__class__s         r3   r4   zBleachSanitizerFilter.__init__   s:    2 4J?)B&#6 *D:6LVLLr5   c                     |d   }|dv rU|d   | j                   v r| j                  |      S | j                  ryd|v rt        |d         |d<   | j	                  |      S |dk(  r| j
                  s|S y|S )a~  Sanitize a token either by HTML-encoding or dropping.

        Unlike sanitizer.Filter, allowed_attributes can be a dict of {'tag':
        ['attribute', 'pairs'], 'tag': callable}.

        Here callable is a function with two arguments of attribute name and
        value. It should return true of false.

        Also gives the option to strip tags instead of encoding.

        type)StartTagEndTagEmptyTagnamedataCommentN)r;   allow_tokenr9   r
   disallowed_tokenr:   )r2   token
token_types      r3   sanitize_tokenz$BleachSanitizerFilter.sanitize_token   s     6]
;;V} 5 55''..// U? %;5=$IE&M,,U339$++ , Lr5   c                    d|v rfi }|d   j                         D ]?  \  }}|\  }}| j                  |d   ||      s#|| j                  v rwt        j                  ddt        |            j                         }|j                  dd      }t        j                  d|      r!|j                  d      d   | j                  vr|| j                  v r5t        j                  d	d
t        |            }|j                         }|s|}d|d   f| j                  v r'|dt        d   dffv rt        j                  d|      r%|dk(  r| j!                  |      }|||<   B t#        |      |d<   |S )z-Handles the case where we're allowing the tagrk   rj   u   [` - - \s]+r7   u   �z^[a-z0-9][-+.a-z0-9]*::r   zurl\s*\(\s*[^#\s][^)]+?\) N)Nr   xlinkr   z
^\s*[^#\s])Nstyle)itemsra   attr_val_is_uriresubr   lowerreplacematchsplitr=   svg_attr_val_allows_refr*   svg_allow_local_hrefr   searchsanitize_cssr
   )	r2   ro   attrsnamespaced_nameval	namespacerj   val_unescapednew_vals	            r3   rm   z!BleachSanitizerFilter.allow_token  s   U? E(-f(;(;(= 3-$"1	4 ''ftSA #d&:&::$&FF2 %' (-uw " %2$9$9(B$GM !:MJ*005a8@V@VV  #d&B&BB ff%A%(%-c]4G &mmoG" 
 & %-(D,E,EE&>Jw<OQW;X*YY99]C8$ #&66++C0C *-o&g3-j 359E&Mr5   c                 >   t        j                  d      j                  d|      }|j                  d      }t        j                  d      }|D ]  }|j	                  |      r y t        j                  d|      syg }t        j
                  d|      D ]v  \  }}|s	|j                         | j                  v r|j                  |dz   |z   dz          @|j                         | j                  v s]|j                  |dz   |z   dz          x dj                  |      S )	zSanitizes css in style tagszurl\s*\(\s*[^\s)]+?\s*\)\s*rt   ;zI^([-/:,#%.'"\sa-zA-Z0-9!]|\w-\w|'[\s\w]+'\s*|"[\s\w]+"|\([\d,%\.\s]+\))*$r7   z ^\s*([-\w]+\s*:[^:;]*(;\s*|$))*$z([-\w]+)\s*:\s*([^:;]*)z: )ry   compilerz   r~   r}   findallr{   r<   appendr>   join)r2   rv   partsgauntletpartrF   proprV   s           r3   r   z"BleachSanitizerFilter.sanitize_cssP  s    

89==c5I C ::\
  	D>>$'	 xx:EB::&?G 	8KD%zz|t:::TD[50367!<!<<TD[50367	8 xxr5   )
rG   rH   rI   rJ   rL   r4   rq   rm   r   __classcell__)rd   s   @r3   r@   r@      s+    
 +=+0dM@ DAF r5   r@   )
__future__r   ry   xml.sax.saxutilsr   tensorboard._vendorr   &tensorboard._vendor.html5lib.constantsr   $tensorboard._vendor.html5lib.filtersr   'tensorboard._vendor.html5lib.serializerr   #tensorboard._vendor.bleach.encodingr	    tensorboard._vendor.bleach.utilsr
   rK   rL   rM   rN   objectr   r^   Filterr@   rO   r5   r3   <module>r      s|    ' 	 % ( = : B = C$ '	Iy   0 j0f j0Z&LRkI,, kr5   