o
    í6Wj7\  ã                   @   sJ  d Z dgZddlmZmZmZmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZmZmZmZmZmZ ddlZddlmZmZmZmZmZ ddlmZmZmZ ddl Z dd	l!m"Z" dd
lm#Z#m$Z$m%Z%m&Z& e	rkddl'm(Z( ddl)m*Z+ G dd„ deƒZ,G dd„ de+j-ƒZ.G dd„ de/ƒZ0G dd„ de+j1ƒZ2G dd„ de2ƒZ3G dd„ de2ƒZ4dS )ÚMITÚHTML5TreeBuilderé    )	ÚAnyÚcastÚDictÚIterableÚOptionalÚSequenceÚTYPE_CHECKINGÚTupleÚUnion)Ú	TypeAlias)Ú_AttributeValueÚ_AttributeValuesÚ	_EncodingÚ
_EncodingsÚ_NamespaceURLÚ
_RawMarkupN)ÚDetectsXMLParsedAsHTMLÚ
PERMISSIVEÚHTMLÚHTML_5ÚHTMLTreeBuilder)ÚNamespacedAttributeÚPageElementÚnonwhitespace_re)Ú
namespaces)ÚCommentÚDoctypeÚNavigableStringÚTag©ÚBeautifulSoup)Úbasec                   @   sÖ   e Zd ZU dZdZeed< eeee	gZ
ee ed< dZeed< ded< ee ed	< 	
	
	
dded	ee dee dee deeeee ee ef  f
dd„Zdedd
fdd„Zdeddfdd„Zdedefdd„Zd
S )r   aj  Use `html5lib <https://github.com/html5lib/html5lib-python>`_ to
    build a tree.

    Note that `HTML5TreeBuilder` does not support some common HTML
    `TreeBuilder` features. Some of these features could theoretically
    be implemented, but at the very least it's quite difficult,
    because html5lib moves the parse tree around as it's being built.

    Specifically:

    * This `TreeBuilder` doesn't use different subclasses of
      `NavigableString` (e.g. `Script`) based on the name of the tag
      in which the string was found.
    * You can't use a `SoupStrainer` to parse only part of a document.
    Úhtml5libÚNAMEÚfeaturesTÚTRACKS_LINE_NUMBERSÚTreeBuilderForHtml5libÚunderlying_builderÚuser_specified_encodingNÚmarkupÚdocument_declared_encodingÚexclude_encodingsÚreturnc                 c   sb   � || _ |df|dffD ]\}}|r tjd|› d|› d�dd� qtj|dd� |d d dfV  d S )	Nr,   r-   zYou provided a value for z0, but the html5lib tree builder doesn't support Ú.é   ©Ú
stacklevelF)r*   ÚwarningsÚwarnr   Úwarn_if_markup_looks_like_xml)Úselfr+   r*   r,   r-   ÚvariableÚname© r9   úd/home/esfera/Documents/content_generation/venv/lib/python3.10/site-packages/bs4/builder/_html5lib.pyÚprepare_markupW   s   €þþ€zHTML5TreeBuilder.prepare_markupc                 C   s¬   | j dur| j jdurtjddd� tj| jd�}| jdus J ‚|| j_t	ƒ }t
|tƒs1| j|d< |j|fi |¤Ž}t
|tƒrCd|_n|jjjd }|j}||_d| j_dS )z‰Run some incoming markup through some parsing process,
        populating the `BeautifulSoup` object in `HTML5TreeBuilder.soup`.
        Nz‚You provided a value for parse_only, but the html5lib tree builder doesn't support parse_only. The entire document will be parsed.é   r1   )ÚtreeÚoverride_encodingr   )ÚsoupÚ
parse_onlyr3   r4   r$   Ú
HTMLParserÚcreate_treebuilderr)   ÚparserÚdictÚ
isinstanceÚstrr*   ÚparseÚoriginal_encodingÚ	tokenizerÚstreamÚcharEncodingr8   )r6   r+   rC   Úextra_kwargsÚdocrH   r9   r9   r:   Úfeedu   s$   þ


