a
    ¥Q•hLb  ã                   @   s  d dl Z d dlZd dlmZmZ d dlmZ d dlmZm	Z	 d dl
Z
ddlmZmZmZ edd„ eD ƒƒZed	d„ eD ƒƒZed
d„ e	D ƒƒZeeddgƒB Ze d¡Zh d£Ze d¡Zi Zdd„ ZG dd„ dƒZG dd„ deƒZG dd„ deƒZG dd„ dƒZG dd„ dƒZdd„ Z dS )é    N)ÚBytesIOÚStringIO)ÚPath)Úascii_lettersÚascii_uppercaseé   )ÚEOFÚReparseErrorÚspace_charactersc                 c   s   | ]}|  ¡ V  qd S ©N©Úencode©Ú.0Úitem© r   úN/var/www/sistema_ama/venv/lib/python3.9/site-packages/tinyhtml5/inputstream.pyÚ	<genexpr>   ó    r   c                 c   s   | ]}|  ¡ V  qd S r   r   r   r   r   r   r      r   c                 c   s   | ]}|  ¡ V  qd S r   r   r   r   r   r   r      r   ó   >ó   <u¡   [---ÂŸï·�-ï·¯ï¿¾ï¿¿ðŸ¿¾ðŸ¿¿ð¯¿¾ð¯¿¿ð¿¿¾ð¿¿¿ñ�¿¾ñ�¿¿ñŸ¿¾ñŸ¿¿ñ¯¿¾ñ¯¿¿ñ¿¿¾ñ¿¿¿ò�¿¾ò�¿¿òŸ¿¾òŸ¿¿ò¯¿¾ò¯¿¿ò¿¿¾ò¿¿¿ó�¿¾ó�¿¿óŸ¿¾óŸ¿¿ó¯¿¾ó¯¿¿ó¿¿¾ó¿¿¿ô�¿¾ô�¿¿í €-í¿¿]>    iþÿ
 iþÿ iþÿ iÿÿ iÿÿ iÿÿ iÿÿ iÿÿ iÿÿ iþÿ iþÿ iþÿ iþÿ iþÿ	 iÿÿ iÿÿ iÿÿ	 iÿÿ iÿÿ iÿÿ iÿÿ iÿÿ iþÿ iþÿ iþÿ iþÿ iþÿ iÿÿ
 iþÿ iþÿ iþÿ iÿÿ z[	- -/:-@\[-`{-~]c                 K   sš   t | tƒr:t| ƒdk r:t| ƒ ¡ r:tt| ƒ ¡ fi |¤ŽS t | tƒrXt|  ¡ fi |¤ŽS t t| dƒrn|  d¡n| tƒr†t| fi |¤ŽS t	| fi |¤ŽS d S )NéÈ   Úreadr   )
Ú
isinstanceÚstrÚlenr   Úis_fileÚHTMLUnicodeInputStreamÚ	read_textÚhasattrr   ÚHTMLBinaryInputStream)ÚsourceÚkwargsr   r   r   ÚHTMLInputStream&   s    "
r#   c                   @   sZ   e Zd 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d„Zdd„ ZdS )r   z×Provides a Unicode stream of characters to the HTMLTokenizer.

    This class takes care of character encoding and removing or replacing
    incorrect byte-sequences and also provides column and line tracking.

    c                 C   s.   dg| _ tdƒdf| _|  |¡| _|  ¡  dS )aš  Initialise the HTMLInputStream.

        Create a normalized stream from source for use by tinyhtml5.

        source can be either a file-object, local filename or a string.

        The optional encoding parameter must be a string that indicates
        the encoding.  If specified, that encoding will be used,
        regardless of any BOM or later declaration (such as in a meta
        element).

        r   úutf-8ÚcertainN)Z	new_linesÚlookup_encodingÚencodingÚopen_streamÚstreamÚreset©Úselfr!   r   r   r   Ú__init__9   s    zHTMLUnicodeInputStream.__init__c                 C   s.   d| _ d| _d| _g | _d| _d| _d | _d S )NÚ r   )ÚchunkÚ
chunk_sizeÚchunk_offsetÚerrorsÚprevious_number_linesÚprevious_number_columnsÚ_buffered_character©r,   r   r   r   r*   N   s    zHTMLUnicodeInputStream.resetc                 C   s   t |dƒr|S t|ƒS )zuProduce a file object from source.

        source can be either a file object, local filename or a string.

        r   )r   r   r+   r   r   r   r(   \   s    z"HTMLUnicodeInputStream.open_streamc                 C   sT   | j }| dd|¡}| j| }| dd|¡}|dkr@| j| }n||d  }||fS )NÚ
r   éÿÿÿÿr   )r/   Úcountr3   Úrfindr4   )r,   Úoffsetr/   Znumber_linesZposition_lineZlast_line_positionZposition_columnr   r   r   Ú	_positiond   s    
z HTMLUnicodeInputStream._positionc                 C   s   |   | j¡\}}|d |fS )z9Return (line, col) of the current position in the stream.r   )r<   r1   )r,   ÚlineÚcolumnr   r   r   Úpositiono   s    zHTMLUnicodeInputStream.positionc                 C   s6   | j | jkr|  ¡ stS | j }| j| }|d | _ |S )zlRead one character from the stream or queue if available.

        Return EOF when EOF is reached.

        r   )r1   r0   Ú
read_chunkr   r/   )r,   r1   Ú	characterr   r   r   rA   t   s    

z HTMLUnicodeInputStream.characterc                 C   sð   |   | j¡\| _| _d| _d| _d| _| j d¡}| jrJ| j| }d | _n|sRdS t	|ƒdkr t
|d ƒ}|dksŠd|  kr†d	kr n n|d | _|d d… }tt	t |¡ƒƒD ]}| j d
¡ q²| dd¡}| dd¡}|| _t	|ƒ| _dS )Nr.   r   i (  Fr   r8   é   i Ø  iÿÛ  zinvalid-codepointz
r7   úT)r<   r0   r3   r4   r/   r1   r)   r   r5   r   ÚordÚrangeÚinvalid_unicode_reÚfindallr2   ÚappendÚreplace)r,   ÚdataÚlastÚ_r   r   r   r@   …   s0    ÿ
 

z!HTMLUnicodeInputStream.read_chunkFc                 C   sð   zt ||f }W nV tyf   d dd„ |D ƒ¡}|s@d|› �}t d|› d�¡}| }t ||f< Y n0 g }| | j| j¡}|du r”| j| jkrÄqæn0| 	¡ }|| jkrÄ| 
| j| j|… ¡ || _qæ| 
| j| jd… ¡ |  ¡ slqæqld |¡S )a   Return a string of characters from the stream.

        String goes up to but does not include any character in 'characters' or
        EOF. 'characters' must be a container that supports the 'in' method and
        iteration over its characters.

        r.   c                 S   s   g | ]}d t |ƒd›�‘qS )z\xZ02x)rD   )r   rA   r   r   r   Ú
<listcomp>·   r   z6HTMLUnicodeInputStream.chars_until.<locals>.<listcomp>ú^ú[z]+N)Úcharacters_until_regexÚKeyErrorÚjoinÚreÚcompileÚmatchr/   r1   r0   ÚendrH   r@   )r,   Ú
charactersZoppositeÚregexÚresultrU   rV   r   r   r   Úchars_untilª   s,    


z"HTMLUnicodeInputStream.chars_untilc                 C   sT   |t urP| jdkr.|| j | _|  jd7  _n"|  jd8  _| j| j |ksPJ ‚d S )Nr   r   )r   r1   r/   r0   )r,   Úcharr   r   r   ÚungetØ   s    
zHTMLUnicodeInputStream.ungetN)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r-   r*   r(   r<   r?   rA   r@   rZ   r\   r   r   r   r   r   1   s   %
.r   c                       sR   e Zd ZdZddd„Z‡ fdd„Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
‡  ZS )r    zÕProvide a binary stream of characters to the HTMLTokenizer.

    This class takes care of character encoding and removing or replacing
    incorrect byte-sequences and also provides column and line tracking.

    Núwindows-1252c                 C   sX   |   |¡| _d| _|| _|| _|| _|| _|| _|  ¡ | _	| j	d d usLJ ‚|  
¡  d S )Ni   r   )r(   Ú
raw_streamÚnumber_bytes_metaÚoverride_encodingÚtransport_encodingÚsame_origin_parent_encodingÚlikely_encodingÚdefault_encodingÚdetermine_encodingr'   r*   )r,   r!   rd   re   rf   rg   rh   r   r   r   r-   ð   s    
zHTMLBinaryInputStream.__init__c                    s*   | j d jj}|| jdƒ| _tƒ  ¡  d S )Nr   rI   )r'   Ú
codec_infoÚstreamreaderrb   r)   Úsuperr*   )r,   rk   ©Ú	__class__r   r   r*   	  s    zHTMLBinaryInputStream.resetc                 C   s0   t |dƒr(t |dƒr | ¡ r |S | ¡ }t|ƒS )Nr   Úseekable)r   ro   r   r   r+   r   r   r   r(     s
    
z!HTMLBinaryInputStream.open_streamc                 C   sê   |   ¡ df}|d d ur|S t| jƒdf}|d d ur:|S t| jƒdf}|d d urX|S |  ¡ df}|d d urt|S t| jƒdf}|d d ur¢|d j d¡s¢|S t| jƒdf}|d d urÀ|S t| j	ƒdf}|d d urÞ|S tdƒdfS )Nr%   r   Ú	tentativezutf-16ra   )
Ú
detect_bomr&   rd   re   Údetect_encoding_metarf   ÚnameÚ
startswithrg   rh   )r,   r'   r   r   r   ri     s,    z(HTMLBinaryInputStream.determine_encodingc                 C   s¢   | j d dksJ ‚t|ƒ }d u r&d S |jdv rFtdƒ}|d usžJ ‚nX|| j d krf| j d df| _ n8| j d¡ |df| _ |  ¡  td| j d › d|› �ƒ‚d S )Nr   r%   ©úutf-16beúutf-16ler$   r   zEncoding changed from z to )r'   r&   rs   rb   Úseekr*   r	   )r,   Znew_encodingr   r   r   Úchange_encoding=  s    

ÿz%HTMLBinaryInputStream.change_encodingc              
   C   s„   t jdt jdt jdt jdt jdi}| j d¡}t|t	ƒs<J ‚dD ]2}| 
|d|… ¡ }r@| j |¡ t|ƒ  S q@| j d	¡ dS )
z´Attempt to detect at BOM at the start of the stream.

        If an encoding can be determined from the BOM return the name of the
        encoding otherwise return None.

        r$   rw   rv   zutf-32lezutf-32beé   )é   rz   é   Nr   )ÚcodecsÚBOM_UTF8ÚBOM_UTF16_LEÚBOM_UTF16_BEÚBOM_UTF32_LEÚBOM_UTF32_BErb   r   r   ÚbytesÚgetrx   r&   )r,   ZbomsÚstringrx   r'   r   r   r   rq   M  s    û	z HTMLBinaryInputStream.detect_bomc                 C   sV   | j  | j¡}t|tƒsJ ‚t|ƒ}| j  d¡ | ¡ }|durR|jdv rRt	dƒ}|S )z1Report the encoding declared by the meta element.r   Nru   r$   )
rb   r   rc   r   rƒ   ÚEncodingParserrx   Úget_encodingrs   r&   )r,   ÚbufferÚparserr'   r   r   r   rr   j  s    z*HTMLBinaryInputStream.detect_encoding_meta)NNNNra   )r]   r^   r_   r`   r-   r*   r(   ri   ry   rq   rr   Ú__classcell__r   r   rm   r   r    è   s      þ
(r    c                   @   sz   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	e
e	eƒZe
dd„ ƒZefdd„Zdd„ Zdd„ Zdd„ ZdS )ÚEncodingBytesz«Bytes-like object with an associated position and various extra methods.

    If the position is ever greater than the string length then an exception is
    raised.

    c                 C   s   t |tƒsJ ‚t | | ¡ ¡S r   )r   rƒ   Ú__new__Úlower)ÚclsÚvaluer   r   r   rŒ   €  s    zEncodingBytes.__new__c                 C   s
   d| _ d S )Nr8   ©r<   )r,   r�   r   r   r   r-   „  s    zEncodingBytes.__init__c                 C   s0   | j d  }| _ |t| ƒkr t‚| ||d … S ©Nr   ©r<   r   ÚStopIteration©r,   r?   r   r   r   Ú__next__‡  s    zEncodingBytes.__next__c                 C   s    | j d  | _ }| ||d … S r‘   r�   r”   r   r   r   Úprevious�  s    zEncodingBytes.previousc                 C   s"   | j t| ƒkrt‚td|ƒ| _ d S ©Nr   )r<   r   r“   Úmaxr”   r   r   r   Úset_position‘  s    zEncodingBytes.set_positionc                 C   s&   | j t| ƒkrt‚| j dkr"| j S d S r—   r’   r6   r   r   r   Úget_position–  s    
