a
    |"krW  ã                   @   sÞ   d Z ddlZddlZddlmZ dgZe d¡Ze d¡Ze d¡Z	e d¡Z
e d	¡Ze d
¡Ze d¡Ze d¡Ze d¡Ze d¡Ze dej¡Ze dej¡Ze dej¡Ze d¡Ze d¡ZG dd„ dejƒZdS )zA parser for HTML and XHTML.é    N)ÚunescapeÚ
HTMLParserz[&<]z
&[a-zA-Z#]z%&([a-zA-Z][-.a-zA-Z0-9]*)[^a-zA-Z0-9]z)&#(?:[0-9]+|[xX][0-9a-fA-F]+)[^0-9a-fA-F]z	<[a-zA-Z]z
</[a-zA-Z]ú>z--!?>z-?>z0([a-zA-Z][^\t\n\r\f />]*)(?:[\t\n\r\f ]|/(?!>))*a{  
  (
    (?<=['"\t\n\r\f /])[^\t\n\r\f />][^\t\n\r\f /=>]*  # attribute name
   )
  ([\t\n\r\f ]*=[\t\n\r\f ]*        # value indicator
    ('[^']*'                        # LITA-enclosed value
    |"[^"]*"                        # LIT-enclosed value
    |(?!['"])[^>\t\n\r\f ]*         # bare value
    )
   )?
  (?:[\t\n\r\f ]|/(?!>))*           # possibly followed by a space
a  
  [a-zA-Z][^\t\n\r\f />]*           # tag name
  [\t\n\r\f /]*                     # optional whitespace before attribute name
  (?:(?<=['"\t\n\r\f /])[^\t\n\r\f />][^\t\n\r\f /=>]*  # attribute name
    (?:[\t\n\r\f ]*=[\t\n\r\f ]*    # value indicator
      (?:'[^']*'                    # LITA-enclosed value
        |"[^"]*"                    # LIT-enclosed value
        |(?!['"])[^>\t\n\r\f ]*     # bare value
       )
     )?
    [\t\n\r\f /]*                   # possibly followed by a space
   )*
   >?
aF  
  <[a-zA-Z][^\t\n\r\f />\x00]*       # tag name
  (?:[\s/]*                          # optional whitespace before attribute name
    (?:(?<=['"\s/])[^\s/>][^\s/=>]*  # attribute name
      (?:\s*=+\s*                    # value indicator
        (?:'[^']*'                   # LITA-enclosed value
          |"[^"]*"                   # LIT-enclosed value
          |(?!['"])[^>\s]*           # bare value
         )
        \s*                          # possibly followed by a space
       )?(?:\s|/(?!>))*
     )*
   )?
  \s*                                # trailing whitespace
z#</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)\s*>c                   @   s   e Zd ZdZdZdZdddœdd„Zd	d
„ Zdd„ Zdd„ Z	dZ
dd„ Zddœdd„Zdd„ Zd>dd„Zdd„ Zdd„ Zd?dd„Zd@d d!„Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Zd:d;„ Zd<d=„ Z dS )Ar   aE  Find tags and other markup and call handler functions.

    Usage:
        p = HTMLParser()
        p.feed(data)
        ...
        p.close()

    Start tags are handled by calling self.handle_starttag() or
    self.handle_startendtag(); end tags by self.handle_endtag().  The
    data between tags is passed from the parser to the derived class
    by calling self.handle_data() with the data as argument (the data
    may be split up in arbitrary chunks).  If convert_charrefs is
    True the character references are converted automatically to the
    corresponding Unicode character (and self.handle_data() is no
    longer split in chunks), otherwise they are passed by calling
    self.handle_entityref() or self.handle_charref() with the string
    containing respectively the named or numeric reference as the
    argument.
    )ZscriptÚstyleZxmpZiframeZnoembedZnoframes)ZtextareaÚtitleTF)Úconvert_charrefsÚ	scriptingc                C   s   || _ || _|  ¡  dS )az  Initialize and reset this instance.

        If convert_charrefs is true (the default), all character references
        are automatically converted to the corresponding Unicode characters.

        If *scripting* is false (the default), the content of the
        ``noscript`` element is parsed normally; if it's true,
        it's returned as is without being parsed.
        N)r   r   Úreset)Úselfr   r   © r   ú#/usr/lib64/python3.9/html/parser.pyÚ__init__v   s    
zHTMLParser.__init__c                 C   sF   d| _ d| _t| _d| _d| _d| _g | _d| _d| _	t
j | ¡ dS )z1Reset this instance.  Loses all unprocessed data.Ú z???NTr   é   )ÚrawdataÚlasttagÚinteresting_normalÚinterestingÚ
cdata_elemÚ_support_cdataÚ
_escapableÚ_pendingÚ_pending_lenÚ_parse_thresholdÚ_markupbaseÚ
ParserBaser	   ©r
   r   r   r   r	   „   s    zHTMLParser.resetc                 C   s®   |  j t|ƒ7  _ | j | jk r,| j |¡ n~| jsB|  j|7  _n,| j |¡ |  jd | j¡7  _| j ¡  d| _ t| jƒ}|  d¡ t| jƒ|k ržd| _nt| jƒ| _dS )z‘Feed data to the parser.

        Call this as often as you want, with as little or as much text
        as you want (may include '\n').
        r   r   r   N)	r   Úlenr   r   Úappendr   ÚjoinÚclearÚgoahead)r
   ÚdataÚnr   r   r   Úfeed‘   s    



