a
    ÷£BeþZ  ã                   @   s¤  d Z ddlZddlZddlZejddedd� ddlmZmZ ddlm	Z	 dd	l
mZmZ dd
l
m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 ddlmZmZ ddlmZm Z  ddl!m"Z" e	j#Z$e"e$ƒZ%e	j&d e	j&d e	j&d hZ'e	j&d Z(e	j&d Z)e	j&d Z*e	j&d Z+e,dƒZ-e,dƒZ.G dd„ dƒZ/G dd„ de ƒZ0G dd„ deƒZ1dd„ Z2d d!„ Z3d"d#„ Z4e 5d$¡Z6d%d&„ Z7G d'd(„ d(eƒZ8dS ))z‡
Shim module between Bleach and html5lib. This makes it easier to upgrade the
html5lib library without having to change a lot of code.
é    NÚignorez"html5lib's sanitizer is deprecatedzbleach._vendor.html5lib)ÚmessageÚcategoryÚmodule)Ú
HTMLParserÚgetTreeWalker)Ú	constants)Ú
namespacesÚprefixes)Ú_ReparseException)ÚFilter)Úallowed_protocolsÚallowed_css_propertiesÚallowed_svg_propertiesÚattr_val_is_uriÚsvg_attr_val_allows_refÚsvg_allow_local_href)ÚHTMLInputStream)ÚescapeÚHTMLSerializer)ÚattributeMapÚHTMLTokenizer)ÚTrieZStartTagZEndTagZEmptyTagZ
CharactersZ
ParseError)pÚaZabbrÚaddressZareaÚarticleÚasideZaudioÚbÚbaseZbdiZbdoÚ
blockquoteÚbodyÚbrZbuttonZcanvasÚcaptionZciteÚcodeÚcolZcolgroupÚdataZdatalistÚddÚdelÚdetailsÚdfnÚdialogÚdivÚdlÚdtZemÚembedÚfieldsetÚ
figcaptionÚfigureÚfooterÚformÚh1Úh2Úh3Úh4Úh5Úh6ÚheadÚheaderÚhgroupÚhrÚhtmlÚiZiframeZimgÚinputZinsZkbdZkeygenÚlabelZlegendÚliÚlinkÚmapÚmarkZmenuÚmetaZmeterÚnavZnoscriptÚobjectÚolZoptgroupÚoptionÚoutputÚpÚparamZpictureÚpreÚprogressÚqÚrpÚrtÚrubyÚsZsampÚscriptÚsectionÚselectÚslotZsmallÚsourceÚspanÚstrongÚstyleÚsubÚsummaryÚsupÚtableZtbodyÚtdÚtemplateZtextareaZtfootÚthZtheadÚtimeÚtitleÚtrÚtrackÚuÚulÚvarZvideoZwbr)!r   r   r   r   r(   r*   r&   r+   r,   r-   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r;   r<   r=   rB   ÚmainrG   rI   rL   rN   rV   r`   ri   c                   @   sf   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZedd	„ ƒZd
d„ Z	ddd„Z
dd„ Zdd„ Zdd„ ZdS )ÚInputStreamWithMemoryzÄWraps an HTMLInputStream to remember characters since last <

    This wraps existing HTMLInputStream classes to keep track of the stream
    since the last < which marked an open tag state.

    c                 C   s$   || _ | j j| _| j j| _g | _d S ©N)Ú_inner_streamÚresetÚpositionÚ_buffer)ÚselfZinner_stream© rs   úW/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/bleach/html5lib_shim.pyÚ__init__ü   s    

zInputStreamWithMemory.__init__c                 C   s   | j jS rm   )rn   Úerrors©rr   rs   rs   rt   rv     s    zInputStreamWithMemory.errorsc                 C   s   | j jS rm   )rn   ÚcharEncodingrw   rs   rs   rt   rx     s    z"InputStreamWithMemory.charEncodingc                 C   s   | j jS rm   )rn   ÚchangeEncodingrw   rs   rs   rt   ry   
  s    z$InputStreamWithMemory.changeEncodingc                 C   s   | j  ¡ }|r| j |¡ |S rm   )rn   Úcharrq   Úappend)rr   Úcrs   rs   rt   rz     s    
zInputStreamWithMemory.charFc                 C   s$   | j j||d�}| j t|ƒ¡ |S )N)Úopposite)rn   Ú
charsUntilrq   ÚextendÚlist)rr   Ú
charactersr}   Úcharsrs   rs   rt   r~     s    z InputStreamWithMemory.charsUntilc                 C   s   | j r| j  d¡ | j |¡S )Néÿÿÿÿ)rq   Úpoprn   Úunget)rr   rz   rs   rs   rt   r…     s    zInputStreamWithMemory.ungetc                 C   s   d  | j¡S )zþReturns the stream history since last '<'

        Since the buffer starts at the last '<' as as seen by tagOpenState(),
        we know that everything from that point to when this method is called
        is the "tag" that is being tokenized.

        Ú )Újoinrq   rw   rs   rs   rt   Úget_tag  s    zInputStreamWithMemory.get_tagc                 C   s   dg| _ dS )z¶Resets stream history to just '<'

        This gets called by tagOpenState() which marks a '<' that denotes an
        open tag. Any time we see that, we reset the buffer.

        ú<N)rq   rw   rs   rs   rt   Ú	start_tag)  s    zInputStreamWithMemory.start_tagN)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ru   Úpropertyrv   rx   ry   rz   r~   r…   rˆ   rŠ   rs   rs   rs   rt   rl   ô   s   




rl   c                       sT   e Zd ZdZd‡ fdd„	Z‡ fdd„Zd‡ fdd	„	Z‡ fd
d„Z‡ fdd„Z‡  Z	S )ÚBleachHTMLTokenizerz1Tokenizer that doesn't consume character entitiesFc                    s.   t ƒ jf i |¤Ž || _t| jƒ| _d | _d S rm   )Úsuperru   Úconsume_entitiesrl   ÚstreamÚemitted_last_token)rr   r’   Úkwargs©Ú	__class__rs   rt   ru   6  s    zBleachHTMLTokenizer.__init__c                 #   sP  d }t ƒ  ¡ D ]ì}|d urâ|d dkrf|d tv rf| d¡rftdd„ |d  ¡ D ƒƒ|d< d }|V  q|d dkr¸| jjd ur¸|d  ¡  	¡ | jjvr¸| j
 ¡ |d< t|d< d }|V  q|d tkrÐ|V  |}q|V  |V  d }q|d tkrô|}q|V  q|�rL|d dk�r$t| j
 ¡ dœV  n(|d d	v �rFt| j
 ¡ dœV  n|V  d S )
Nr%   z#invalid-character-in-attribute-nameÚtypec                 s   s2   | ]*\}}d |vrd|vrd|vr||fV  qdS )ú"ú'r‰   Nrs   )Ú.0Ú	attr_nameZ
attr_valuers   rs   rt   Ú	<genexpr>Q  s
   ûz/BleachHTMLTokenizer.__iter__.<locals>.<genexpr>z!expected-closing-tag-but-got-charzeof-in-tag-name©r˜   r%   )zeof-in-attribute-namez eof-in-attribute-value-no-quotes)r‘   Ú__iter__ÚTAG_TOKEN_TYPESÚgetr   ÚitemsÚparserÚtagsÚlowerÚstripr“   rˆ   ÚTAG_TOKEN_TYPE_CHARACTERSÚTAG_TOKEN_TYPE_PARSEERROR)rr   Zlast_error_tokenÚtokenr–   rs   rt   rŸ   A  sR    
ÿ
þý
þ	
ÿ
þý	zBleachHTMLTokenizer.__iter__Nc                    sJ   | j rtƒ  ||¡S |r4| jd d d  d7  < n| j tddœ¡ d S )Nr%   rƒ   é   ú&rž   )r’   r‘   ÚconsumeEntityÚcurrentTokenÚ
tokenQueuer{   r§   )rr   ZallowedCharZfromAttributer–   rs   rt   r¬   œ  s
    z!BleachHTMLTokenizer.consumeEntityc                    s   | j  ¡  tƒ  ¡ S rm   )r“   rŠ   r‘   ÚtagOpenStaterw   r–   rs   rt   r¯   ®  s    
z BleachHTMLTokenizer.tagOpenStatec                    s¶   | j }| jjd ur |d tv r |d  ¡ | jjvr | jjrh| jrb|d tkrb|d  ¡ tv rbd}qrd}n
| j	 
¡ }t|dœ}| | _ | _| j |¡ | j| _d S | j | _tƒ  ¡  d S )Nr˜   ÚnameÚ
r†   rž   )r­   r£   r¤   r    r¥   r¦   r”   ÚTAG_TOKEN_TYPE_STARTÚHTML_TAGS_BLOCK_LEVELr“   rˆ   r§   r®   r{   Z	dataStateÚstater‘   ÚemitCurrentToken)rr   r©   Znew_dataZ	new_tokenr–   rs   rt   rµ   ¶  s0    
ÿ
þýÿ
þý

z$BleachHTMLTokenizer.emitCurrentToken)F)NF)
r‹   rŒ   r�   rŽ   ru   rŸ   r¬   r¯   rµ   Ú__classcell__rs   rs   r–   rt   r�   3  s   [r�   c                       s*   e Zd ZdZ‡ fdd„Zd	dd„Z‡  ZS )
ÚBleachHTMLParserz$Parser that uses BleachHTMLTokenizerc                    sB   |durt dd„ |D ƒƒnd| _|| _|| _tƒ jf i |¤Ž dS )aÏ  
        :arg tags: set of allowed tags--everything else is either stripped or
            escaped; if None, then this doesn't look at tags at all
        :arg strip: whether to strip disallowed tags (True) or escape them (False);
            if tags=None, then this doesn't have any effect
        :arg consume_entities: whether to consume entities (default behavior) or
            leave them as is when tokenizing (BleachHTMLTokenizer-added behavior)

        Nc                 s   s   | ]}|  ¡ V  qd S rm   )r¥   )r›   Útagrs   rs   rt   r�   ñ  ó    z,BleachHTMLParser.__init__.<locals>.<genexpr>)Ú	frozensetr¤   r¦   r’   r‘   ru   )rr   r¤   r¦   r’   r•   r–   rs   rt   ru   æ  s
    ÿzBleachHTMLParser.__init__Fr+   Tc                 K   sh   || _ || _|| _tf || j| dœ|¤Ž| _|  ¡  z|  ¡  W n" tyb   |  ¡  |  ¡  Y n0 d S )N)r“   r’   r£   )	ZinnerHTMLModeÚ	containerÚ	scriptingr�   r’   Z	tokenizerro   ZmainLoopÚReparseException)rr   r“   Z	innerHTMLr»   r¼   r•   rs   rs   rt   Ú_parse÷  s    	ÿÿzBleachHTMLParser._parse)Fr+   T)r‹   rŒ   r�   rŽ   ru   r¾   r¶   rs   rs   r–   rt   r·   ã  s    ÿr·   c                 C   s”   | d dkrˆt | ƒdk rdS | d dv r<| dd… d }}n| dd… d }}|d	krZdS t||ƒ}d|  k rxd
k r„n nt|ƒS dS t | d¡S )a9  Convert an entity (minus the & and ; part) into what it represents

    This handles numeric, hex, and text entities.

    :arg value: the string (minus the ``&`` and ``;`` part) to convert

    :returns: unicode character or None if it's an ambiguous ampersand that
        doesn't match a character entity

    r   ú#é   Nrª   ©ÚxÚXé   é
   r†   i   )ÚlenÚintÚchrÚENTITIESr¡   )ÚvalueZint_as_stringr   Ú
code_pointrs   rs   rt   Úconvert_entity  s    
rÌ   c                 C   s�   d| vr| S g }t | ƒD ]l}|s"q| d¡rzt|ƒ}|durzt|ƒ}|durz| |¡ |t|ƒd d… }|r| |¡ q| |¡ qd |¡S )z‘Converts all found entities in the text

    :arg text: the text to convert entities in

    :returns: unicode text with converted entities

    r«   NrÀ   r†   )Únext_possible_entityÚ
startswithÚmatch_entityrÌ   r{   rÆ   r‡   )ÚtextÚnew_textÚpartÚentityZ	convertedÚ	remainderrs   rs   rt   Úconvert_entities1  s$    


rÕ   c                 C   s  | d dkrt dƒ‚| dd… } t| ƒ} d}dtj }| rÆ| d dkrÆd}|  d¡ | rx| d d	v rxd
}||  d¡7 }nd}| rª| d |vrª|  d¡}||vr qª||7 }q||rÂ| rÂ| d dkrÂ|S dS | rø| d |vrø|  d¡}||7 }t |¡sÆdS qÆ|�r| �r| d dk�r|S dS )av  Returns first entity in stream or None if no entity exists

    Note: For Bleach purposes, entities must start with a "&" and end with a
    ";". This ignores ambiguous character entities that have no ";" at the end.

    :arg stream: the character stream

    :returns: the entity string without "&" or ";" if it's a valid character
        entity; ``None`` otherwise

    r   r«   zStream should begin with "&"rª   Nr†   z<&=;r¿   rÁ   Z0123456789abcdefABCDEFÚ
0123456789ú;)Ú
ValueErrorr€   ÚstringÚ
whitespacer„   ÚENTITIES_TRIEZhas_keys_with_prefix)r“   Zpossible_entityZend_charactersÚallowedr|   rs   rs   rt   rÏ   T  s:    





rÏ   z(&)c                 c   sB   t t | ¡ƒD ].\}}|dkr&|V  q|d dkrd| V  qdS )z·Takes a text and generates a list of possible entities

    :arg text: the text to look at

    :returns: generator where each part (except the first) starts with an
        "&"

    r   rÀ   r«   N)Ú	enumerateÚAMP_SPLIT_REÚsplit)rÐ   r?   rÒ   rs   rs   rt   rÍ   “  s
    	rÍ   c                       s.   e Zd ZdZdZdd„ Zd‡ fdd„	Z‡  ZS )	ÚBleachHTMLSerializerz[HTMLSerializer that undoes & -> &amp; in attributes and sets
    escape_rcdata to True
    Tc                 c   s†   |  dd¡}t|ƒD ]l}|sq| d¡rrt|ƒ}|durrt|ƒdurrd|› d�V  |t|ƒd d… }|r|V  q|  dd¡V  qdS )z,Escapes just bare & in HTML attribute valuesz&amp;r«   Nr×   rÀ   )ÚreplacerÍ   rÎ   rÏ   rÌ   rÆ   )rr   ÚstokenrÒ   rÓ   rs   rs   rt   Úescape_base_amp°  s    
z$BleachHTMLSerializer.escape_base_ampNc                 #   s~   d}d}t ƒ  ||¡D ]b}|rd|dkr,d}n0|rP|dkr\|  |¡E dH  d}qn|dkr\d}|V  q| d¡rrd}|V  qdS )zÜWrap HTMLSerializer.serialize and conver & to &amp; in attribute values

        Note that this converts & to &amp; in attribute values where the & isn't
        already part of an unambiguous character entity.

        Fú>r™   Nú=Tr‰   )r‘   Ú	serializerã   rÎ   )rr   Z
treewalkerÚencodingÚin_tagZafter_equalsrâ   r–   rs   rt   ræ   Ï  s"    
zBleachHTMLSerializer.serialize)N)r‹   rŒ   r�   rŽ   Zescape_rcdatarã   ræ   r¶   rs   rs   r–   rt   rà   £  s   
rà   )9rŽ   ÚrerÙ   ÚwarningsÚfilterwarningsÚDeprecationWarningZbleach._vendor.html5libr   r   r   Z!bleach._vendor.html5lib.constantsr	   r
   r   r½   Z$bleach._vendor.html5lib.filters.baser   Z)bleach._vendor.html5lib.filters.sanitizerr   r   r   r   r   r   ZSanitizerFilterZ$bleach._vendor.html5lib._inputstreamr   Z"bleach._vendor.html5lib.serializerr   r   Z"bleach._vendor.html5lib._tokenizerr   r   Zbleach._vendor.html5lib._trier   ÚentitiesrÉ   rÛ   Z
tokenTypesr    r²   ZTAG_TOKEN_TYPE_ENDr§   r¨   rº   Z	HTML_TAGSr³   rl   r�   r·   rÌ   rÕ   rÏ   ÚcompilerÞ   rÍ   rà   rs   rs   rs   rt   Ú<module>   s\   ü ý



ÿyÿ'? 1,"#<