zEncodingBytes.get_positionc                 C   s   | | j | j d … S r‘   )r?   r6   r   r   r   Úcurrent_bytež  s    zEncodingBytes.current_bytec                 C   sH   | j }|t| ƒk r>| ||d … }||vr4|| _|S |d7 }q|| _dS )zSkip past a list of characters.r   N©r?   r   r<   ©r,   rW   r?   rA   r   r   r   Úskip¢  s    
zEncodingBytes.skipc                 C   sH   | j }|t| ƒk r>| ||d … }||v r4|| _|S |d7 }q|| _d S r‘   rœ   r�   r   r   r   Ú
skip_until®  s    
zEncodingBytes.skip_untilc                 C   s(   |   || j¡ }r$|  jt|ƒ7  _|S )zßLook for a sequence of bytes at the start of a string.

        If the bytes are found return True and advance the position to the byte
        after the match. Otherwise return False and leave the position alone.

        )rt   r?   r   )r,   rƒ   rY   r   r   r   Úmatch_bytes¹  s    zEncodingBytes.match_bytesc                 C   s<   z |   || j¡t|ƒ d | _W n ty6   t‚Y n0 dS )z™Look for the next sequence of bytes matching a given sequence.

        If a match is found advance the position to the last byte of the match.

        r   T)Úindexr?   r   r<   Ú