zHTMLParser.feedc                 C   s:   | j r,|  jd | j ¡7  _| j  ¡  d| _|  d¡ dS )zHandle any buffered data.r   r   r   N)r   r   r   r    r   r!   r   r   r   r   Úclose¯   s
    
zHTMLParser.closeNc                 C   s   | j S )z)Return full source of start tag: '<...>'.)Ú_HTMLParser__starttag_textr   r   r   r   Úget_starttag_text¹   s    zHTMLParser.get_starttag_text©Ú	escapablec                C   sp   |  ¡ | _|| _| jdkr(t d¡| _nD|rP| jsPt d| j tjtjB ¡| _nt d| j tjtjB ¡| _d S )NÚ	plaintextz\Zz&|</%s(?=[\t\n\r\f />])z</%s(?=[\t\n\r\f />]))	Úlowerr   r   ÚreÚcompiler   r   Ú
IGNORECASEÚASCII)r
   Úelemr)   r   r   r   Úset_cdata_mode½   s    



ÿ
ÿzHTMLParser.set_cdata_modec                 C   s   t | _d | _d| _d S )NT)r   r   r   r   r   r   r   r   Úclear_cdata_modeÉ   s    zHTMLParser.clear_cdata_modec                 C   s
   || _ dS )a  Enable or disable support of the CDATA sections.
        If enabled, "<[CDATA[" starts a CDATA section which ends with "]]>".
        If disabled, "<[CDATA[" starts a bogus comments which ends with ">".

        This method is not called by default. Its purpose is to be called
        in custom handle_starttag() and handle_endtag() methods, with
        value that depends on the adjusted current node.
        See https://html.spec.whatwg.org/multipage/parsing.html#markup-declaration-open-state
        for details.
        N)r   )r
   Úflagr   r   r   Ú_set_support_cdataÎ   s    zHTMLParser._set_support_cdatac                 C   sB  | j }d}t|ƒ}||k �rÚ| jrv| jsv| d|¡}|dk r | dt||d ƒ¡}|dkrpt d¡ 	||¡sp�qÚ|}n*| j
 	||¡}|r’| ¡ }n| jrœ�qÚ|}||k rÞ| jrÌ| jrÌ|  t|||… ƒ¡ n|  |||… ¡ |  ||¡}||krö�qÚ|j}|d|ƒ�r<t ||¡�r"|  |¡}	nŒ|d|ƒ�r:|  |¡}	nt|d|ƒ�rR|  |¡}	n\|d|ƒ�rj|  |¡}	nD|d	|ƒ�r‚|  |¡}	n,|d
 |k �s–|�rÚ|  d¡ |d
 }	n�qÚ|	dk �r.|�sÂ�qÚt ||¡�rÔ�nV|d|ƒ�r$|d |k�rú|  d¡ n&t ||¡�r
