a
    ˜ëjšM  ã                   @  s*  d Z ddlmZ dZdgZddlmZ ddl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!ƒZ0dS )zCUse the HTMLParser library to parse HTML files that aren't too bad.é    )ÚannotationsÚMITÚHTMLParserTreeBuilder)Ú
HTMLParserN)Ú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.parserc                   @  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d8dddddœdd„Z	d9ddddœdd „Z
ddd!œd"d#„Ze d$¡Ze d%¡Ze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d0œd1d2„Zddd!œd3d4„Zddd!œd5d6„Zd7S ):Ú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   úV/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/bs4/builder/_htmlparser.pyr2   U   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   Úerrorp   s    zBeautifulSoupHTMLParser.errorzList[Tuple[str, Optional[str]]])ÚtagÚattrsr9   c                 C  s"   | j ||dd� | 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_element©Úcheck_already_closedN)Ú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Ú	sourceposFr>   )r1   r+   r)   r'   r   Ú_DuplicateAttributeHandlerr,   r0   Zstore_line_numbersÚgetposr@   Zis_empty_elementrA   r3   ÚappendZ_root_tag_nameZ_root_tag_encountered)r4   r;   r<   r=   Z	attr_dictÚkeyÚvalueZon_duperE   rF   ZtagObjr5   r5   r6   r@   ”   s0    




ÿ

z'BeautifulSoupHTMLParser.handle_starttag)r;   r?   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,   rA   )r4   r;   r?   r5   r5   r6   rA   Ò   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   rM   r5   r5   r6   rN   ä   s    z#BeautifulSoupHTMLParser.handle_dataz^([0-9]+)(.*)z^([0-9a-f]+)(.*)zTuple[str, bool, str])Únamer9   c           	      C  sÀ   d}d}d}d}| j }| d¡s*| d¡r@|dd… }d}| j}d}zt||ƒ}W nB ty”   | |¡}|dur�t| ¡ d	 |ƒ}| ¡ d }Y n0 |du r¨d}|}nt |¡\}}|||fS )
a°  Convert a numeric character reference into an actual character.

        :param name: The number of the character reference, as
          obtained by html.parser

        :return: A 3-tuple (dereferenced, replacement_added,
          extra_data). `dereferenced` is the dereferenced character
          reference, or the empty string if there was no
          reference. `replacement_added` is True if the reference
          could only be dereferenced by replacing content with U+FFFD
          REPLACEMENT CHARACTER. `extra_data` is a portion of data
          following the character reference, which was deemed to be
          normal data and not part of the reference at all.
        rD   Fé
   ÚxÚXé   Né   r   )	Ú&_DECIMAL_REFERENCE_WITH_FOLLOWING_DATAÚ
startswithÚ"_HEX_REFERENCE_WITH_FOLLOWING_DATAÚintÚ
ValueErrorÚsearchÚgroupsr   Znumeric_character_reference)	ÚclsrP   ÚdereferencedÚreplacement_addedÚ
extra_dataÚbaseÚregZ	real_nameÚmatchr5   r5   r6   Ú(_dereference_numeric_character_referenceë   s,    
z@BeautifulSoupHTMLParser._dereference_numeric_character_referencec                 C  sD   |   |¡\}}}|rd| j_|dur.|  |¡ |dur@|  |¡ 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.
        TN)rd   r,   Úcontains_replacement_charactersrN   )r4   rP   r^   r_   r`   r5   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ÚgetrN   )r4   rP   Ú	characterrM   r5   r5   r6   Úhandle_entityref1  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,   ÚendDatarN   r   rO   r5   r5   r6   Úhandle_commentD  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,   rj   ÚlenrN   r   )r4   rl   r5   r5   r6   Úhandle_declM  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)ÚupperrW   r   rm   r   r,   rj   rN   )r4   rM   r]   r5   r5   r6   Úunknown_declW  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,   rj   rN   Z_document_might_be_xmlr   rO   r5   r5   r6   Ú	handle_pif  s    

z!BeautifulSoupHTMLParser.handle_piN)T)T)Ú__name__Ú
__module__Ú__qualname__r'   Ú__annotations__r)   r2   r:   rB   r@   rA   rN   ÚreÚcompilerV   rX   Úclassmethodrd   rf   ri   rk   rn   rp   rq   r5   r5   r5   r6   r$   >   s.   
ü ü>

6	
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.

    FrC   Ú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   )r}   Ú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   Úupdater}   )r4   r}   r   r.   Zextra_parser_kwargsÚargrK   ©Ú	__class__r5   r6   r2   ‚  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_htmlr‹   zPCould not convert input to Unicode, and html.parser will not accept bytestrings.)	Ú
isinstancer&   rI   r   Zunicode_markupr   Zoriginal_encodingZdeclared_html_encodingre   )r4   rˆ   r‰   rŠ   r‹   rŒ   r�   Zdammitr5   r5   r6   Úprepare_markup   s2    


û
ÿüz$HTMLParserTreeBuilder.prepare_markupztype[BeautifulSoupHTMLParser]r7   )rˆ   Ú_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)	r}   rŽ   r&   r,   ÚfeedÚcloseÚAssertionErrorr   r3   )r4   rˆ   r�   r-   r.   ÚparserÚer5   r5   r6   r‘   è  s    

zHTMLParserTreeBuilder.feed)NN)NNN)rr   rs   rt   Ú__doc__ry   ru   rz   Ú
HTMLPARSERr{   r   r   r|   r~   r2   r�   r$   r‘   Ú__classcell__r5   r5   r†   r6   r   q  s   
  ý!   ûH)1r–   Ú
__future__r   Ú__license__Ú__all__Úhtml.parserr   rv   Ú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&   rG   r$   r   r5   r5   r5   r6   Ú<module>   s*   ÿ4   5