ValueErrorr“   )r,   rƒ   r   r   r   Újump_toÄ  s
     
zEncodingBytes.jump_toN)r]   r^   r_   r`   rŒ   r-   r•   r–   r™   rš   Úpropertyr?   r›   Úspace_characters_bytesrž   rŸ   r    r£   r   r   r   r   r‹   x  s   

r‹   c                   @   sX   e Zd 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S )r†   z@Mini parser for detecting character encoding from meta elements.c                 C   s   t |ƒ| _d | _d S r   )r‹   rJ   r'   ©r,   rJ   r   r   r   r-   Ô  s    
zEncodingParser.__init__c              
   C   sÀ   d| j vrd S | j| j| j| j| j| jdœ}| j D ]†}d}z| j  d¡ W n tyb   Y  qºY n0 | ¡ D ]B\}}| j  	|¡rlz|ƒ }W  q°W ql ty¬   d}Y  q°Y ql0 ql|s2 qºq2| j
S )Nó   <meta)s   <!--r§   s   </s   <!s   <?r   Tr   F)rJ   Úhandle_commentÚhandle_metaÚhandle_possible_end_tagÚhandle_otherÚhandle_possible_start_tagr£   r“   Úitemsr    r'   )r,   Zmethod_dispatchrL   Zkeep_parsingÚkeyÚmethodr   r   r   r‡   Ø  s4    
ú

zEncodingParser.get_encodingc                 C   s   | j  d¡S )zSkip over comments.s   -->©rJ   r£   r6   r   r   r   r¨   ÷  s    zEncodingParser.handle_commentc                 C   sà   | j jtvrdS d}d }|  ¡  }d u r,dS |d dkr\|d dk}|rÚ|d urÚ|| _dS q|d dkrŒ|d }t|ƒ}|d urÚ|| _dS q|d dkrtt|d ƒƒ}| ¡  }d urt|ƒ}|d ur|rÖ|| _dS |}qd S )	NTFr   s
   http-equivr   s   content-typeó   charsets   content)	rJ   r›   r¥   Úget_attributer'   r&   ÚContentAttributeParserr‹   Úparse)r,   Z
has_pragmaZpending_encodingÚ	attributeZtentative_encodingÚcodecZcontent_parserr   r   r   r©   û  s4    zEncodingParser.handle_metac                 C   s   | j dd�S )NF©Úend_tag)Úhandle_possible_tagr6   r   r   r   r¬     s    z(EncodingParser.handle_possible_start_tagc                 C   s   t | jƒ | jdd�S )NTr·   )ÚnextrJ   r¹   r6   r   r   r   rª     s    
z&EncodingParser.handle_possible_end_tagc                 C   sX   | j }|jtvr(|r$| ¡  |  ¡  dS | t¡}|dkrD| ¡  n|  ¡ d u rDqTqDdS )NTr   )rJ   r›   Úascii_letters_bytesr–   r«   rŸ   Úspaces_angle_bracketsr²   )r,   r¸   rJ   rA   r   r   r   r¹   #  s    


z"EncodingParser.handle_possible_tagc                 C   s   | j  d¡S )Nr   r°   r6   r   r   r   r«   :  s    zEncodingParser.handle_otherc                 C   sô  | j }| ttdgƒB ¡}|du s2t|ƒdks2J ‚|dv r>dS g }g }|dkrV|rVq¸nX|tv rj| ¡ }q¸nD|dv r€d |¡dfS |tv r˜| | ¡ ¡ n|du r¤dS | |¡ t	|ƒ}qF|dkrÖ| 
¡  d |¡dfS t	|ƒ | ¡ }| }dv �rJt	|ƒ}||k�r"t	|ƒ d |¡d |¡fS |tv �r<| | ¡ ¡ qô| |¡ qônJ|d	k�rbd |¡dfS |tv �r|| | ¡ ¡ n|du �rŠdS | |¡ t	|ƒ}|tv �rºd |¡d |¡fS |tv �rÔ| | ¡ ¡ n|du �râdS | |¡ �q”dS )
z{Return a (name, value) pair for the next attribute in the stream.

        If no attribute is found, return None.

        ó   /Nr   )r   Nó   =)r½   r   r   )ó   'ó   "r   )rJ   rž   r¥   Ú	frozensetr   rR   Úascii_uppercase_bytesrH   r�   rº   r–   r¼   )r,   rJ   rA   Úattribute_nameÚattribute_valueÚquoter   r   r   r²   =  s`    










