a
    (ñ»j%D  ã                   @  s"  d Z ddlmZ dZdgZddlmZ ddlmZm	Z	m
Z
mZmZmZmZmZmZmZmZ ddlmZmZmZmZmZmZ ddlmZmZ dd	lmZmZm Z m!Z! dd
l"m#Z# erÞddl$m%Z% ddlm&Z& ddl'm(Z(m)Z)m*Z* dZ+e	ee,e,f e,e,gdf Z-G dd„ deeƒZ.G dd„ de ƒZ/dS )zCUse the HTMLParser library to parse HTML files that aren't too bad.é    )ÚannotationsÚMITÚHTMLParserTreeBuilder)Ú
HTMLParser)ÚAnyÚCallableÚcastÚDictÚIterableÚListÚOptionalÚTYPE_CHECKINGÚTupleÚTypeÚUnion)ÚAttributeDictÚCDataÚCommentÚDeclarationÚDoctypeÚProcessingInstruction)ÚEntitySubstitutionÚUnicodeDammit)ÚDetectsXMLParsedAsHTMLÚHTMLÚHTMLTreeBuilderÚSTRICT©ÚParserRejectedMarkup)ÚBeautifulSoup)ÚNavigableString)Ú	_EncodingÚ
_EncodingsÚ
_RawMarkupzhtml.parserNc                   @  s  e Zd ZU dZded< dZded< edœddd	dd
œdd„Zd	ed< ded< ded< dddœdd„Zddddœdd„Zd3dddddœdd„Z	d4ddddœdd „Z
ddd!œd"d#„Zddd$œd%d&„Zddd$œd'd(„Zddd!œd)d*„Zddd+œd,d-„Zddd!œd.d/„Zddd!œd0d1„Zd2S )5ÚBeautifulSoupHTMLParserÚreplaceÚstrÚREPLACEÚignoreÚIGNORE©Úon_duplicate_attributer   r   z&Union[str, _DuplicateAttributeHandler])ÚsoupÚargsr+   Úkwargsc                O  s@   || _ || _|jj| _tj| g|¢R i |¤Ž g | _|  ¡  d S ©N)r,   r+   ÚbuilderÚattribute_dict_classr   Ú__init__Úalready_closed_empty_elementZ_initialize_xml_detector)Úselfr,   r+   r-   r.   © r5   ú=E:\soccer\ml_env\Lib\site-packages\bs4/builder/_htmlparser.pyr2   T   s    
	z BeautifulSoupHTMLParser.__init__r+   z	List[str]r3   r,   ÚNone)ÚmessageÚreturnc                 C  s   t |ƒ‚d S r/   r   )r4   r8   r5   r5   r6   Úerroro   s    zBeautifulSoupHTMLParser.errorzList[Tuple[str, Optional[str]]])ÚtagÚattrsr9   c                 C  s   | j ||dd� |  |¡ dS )z…Handle an incoming empty-element tag.

        html.parser only calls this method when the markup looks like
        <tag/>.
        F)Úhandle_empty_elementN)Úhandle_starttagÚhandle_endtag)r4   r;   r<   r5   r5   r6   Úhandle_startendtag   s    z*BeautifulSoupHTMLParser.handle_startendtagTÚbool)r;   r<   r=   r9   c                 C  sð   |   ¡ }|D ]f\}}|du r d}||v rj| j}|| jkr:qr|d| jfv rR|||< qrtt|ƒ}||||ƒ q|||< q| jjjrŒ|  	¡ \}}	nd }}	| jj
|dd|||	d�}
|
durØ|
jrØ|rØ| j|dd� | j |¡ | jdu rì|  |¡ dS )zÓHandle an opening tag, e.g. '<tag>'

        :param handle_empty_element: True if this tag is known to be
            an empty-element tag (i.e. there is not expected to be any
            closing tag).
        NÚ )Ú
sourcelineÚ	sourceposF)Úcheck_already_closed)r1   r+   r)   r'   r   Ú_DuplicateAttributeHandlerr,   r0   Zstore_line_numbersÚgetposr>   Zis_empty_elementr?   r3   ÚappendZ_root_tag_nameZ_root_tag_encountered)r4   r;   r<   r=   Z	attr_dictÚkeyÚvalueZon_duperC   rD   ZtagObjr5   r5   r6   r>   Ž   s0    