zHTML5TreeBuilder.feedÚnamespaceHTMLElementsc                 C   s   t || j| jd�| _| jS )zÈCalled by html5lib to instantiate the kind of class it
        calls a 'TreeBuilder'.

        :param namespaceHTMLElements: Whether or not to namespace HTML elements.

        :meta private:
        )Ústore_line_numbers)r(   r?   rP   r)   )r6   rO   r9   r9   r:   rB   š   s   

ÿz#HTML5TreeBuilder.create_treebuilderÚfragmentc                 C   s   d| S )zSee `TreeBuilder`.z)<html><head></head><body>%s</body></html>r9   )r6   rQ   r9   r9   r:   Útest_fragment_to_document©   s   z*HTML5TreeBuilder.test_fragment_to_document)NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r%   rF   Ú__annotations__r   r   r   r&   r   r'   Úboolr   r   r   r   r   r;   rN   rB   rR   r9   r9   r9   r:   r   ;   s8   
 ûþýüû
ú%ÿ
þc                	       sÎ   e Zd ZU ded< eej ed< 		d%deded dedef‡ fd	d
„Z	d&dd„Z
deeef ddfdd„Zdededdfdd„Zdeddfdd„Zd&dd„Zd&dd„Zd'dd „Zd(d!d"„Zd'd#d$„Z‡  ZS ))r(   r"   r?   rC   NTrO   rP   Úkwargsc                    s^   |r|| _ ntjdtdd� ddlm} |	d	d|i|¤Ž| _ tt| ƒ |¡ d | _	|| _
d S )
NaK  The optionality of the 'soup' argument to the TreeBuilderForHtml5lib constructor is deprecated as of Beautiful Soup 4.13.0: 'soup' is now required. If you can't pass in a BeautifulSoup object here, or you get this warning and it seems mysterious to you, please contact the Beautiful Soup developer team for possible un-deprecation.é   r1   r   r!   Ú úhtml.parserrP   )r[   r\   )r?   r3   r4   ÚDeprecationWarningÚbs4r"   Úsuperr(   Ú__init__rC   rP   )r6   rO   r?   rP   rY   r"   ©Ú	__class__r9   r:   r`   ²   s$   ýÿÿÿ
zTreeBuilderForHtml5lib.__init__r.   ÚElementc                 C   s   | j  ¡  t| j | j d ƒS ©N)r?   Úresetrc   ©r6   r9   r9   r:   ÚdocumentClassÓ   s   
z$TreeBuilderForHtml5lib.documentClassÚtokenc                 C   sP   t t|d ƒ}t tt |d ƒ}t tt |d ƒ}t |||¡}| j |¡ d S )Nr8   ÚpublicIdÚsystemId)r   rF   r   r   Úfor_name_and_idsr?   Úobject_was_parsed)r6   rh   r8   ri   rj   Údoctyper9   r9   r:   ÚinsertDoctype×   s
   z$TreeBuilderForHtml5lib.insertDoctyper8   Ú	namespacec                 C   s`   d }d }| j d ur| jr| j jj ¡ \}}|d usJ ‚|d }| jj||||d�}t|| j|ƒS )Né   )Ú
sourcelineÚ	sourcepos)rC   rP   rI   rJ   Úpositionr?   Únew_tagrc   )r6   r8   ro   rq   rr   Útagr9   r9   r:   ÚelementClassß   s   ÿz#TreeBuilderForHtml5lib.elementClassÚdataÚTextNodec                 C   s   t t|ƒ| jƒS rd   )rx   r   r?   )r6   rw   r9   r9   r:   ÚcommentClassï   s   z#TreeBuilderForHtml5lib.commentClassc                 C   ó   t ƒ ‚)zôThis is only used by html5lib HTMLParser.parseFragment(),
        which is never used by Beautiful Soup, only by the html5lib
        unit tests. Since we don't currently hook into those tests,
        the implementation is left blank.
        ©ÚNotImplementedErrorrf   r9   r9   r:   ÚfragmentClassò   s   z$TreeBuilderForHtml5lib.fragmentClassc                 C   rz   ©z˜This is only used by the html5lib unit tests. Since we
        don't currently hook into those tests, the implementation is
        left blank.
        r{   rf   r9   r9   r:   ÚgetFragmentú   ó   z"TreeBuilderForHtml5lib.getFragmentÚnodec                 C   s   | j  |j¡ d S rd   )r?   ÚappendÚelement©r6   r�   r9   r9   r:   ÚappendChild  s   z"TreeBuilderForHtml5lib.appendChildc                 C   s   | j S rd   )r?   rf   r9   r9   r:   ÚgetDocument
  ó   z"TreeBuilderForHtml5lib.getDocumentc                 C   rz   r~   r{   r„   r9   r9   r:   ÚtestSerializer  r€   z%TreeBuilderForHtml5lib.testSerializer)NT)r.   rc   ©r�   rc   r.   N)r.   r"   )rS   rT   rU   rW   r   r$   rA   rX   r   r`   rg   r   rF   rn   rv   ry   r}   r   r…   r†   rˆ   Ú__classcell__r9   r9   ra   r:   r(   ®   s.   
 üþýüû
!



	r(   c                   @   s¾   e Zd ZU dZeed< eed< defdd„Zdee	e
ef  fdd„Zd	e
d
eddfdd„Zdee	e
ef  fdd„Zdee
 fdd„Zdefdd„Zd	e
defdd„Zd	e
defdd„ZdS )ÚAttrListz@Represents a Tag's attributes in a way compatible with html5lib.rƒ   Úattrsc                 C   s   || _ t| j jƒ| _d S rd   )rƒ   rD   rŒ   )r6   rƒ   r9   r9   r:   r`     s   zAttrList.__init__r.   c                 C   s   t | j ¡ ƒ ¡ S rd   )ÚlistrŒ   ÚitemsÚ__iter__rf   r9   r9   r:   r�     ó   zAttrList.__iter__r8   ÚvalueNc                 C   st   | j jpi }|| dg ¡v s| j j|v r3|| | j jg ¡v r3t|tƒs3t|tƒs*J ‚| j  t 	|¡¡}|| j |< d S )NÚ*)
rƒ   Úcdata_list_attributesÚgetr8   rE   r�   rF   Úattribute_value_list_classr   Úfindall)r6   r8   r‘   Ú	list_attrr9   r9   r:   Ú__setitem__"  s   
ÿzAttrList.__setitem__c                 C   ó   t | j ¡ ƒS rd   )r�   rŒ   rŽ   rf   r9   r9   r:   rŽ   3  ó   zAttrList.itemsc                 C   r™   rd   ©r�   rŒ   Úkeysrf   r9   r9   r:   rœ   6  rš   zAttrList.keysc                 C   s
   t | jƒS rd   )ÚlenrŒ   rf   r9   r9   r:   Ú__len__9  ó   
