
    >jM                    T   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ee          Z/ G 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                     e Zd ZU dZded<   dZded<   	 edd.dZded<   ded<   ded<   d/dZd0dZ	 d1d2dZ	d1d3dZ
d4d Z ej        d!          Z ej        d"          Zed5d%            Zd6d&Zd6d'Zd4d(Zd7d*Zd4d+Zd4d,Zd-S )8BeautifulSoupHTMLParserreplacestrREPLACEignoreIGNOREon_duplicate_attributesoupr    argsr   r-   &Union[str, _DuplicateAttributeHandler]kwargsc                   || _         || _        |j        j        | _        t	          j        | g|R i | g | _        |                                  d S N)r.   r-   builderattribute_dict_classr   __init__already_closed_empty_element_initialize_xml_detector)selfr.   r-   r/   r1   s        [/home/ice/.hermes/hermes-agent/venv/lib/python3.11/site-packages/bs4/builder/_htmlparser.pyr6   z BeautifulSoupHTMLParser.__init__U   sc     	&<#$(L$E!D242226222 -/)%%'''''    z	List[str]r7   messagereturnNonec                     t          |          r3   r   )r9   r<   s     r:   errorzBeautifulSoupHTMLParser.errorp   s     #7+++r;   tagattrsList[Tuple[str, Optional[str]]]c                d    |                      ||d           |                     |d           dS )zHandle 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)r9   rA   rB   s      r:   handle_startendtagz*BeautifulSoupHTMLParser.handle_startendtag   s@     	S%eDDD 	3U;;;;;r;   TrE   boolc                4   |                                  }|D ]Y\  }}|d}||v rG| j        }|| j        k    r |d| j        fv r|||<   1t	          t
          |          } ||||           T|||<   Z| j        j        j        r| 	                                \  }}	ndx}}	| j        
                    |dd|||	          }
|
:|
j        r3|r1|                     |d           | j                            |           | j        |                     |           dS dS )zHandle 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	sourceposFrF   )r5   r-   r+   r)   r	   _DuplicateAttributeHandlerr.   r4   store_line_numbersgetposrH   is_empty_elementrI   r7   append_root_tag_name_root_tag_encountered)r9   rA   rB   rE   	attr_dictkeyvalueon_duperN   rO   tagObjs              r:   rH   z'BeautifulSoupHTMLParser.handle_starttag   sv    $(#<#<#>#>	 	' 	'JC }i 5dk))t| 444%*IcNN"#=wGGGGIsE2222!&	# 9/ 	*$(KKMM!J		%))J**tY: + 
 
 &"9>R s??? -44S999&&&s+++++ '&r;   rG   c                    |r%|| j         v r| j                             |           dS | j                            |           dS )zHandle 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)r7   remover.   rI   )r9   rA   rG   s      r:   rI   z%BeautifulSoupHTMLParser.handle_endtag   sT       	)C4+L$L$L
 -44S99999I##C(((((r;   datac                :    | j                             |           dS )z4Handle some textual data that shows up between tags.N)r.   handle_datar9   r^   s     r:   r`   z#BeautifulSoupHTMLParser.handle_data   s    	d#####r;   z^([0-9]+)(.*)z^([0-9a-f]+)(.*)nameTuple[str, bool, str]c                   d}d}d}d}| j         }|                    d          s|                    d          r|dd         }d}| j        }d}	 t          ||          }ni# t          $ r\ |                    |          }|Bt          |                                d	         |          }|                                d         }Y nw xY w|d}|}nt          j        |          \  }}|||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.
        rM   F
   xX   N   r   )	&_DECIMAL_REFERENCE_WITH_FOLLOWING_DATA
startswith"_HEX_REFERENCE_WITH_FOLLOWING_DATAint
ValueErrorsearchgroupsr   numeric_character_reference)	clsrb   dereferencedreplacement_added
extra_databasereg	real_namematchs	            r:   (_dereference_numeric_character_referencez@BeautifulSoupHTMLParser._dereference_numeric_character_reference   s     !&
8??3 	94??3#7#7 	98DD8C"&		/D$II 	/ 	/ 	/ JJt$$E q 1488	"\\^^A.
	/" LJJ.;.WXa.b.b+L+.
::s   A! !A#CCc                    |                      |          \  }}}|rd| j        _        ||                     |           ||                     |           dS dS )zHandle 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)rz   r.   contains_replacement_charactersr`   )r9   rb   rs   rt   ru   s        r:   handle_charrefz&BeautifulSoupHTMLParser.handle_charref"  st     7;6c6cdh6i6i3' 	=8<DI5#\***!Z((((( "!r;   c                    t           j                            |          }||}nd|z  }|                     |           dS )zHandle 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   HTML_ENTITY_TO_CHARACTERgetr`   )r9   rb   	characterr^   s       r:   handle_entityrefz(BeautifulSoupHTMLParser.handle_entityref1  sL     '?CCDII	 DD 4<Dr;   c                    | j                                          | j                             |           | j                             t                     dS )zOHandle an HTML comment.

        :param data: The text of the comment.
        N)r.   endDatar`   r   ra   s     r:   handle_commentz&BeautifulSoupHTMLParser.handle_commentD  sJ    
 			d###	'"""""r;   declc                    | 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   lenr`   r   )r9   r   s     r:   handle_declz#BeautifulSoupHTMLParser.handle_declM  s`    
 		C