ÿ

z'BeautifulSoupHTMLParser.handle_starttag)r;   rE   r9   c                 C  s,   |r|| j v r| j  |¡ n| j |¡ dS )zìHandle a closing tag, e.g. '</tag>'

        :param tag: A tag name.
        :param check_already_closed: True if this tag is expected to
           be the closing portion of an empty-element tag,
           e.g. '<tag></tag>'.
        N)r3   Úremover,   r?   )r4   r;   rE   r5   r5   r6   r?   Ì   s    	z%BeautifulSoupHTMLParser.handle_endtag)Údatar9   c                 C  s   | j  |¡ dS )z4Handle some textual data that shows up between tags.N)r,   Úhandle_data©r4   rL   r5   r5   r6   rM   Þ   s    z#BeautifulSoupHTMLParser.handle_data)Únamer9   c                 C  sh   |  d¡rt| d¡dƒ}n$|  d¡r8t| d¡dƒ}nt|ƒ}t |¡\}}|rZd| j_|  |¡ dS )z×Handle a numeric character reference by converting it to the
        corresponding Unicode character and treating it as textual
        data.

        :param name: Character number, possibly in hexadecimal.
        Úxé   ÚXTN)Ú
startswithÚintÚlstripr   Znumeric_character_referencer,   Úcontains_replacement_charactersrM   )r4   rO   Z	real_namerL   Zreplacement_addedr5   r5   r6   Úhandle_charrefâ   s    

z&BeautifulSoupHTMLParser.handle_charrefc                 C  s0   t j |¡}|dur|}nd| }|  |¡ dS )zÈHandle a named entity reference by converting it to the
        corresponding Unicode character(s) and treating it as textual
        data.

        :param name: Name of the entity reference.
        Nz&%s)r   ZHTML_ENTITY_TO_CHARACTERÚgetrM   )r4   rO   Ú	characterrL   r5   r5   r6   Úhandle_entityrefú   s
    z(BeautifulSoupHTMLParser.handle_entityrefc                 C  s&   | j  ¡  | j  |¡ | j  t¡ dS )zOHandle an HTML comment.

        :param data: The text of the comment.
        N)r,   ÚendDatarM   r   rN   r5   r5   r6   Úhandle_comment  s    
z&BeautifulSoupHTMLParser.handle_comment)Údeclr9   c                 C  s6   | j  ¡  |tdƒd… }| j  |¡ | j  t¡ dS )zYHandle a DOCTYPE declaration.

        :param data: The text of the declaration.
        zDOCTYPE N)r,   r[   ÚlenrM   r   )r4   r]   r5   r5   r6   Úhandle_decl  s    
z#BeautifulSoupHTMLParser.handle_declc                 C  sN   |  ¡  d¡r$t}|tdƒd… }nt}| j ¡  | j |¡ | j |¡ dS )z{Handle a declaration of unknown type -- probably a CDATA block.

        :param data: The text of the declaration.
        zCDATA[N)ÚupperrS   r   r^   r   r,   r[   rM   )r4   rL   Úclsr5   r5   r6   Úunknown_decl   s    
z$BeautifulSoupHTMLParser.unknown_declc                 C  s0   | j  ¡  | j  |¡ |  |¡ | j  t¡ dS )z\Handle a processing instruction.

        :param data: The text of the instruction.
        N)r,   r[   rM   Z_document_might_be_xmlr   rN   r5   r5   r6   Ú	handle_pi/  s    

z!BeautifulSoupHTMLParser.handle_piN)T)T)Ú__name__Ú
__module__Ú__qualname__r'   Ú__annotations__r)   r2   r:   r@   r>   r?   rM   rW   rZ   r\   r_   rb   rc   r5   r5   r5   r6   r$   =   s&   
ü ü>	
r$   c                      s¦   e Zd ZU dZdZded< dZded< eZded< ee	e
gZd	ed
< ded< dZded< d!ddddœ‡ fdd„Zd"ddddddœdd„Zefddddœdd „Z‡  ZS )#r   z—A Beautiful soup `bs4.builder.TreeBuilder` that uses the
    :py:class:`html.parser.HTMLParser` parser, found in the Python
    standard library.

    FrA   Úis_xmlTÚ	picklabler&   ÚNAMEzIterable[str]Úfeaturesz$Tuple[Iterable[Any], Dict[str, Any]]Úparser_argsÚTRACKS_LINE_NUMBERSNzOptional[Iterable[Any]]zOptional[Dict[str, Any]]r   )rl   Úparser_kwargsr.   c                   sp   t ƒ }dD ]}||v r