n|  ||d d … ¡ �n|d|ƒ�r||}dD ]*}
| |
|d ¡�r8|t|
ƒ8 } �qd�q8|  ||d |… ¡ n®|d|ƒ�r¨| j�r¨|  ||d d … ¡ n‚|||d …  ¡ dk�rÚ|  ||d d … ¡ nP|d	|ƒ�rþ|  ||d d … ¡ n,|d|ƒ�r"|  ||d d … ¡ nt dƒ‚|}	|  ||	¡}q|d|ƒ�rât! ||¡}|�r¤| "¡ dd… }|  #|¡ | $¡ }	|d|	d
 ƒ�s”|	d
 }	|  ||	¡}qn<d||d … v �rÚ|  |||d … ¡ |  ||d ¡}�qÚq|d|ƒ�rÌt% ||¡}|�rB| "d
¡}|  &|¡ | $¡ }	|d|	d
 ƒ�s4|	d
 }	|  ||	¡}qt' ||¡}|�rœ|�rÚ| "¡ ||d … k�rÚ| $¡ }	|	|k�r†|}	|  ||d
 ¡}�qÚn.|d
 |k �rÚ|  d¡ |  ||d
 ¡}n�qÚqdsJ dƒ‚q|�r0||k �r0| j�r| j�r|  t|||… ƒ¡ n|  |||… ¡ |  ||¡}||d … | _ d S )Nr   ú<ú&é"   z[\t\n\r\f ;]ú</ú<!--ú<?ú<!r   é   )z--!z--ú-é   ú	<![CDATA[é   é	   ú	<!doctypezwe should not get here!z&#éÿÿÿÿú;zinteresting.search() lied)(r   r   r   r   ÚfindÚrfindÚmaxr,   r-   Úsearchr   Ústartr   Úhandle_datar   Z	updateposÚ
startswithÚstarttagopenÚmatchÚparse_starttagÚparse_endtagÚparse_commentÚparse_piÚparse_html_declarationÚ
endtagopenÚhandle_commentÚendswithr   Úunknown_declr+   Úhandle_declÚ	handle_piÚAssertionErrorÚcharrefÚgroupÚhandle_charrefÚendÚ	entityrefÚhandle_entityrefÚ
incomplete)r
   r]   r   Úir#   ÚjZampposrM   rK   ÚkÚsuffixÚnamer   r   r   r!   Þ   sâ    
ÿ









zHTMLParser.goaheadc                 C   st  | j }|||d … dks"J dƒ‚|||d … dkr@|  |¡S |||d … dkr”| jr”| d|d ¡}|d	k rvd
S |  ||d |… ¡ |d S |||d …  ¡ dkræ| d|d ¡}|d
krÈd
S |  ||d |… ¡ |d S |||d … dk�rf| d|d ¡}|d	k �rd
S ||d  dk�rH|  ||d |d … ¡ n|  ||d |… ¡ |d S |  |¡S d S )Nr<   r;   z+unexpected call to parse_html_declaration()r>   r9   rA   r?   z]]>r   rC   r@   rB   r   r   z<![ú])	r   rP   r   rE   rV   r+   rW   rT   Úparse_bogus_comment)r
   ra   r   rb   Zgtposr   r   r   rR   j  s2    