zEncodingParser.get_attributeN)r]   r^   r_   r`   r-   r‡   r¨   r©   r¬   rª   r¹   r«   r²   r   r   r   r   r†   Ñ  s   !r†   c                   @   s   e Zd Zdd„ Zdd„ ZdS )r³   c                 C   s   t |tƒsJ ‚|| _d S r   )r   rƒ   rJ   r¦   r   r   r   r-   �  s    zContentAttributeParser.__init__c                 C   s  � zú| j  d¡ | j  jd7  _| j  ¡  | j jdks<W d S | j  jd7  _| j  ¡  | j jdv rª| j j}| j  jd7  _| j j}| j  |¡r¢| j || j j… W S W d S nP| j j}z"| j  t¡ | j || j j… W W S  t� yø   | j |d …  Y W S 0 W n t�y   Y d S 0 d S )Nr±   r   r¾   )rÀ   r¿   )rJ   r£   r?   rž   r›   rŸ   r¥   r“   )r,   rÅ   Zold_positionr   r   r   r´   ‘  s.    

zContentAttributeParser.parseN)r]   r^   r_   r-   r´   r   r   r   r   r³   Œ  s   r³   c                 C   s\   t | tƒr.z|  d¡} W n ty,   Y dS 0 | durXzt | ¡W S  tyV   Y dS 0 dS )z‡Return the Python codec name corresponding to an encoding.

    Return None if the string doesn't correspond to a valid encoding.

    ÚasciiN)r   rƒ   ÚdecodeÚUnicodeDecodeErrorÚwebencodingsÚlookupÚAttributeError)r'   r   r   r   r&   ²  s    
r&   )!r}   rS   Úior   r   Úpathlibr   r…   r   r   rÉ   Ú	constantsr   r	   r
   rÁ   r¥   r»   rÂ   r¼   rT   rF   Znon_bmp_invalid_codepointsZascii_punctuation_rerP   r#   r   r    rƒ   r‹   r†   r³   r&   r   r   r   r   Ú<module>   s8   ÿÿ 8 Y <&