zAttrList.__len__c                 C   s
   | j | S rd   )rŒ   ©r6   r8   r9   r9   r:   Ú__getitem__<  rŸ   zAttrList.__getitem__c                 C   s   |t | j ¡ ƒv S rd   r›   r    r9   r9   r:   Ú__contains__?  r�   zAttrList.__contains__)rS   rT   rU   rV   r    rW   r   r`   r   r   rF   r   r�   r˜   rŽ   rœ   Úintrž   r¡   rX   r¢   r9   r9   r9   r:   r‹     s   
 r‹   c                   @   sn   e Zd ZU ee ed< ee ed< ded< ee ed< ede	fdd„ƒZ
edefd	d
„ƒZdejfdd„ZdS )ÚBeautifulSoupNoderu   Ústringr"   r?   ro   r.   c                 C   s<   | j d us| jd usJ ‚| j d ur| j S | jd usJ ‚| jS rd   )ru   r¥   rf   r9   r9   r:   rƒ   J  s
   
zBeautifulSoupNode.elementc                 C   rz   )zÕReturn the html5lib constant corresponding to the type of
        the underlying DOM object.

        NOTE: This property is only accessed by the html5lib test
        suite, not by Beautiful Soup proper.
        r{   rf   r9   r9   r:   ÚnodeTypeS  s   zBeautifulSoupNode.nodeTypec                 C   rz   rd   r{   rf   r9   r9   r:   Ú	cloneNode_  r‡   zBeautifulSoupNode.cloneNodeN)rS   rT   rU   r   r    rW   r   r   Úpropertyr   rƒ   r£   r¦   Útreebuilder_baseÚNoder§   r9   r9   r9   r:   r¤   C  s   
 r¤   c                   @   s$  e Zd ZU ee ed< dedddee fdd„Zd&dd„Zd	e	fdd„Z
eeeeef f Zeed< eeef Zeed< dee d	d
fdd„Zee
eƒZ	
d'deded d	d
fdd„Z							
d(dd„Zd)dd„Zd*dd„Zd	efd d!„Zd	ejfd"d#„Zd	eee ef fd$d%„ZeeƒZd
S )+rc   ro   rƒ   r?   r"   c                 C   s,   || _ d | _|| _|| _tj | |j¡ d S rd   )ru   r¥   r?   ro   r©   rª   r`   r8   )r6   rƒ   r?   ro   r9   r9   r:   r`   f  s
   zElement.__init__r�   r¤   r.   Nc                 C   s  d }t |jƒtu r|j }}n|j}| |_|d ur(|jd ur(t|tƒs(|j ¡  |d urX| jd urX| jj	rXt | jj	d ƒtu rX| jj	d }| j
 || ¡}| |¡ || j
_d S t|tƒrc| j
 |¡}| jd urs| jj	rs| j d¡}n| jjd ur| j
 ¡ }n| j}| j
j|| j|d� d S )NéÿÿÿÿF)ÚparentÚmost_recent_element)Útyper¥   r   rƒ   r¬   rE   rF   Úextractru   Úcontentsr?   Ú
new_stringÚreplace_withÚ_most_recent_elementÚ_last_descendantÚnext_elementrl   )r6   r�   Ústring_childÚchildÚold_elementÚnew_elementr­   r9   r9   r:   r…   o  s6   
ÿ



ÿzElement.appendChildc                 C   s   | j d usJ ‚t| j ƒS rd   )ru   r‹   rf   r9   r9   r:   ÚgetAttributes¤  s   
zElement.getAttributesÚ_Html5libAttributeNameÚ_Html5libAttributesÚ
attributesc                 C   s°   | j d usJ ‚|d urTt|ƒdkrVt| ¡ ƒD ]\}}t|tƒr+t|Ž }||= |||< qtt|ƒ}| j	j
 | j|¡ t| ¡ ƒD ]	\}}|| j |< q@| j	j
 | j ¡ d S d S d S ©Nr   )ru   r�   r�   rŽ   rE   Útupler   r   r   r?   ÚbuilderÚ$_replace_cdata_list_attribute_valuesr8   Úset_up_substitutions)r6   r½   r8   r‘   Únew_nameÚnormalized_attributesÚvalue_or_valuesr9   r9   r:   ÚsetAttributes¯  s    
€
ÿãzElement.setAttributesrw   ÚinsertBeforec                 C   s6   t | j |¡| jƒ}|r|  ||¡ d S |  |¡ d S rd   )rx   r?   r±   rÇ   r…   )r6   rw   rÇ   Útextr9   r9   r:   Ú
insertTextÒ  s   zElement.insertTextÚrefNodec                 C   s¤   | j d usJ ‚| j  |j¡}t|jƒtu rE| j jrEt| j j|d  ƒtu rE| j j|d  }t|ƒtu s5J ‚| j ||j ¡}| |¡ d S | j  	||j¡ | |_
d S )Nrp   )ru   Úindexrƒ   r®   r   r°   r?   r±   r²   Úinsertr¬   )r6   r�   rÊ   rË   Úold_nodeÚnew_strr9   r9   r:   rÇ   Û  s   ÿ
zElement.insertBeforec                 C   s   |j  ¡  d S rd   )rƒ   r¯   r„   r9   r9   r:   ÚremoveChildî  rš   zElement.removeChildÚ	newParentc                 C   s*  | j }|dus	J ‚|j }|dusJ ‚|j}| dd¡}t|jƒdkr1|dus(J ‚|jd }|j}nd}|j}|j}t|ƒdkr|d }	|durK||	_n||	_||	_|durY|	|_n|	|_|durc|	|_|d jddd�}
|
dusrJ ‚||
_|dur||
|_d|
_|D ]}||_|j 	|¡ q�g |_||_dS )z1Move all of this tag's children into another tag.NFr   r«   T)Úis_initializedÚaccept_self)
ru   Únext_siblingr´   r�   r°   rµ   Úprevious_elementÚprevious_siblingr¬   r‚   )r6   rÐ   rƒ   Únew_parent_elementÚfinal_next_elementÚnew_parents_last_descendantÚnew_parents_last_childÚ(new_parents_last_descendant_next_elementÚ	to_appendÚfirst_childÚlast_childs_last_descendantr·   r9   r9   r:   ÚreparentChildrenñ  sP   
ÿÿÿÿ
zElement.reparentChildrenc                 C   s   | j d u pt| j jƒdkS r¾   )ru   r�   r°   rf   r9   r9   r:   Ú
hasContentG  s   zElement.hasContentc                 C   sP   | j d usJ ‚| j | j j| j¡}t|| j| jƒ}| jD ]	\}}||j|< q|S rd   )ru   r?   rt   r8   ro   rc   r½   )r6   ru   r�   Úkeyr‘   r9   r9   r:   r§   L  s   zElement.cloneNodec                 C   s$   | j d u rtd | jfS | j | jfS )NÚhtml)ro   r   r8   rf   r9   r9   r:   ÚgetNameTupleT  s   
zElement.getNameTuple)r�   r¤   r.   Nrd   )r�   r¤   rÊ   r¤   r.   Nr‰   )rÐ   rc   r.   N) rS   rT   rU   r   r   rW   r    r`   r…   r‹   rº   r   rF   r   r»   r   r   r¼   rÆ   r¨   r½   rÉ   rÇ   rÏ   rÞ   rX   rß   r©   rª   r§   râ   Ú	nameTupler9   r9   r9   r:   rc   c  sH   
 ÿÿ
ÿ
	5
!ÿÿÿ
þ	ÿÿ
þ

Vrc   c                   @   s   e Zd Zdeddfdd„ZdS )rx   rƒ   r?   r"   c                 C   s$   t j | d ¡ d | _|| _|| _d S rd   )r©   rª   r`   ru   r¥   r?   )r6   rƒ   r?   r9   r9   r:   r`   _  s   
zTextNode.__init__N)rS   rT   rU   r   r`   r9   r9   r9   r:   rx   ]  s    rx   )5Ú__license__Ú__all__Útypingr   r   r   r   r   r	   r
   r   r   Útyping_extensionsr   Úbs4._typingr   r   r   r   r   r   r3   Úbs4.builderr   r   r   r   r   Úbs4.elementr   r   r   r$   Úhtml5lib.constantsr   r   r   r   r    r^   r"   Úhtml5lib.treebuildersr#   r©   r   ÚTreeBuilderr(   Úobjectr‹   rª   r¤   rc   rx   r9   r9   r9   r:   Ú<module>   s,   ÿ, 	sg.  {