z!HTMLParser.parse_html_declarationc                 C   sp   | j }| d|¡sJ dƒ‚t ||d ¡}|sFt ||d ¡}|sFdS |rh| ¡ }|  ||d |… ¡ | ¡ S )Nr9   ú"unexpected call to parse_comment()r>   rC   )	r   rK   ÚcommentcloserH   ÚcommentabruptcloserM   rI   rT   r]   )r
   ra   Úreportr   rM   rb   r   r   r   rP   Œ  s    zHTMLParser.parse_commentr   c                 C   s`   | j }|||d … dv s"J dƒ‚| d|d ¡}|dkr>dS |rX|  ||d |… ¡ |d S )Nr<   )r;   r8   rh   r   rC   r   )r   rE   rT   )r
   ra   rk   r   Úposr   r   r   rg   ›  s    zHTMLParser.parse_bogus_commentc                 C   sd   | j }|||d … dks"J dƒ‚t ||d ¡}|s:dS | ¡ }|  ||d |… ¡ | ¡ }|S )Nr<   r:   zunexpected call to parse_pi()rC   )r   ÚpicloserH   rI   rX   r]   ©r
   ra   r   rM   rb   r   r   r   rQ   §  s    zHTMLParser.parse_pic                 C   s(  d | _ |  |¡}|dk r|S | j}|||… | _ g }t ||d ¡}|sPJ dƒ‚| ¡ }| d¡ ¡  | _}||k �r.t	 ||¡}|sŠ�q.| ddd¡\}	}
}|
s¨d }n\|d d… d  krÌ|dd … ksøn |d d… d  krô|dd … k�rn n|dd… }|�rt
|ƒ}| |	 ¡ |f¡ | ¡ }ql|||…  ¡ }|d	v�r¬|  ¡ \}}d
| j v �rˆ|| j  d
¡ }t| j ƒ| j  d
¡ }n|t| j ƒ }|  |||… ¡ |S | d¡�rÆ|  ||¡ n^|  ||¡ || jv �sú| j�rð|dk�sú|dk�r
| j|dd� n|| jv �r$| j|dd� |S )Nr   r   z#unexpected call to parse_starttag()r<   r@   ú'rC   ú")r   ú/>Ú
rq   Znoscriptr*   Fr(   T)r&   Úcheck_for_whole_start_tagr   Útagfind_tolerantrM   r]   r[   r+   r   Úattrfind_tolerantr   r   ÚstripZgetposÚcountr   rF   rJ   rU   Úhandle_startendtagÚhandle_starttagÚCDATA_CONTENT_ELEMENTSr   r1   ÚRCDATA_CONTENT_ELEMENTS)r
   ra   Úendposr   ÚattrsrM   rc   ÚtagÚmÚattrnameÚrestZ	attrvaluer]   ÚlinenoÚoffsetr   r   r   rN   ³  sj    

&ÿ
ÿ


ÿÿÿþzHTMLParser.parse_starttagc                 C   s>   | j }t ||d ¡}|sJ ‚| ¡ }||d  dkr:dS |S )Nr   r   rC   )r   ÚlocatetagendrM   r]   rn   r   r   r   rs   ì  s    z$HTMLParser.check_for_whole_start_tagc                 C   sà   | j }|||d … dks"J dƒ‚| d|d ¡dk r:dS t ||¡sp||d |d … dkrf|d S |  |¡S t ||d ¡}|sˆJ ‚| ¡ }||d  dkr¤dS t ||d ¡}|s¼J ‚| d¡ 	¡ }|  
|¡ |  ¡  |S )	Nr<   r8   zunexpected call to parse_endtagr   r   rC   r@   r   )r   rE   rS   rM   rg   r„   r]   rt   r[   r+   Úhandle_endtagr2   )r
   ra   r   rM   rb   r~   r   r   r   rO   ö  s&    

zHTMLParser.parse_endtagc                 C   s   |   ||¡ |  |¡ d S ©N)ry   r…   ©r
   r~   r}   r   r   r   rx     s    zHTMLParser.handle_startendtagc                 C   s   d S r†   r   r‡   r   r   r   ry     s    zHTMLParser.handle_starttagc                 C   s   d S r†   r   )r
   r~   r   r   r   r…     s    zHTMLParser.handle_endtagc                 C   s   d S r†   r   ©r
   re   r   r   r   r\   !  s    zHTMLParser.handle_charrefc                 C   s   d S r†   r   rˆ   r   r   r   r_   %  s    zHTMLParser.handle_entityrefc                 C   s   d S r†   r   ©r
   r"   r   r   r   rJ   )  s    zHTMLParser.handle_datac                 C   s   d S r†   r   r‰   r   r   r   rT   -  s    zHTMLParser.handle_commentc                 C   s   d S r†   r   )r
   Zdeclr   r   r   rW   1  s    zHTMLParser.handle_declc                 C   s   d S r†   r   r‰   r   r   r   rX   5  s    zHTMLParser.handle_pic                 C   s   d S r†   r   r‰   r   r   r   rV   8  s    zHTMLParser.unknown_decl)T)T)r   )!Ú__name__Ú
__module__Ú__qualname__Ú__doc__rz   r{   r   r	   r$   r%   r&   r'   r1   r2   r4   r!   rR   rP   rg   rQ   rN   rs   rO   rx   ry   r…   r\   r_   rJ   rT   rW   rX   rV   r   r   r   r   r   Z   s>   
 "

9
)r�   r,   r   Zhtmlr   Ú__all__r-   r   r`   r^   rZ   rL   rS   rm   ri   rj   rt   ÚVERBOSEru   r„   Zlocatestarttagend_tolerantZ	endendtagZ
endtagfindr   r   r   r   r   r   Ú<module>   s4   











õóò