| |¡}|||< q
tt| ƒjf i |¤Ž |pFg }|pNi }| |¡ d|d< ||f| _dS )a‚  Constructor.

        :param parser_args: Positional arguments to pass into
            the BeautifulSoupHTMLParser constructor, once it's
            invoked.
        :param parser_kwargs: Keyword arguments to pass into
            the BeautifulSoupHTMLParser constructor, once it's
            invoked.
        :param kwargs: Keyword arguments for the superclass constructor.
        r*   FÚconvert_charrefsN)ÚdictÚpopÚsuperr   r2   Úupdaterl   )r4   rl   rn   r.   Zextra_parser_kwargsÚargrJ   ©Ú	__class__r5   r6   r2   K  s    


zHTMLParserTreeBuilder.__init__r#   zOptional[_Encoding]zOptional[_Encodings]zDIterable[Tuple[str, Optional[_Encoding], Optional[_Encoding], bool]])ÚmarkupÚuser_specified_encodingÚdocument_declared_encodingÚexclude_encodingsr9   c                 c  s€   t |tƒr|dddfV  dS g }|r.| |¡ g }|r@| |¡ t|||d|d�}|jdu rftdƒ‚n|j|j|j|jfV  dS )a2  Run any preliminary steps necessary to make incoming markup
        acceptable to the parser.

        :param markup: Some markup -- probably a bytestring.
        :param user_specified_encoding: The user asked to try this encoding.
        :param document_declared_encoding: The markup itself claims to be
            in this encoding.
        :param exclude_encodings: The user asked _not_ to try any of
            these encodings.

        :yield: A series of 4-tuples: (markup, encoding, declared encoding,
             has undergone character replacement)

            Each 4-tuple represents a strategy for parsing the document.
            This TreeBuilder uses Unicode, Dammit to convert the markup
            into Unicode, so the ``markup`` element of the tuple will
            always be a string.
        NFT)Úknown_definite_encodingsÚuser_encodingsZis_htmlrz   zPCould not convert input to Unicode, and html.parser will not accept bytestrings.)	Ú
isinstancer&   rH   r   Zunicode_markupr   Zoriginal_encodingZdeclared_html_encodingrV   )r4   rw   rx   ry   rz   r{   r|   Zdammitr5   r5   r6   Úprepare_markupi  s2    


û
ÿüz$HTMLParserTreeBuilder.prepare_markupztype[BeautifulSoupHTMLParser]r7   )rw   Ú_parser_classr9   c              
   C  sŒ   | j \}}t|tƒsJ ‚| jdus&J ‚|| jg|¢R i |¤Ž}z| |¡ | ¡  W n, ty€ } zt|ƒ‚W Y d}~n
d}~0 0 g |_dS )z®
        :param markup: The markup to feed into the parser.
        :param _parser_class: An HTMLParser subclass to use. This is only intended for use in unit tests.
        N)	rl   r}   r&   r,   ÚfeedÚcloseÚAssertionErrorr   r3   )r4   rw   r   r-   r.   ÚparserÚer5   r5   r6   r€   ±  s    

zHTMLParserTreeBuilder.feed)NN)NNN)rd   re   rf   Ú__doc__rh   rg   ri   Ú
HTMLPARSERrj   r   r   rk   rm   r2   r~   r$   r€   Ú__classcell__r5   r5   ru   r6   r   :  s   
  ý!   ûH)0r…   Ú
__future__r   Ú__license__Ú__all__Úhtml.parserr   Útypingr   r   r   r	   r
   r   r   r   r   r   r   Zbs4.elementr   r   r   r   r   r   Z
bs4.dammitr   r   Zbs4.builderr   r   r   r   Zbs4.exceptionsr   Zbs4r   r    Zbs4._typingr!   r"   r#   r†   r&   rF   r$   r   r5   r5   r5   r6   Ú<module>   s&   ÿ4  ~