OO%%&	d###	'"""""r;   c                :   |                                                     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)upperrk   r   r   r   r.   r   r`   )r9   r^   rr   s      r:   unknown_declz$BeautifulSoupHTMLParser.unknown_declW  s     ::<<""8,, 	CH(DDC		d###	#r;   c                    | j                                          | j                             |           |                     |           | j                             t                     dS )z\Handle a processing instruction.

        :param data: The text of the instruction.
        N)r.   r   r`   _document_might_be_xmlr   ra   s     r:   	handle_piz!BeautifulSoupHTMLParser.handle_pif  s_    
 			d#####D)))	/00000r;   N)r.   r    r/   r   r-   r0   r1   r   )r<   r(   r=   r>   )rA   r(   rB   rC   r=   r>   )T)rA   r(   rB   rC   rE   rK   r=   r>   )rA   r(   rG   rK   r=   r>   )r^   r(   r=   r>   )rb   r(   r=   rc   )rb   r(   r=   r>   )r   r(   r=   r>   )__name__
__module____qualname__r)   __annotations__r+   r6   r@   rJ   rH   rI   r`   recompilerj   rl   classmethodrz   r}   r   r   r   r   r    r;   r:   r&   r&   >   s         G F$ JQ	( ( ( ( ( (. CBBB++++, , , , < < < <0 &*	<, <, <, <, <,|) ) ) ) )$$ $ $ $ .8RZ-H-H*)34F)G)G&4; 4; 4; [4;l) ) ) )   &# # # ## # # #   1 1 1 1 1 1r;   r&   c                       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$ fdZ	 	 	 d%d&dZefd'd"Z xZS )(r   zA Beautiful soup `bs4.builder.TreeBuilder` that uses the
    :py:class:`html.parser.HTMLParser` parser, found in the Python
    standard library.

    FrK   is_xmlT	picklabler(   NAMEzIterable[str]featuresz$Tuple[Iterable[Any], Dict[str, Any]]parser_argsTRACKS_LINE_NUMBERSNOptional[Iterable[Any]]parser_kwargsOptional[Dict[str, Any]]r1   r   c                    t                      }dD ] }||v r|                    |          }|||<   ! t          t          |           j        di | |pg }|pi }|                    |           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_charrefsNr   )dictpopsuperr   r6   updater   )r9   r   r   r1   extra_parser_kwargsargrY   	__class__s          r:   r6   zHTMLParserTreeBuilder.__init__  s    $ #ff. 	1 	1Cf}}

3+0#C(3#T**3==f===!'R%+0111,1()'7r;   markupr$   user_specified_encodingOptional[_Encoding]document_declared_encodingexclude_encodingsOptional[_Encodings]r=   DIterable[Tuple[str, Optional[_Encoding], Optional[_Encoding], bool]]c              #  8  K   t          |t                    r
|dddfV  dS g }|r|                    |           g }|r|                    |           t          |||d|          }|j        t          d          |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_encodingsis_htmlr   zPCould not convert input to Unicode, and html.parser will not accept bytestrings.)	
isinstancer(   rT   r   unicode_markupr   original_encodingdeclared_html_encodingr|   )r9   r   r   r   r   r   r   dammits           r:   prepare_markupz$HTMLParserTreeBuilder.prepare_markup  s      2 fc"" 	4u----F 57 " 	E
 %++,CDDD*,% 	> !!"<===%=)/
 
 
  ( 'b  
 %(-6	     r;   _parser_classtype[BeautifulSoupHTMLParser]r>   c                "   | j         \  }}t          |t                    sJ | j        J  || j        g|R i |}	 |                    |           |                                 n!# t          $ r}t          |          d}~ww xY w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   r7   )r9   r   r   r/   r1   parseres          r:   r   zHTMLParserTreeBuilder.feed  s    
 'f &#&&&&&
 y$$$ty:4:::6::	*KKLLNNNN 	* 	* 	* 'q)))		*
 /1+++s   )A' '
B1B  B)NN)r   r   r   r   r1   r   )NNN)
r   r$   r   r   r   r   r   r   r=   r   )r   r$   r   r   r=   r>   )r   r   r   __doc__r   r   r   
HTMLPARSERr   r   r   r   r   r6   r   r&   r   __classcell__)r   s   @r:   r   r   q  s          FID#T62H22225555 !%$$$$ 04268 8 8 8 8 8 8B 8<:>26F F F F FP Ul 1 1 1 1 1 1 1 1 1r;   )1r   
__future__r   __license____all__html.parserr   r   typingr   r   r	   r
   r   r   r   r   r   r   r   bs4.elementr   r   r   r   r   r   
bs4.dammitr   r   bs4.builderr   r   r   r   bs4.exceptionsr   bs4r    r!   bs4._typingr"   r#   r$   r   r(   rP   r&   r   r   r;   r:   <module>r      s   I I " " " " " "   # " " " " " 				                                         9 8 8 8 8 8 8 8            0 / / / / / !!!!!!++++++          
%tCH~sC&@$&FG p1 p1 p1 p1 p1j*@ p1 p1 p1f	T1 T1 T1 T1 T1O T1 T1 T1 T1 T1r;   