o
    Ëý°jå8  ã                   @   sò   d dl mZmZmZ d dlmZ ddlmZmZm	Z	 dZ
eeƒdfeee	d dfhB ƒdfeee	d d	fe	d d
fhB ƒdfee	d dfe	d dfgƒdfee	d dfe	d dfgƒdfdœZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )é    )Úabsolute_importÚdivisionÚunicode_literals)Ú	text_typeé   )ÚscopingElementsÚtableInsertModeElementsÚ
namespacesNFÚhtmlÚbuttonÚolÚulÚtableÚoptgroupÚoptionT)Nr   Úlistr   Úselectc                   @   sb   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zddd„Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zd
S )ÚNodezRepresents an item in the treec                 C   s(   || _ d| _d| _i | _g | _g | _dS )zRCreates a Node

        :arg name: The tag name associated with the node

        N)ÚnameÚparentÚvalueÚ
attributesÚ
childNodesÚ_flags)Úselfr   © r   ú™/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/pip/_vendor/html5lib/treebuilders/base.pyÚ__init__   s   
zNode.__init__c                 C   s6   d  dd„ | j ¡ D ƒ¡}|rd| j|f S d| j S )Nú c                 S   s   g | ]
\}}d ||f ‘qS )z%s="%s"r   )Ú.0r   r   r   r   r   Ú
<listcomp>.   s    ÿz Node.__str__.<locals>.<listcomp>z<%s %s>ú<%s>)Újoinr   Úitemsr   )r   ÚattributesStrr   r   r   Ú__str__-   s   
þ
zNode.__str__c                 C   s
   d| j  S )Nr!   )r   ©r   r   r   r   Ú__repr__6   s   
zNode.__repr__c                 C   ó   t ‚)z[Insert node as a child of the current node

        :arg node: the node to insert

        ©ÚNotImplementedError©r   Únoder   r   r   ÚappendChild9   ó   zNode.appendChildNc                 C   r(   )aB  Insert data as text in the current node, positioned before the
        start of node insertBefore or to the end of the node's text.

        :arg data: the data to insert

        :arg insertBefore: True if you want to insert the text before the node
            and False if you want to insert it after the node

        r)   )r   ÚdataÚinsertBeforer   r   r   Ú
insertTextA   ó   
zNode.insertTextc                 C   r(   )a  Insert node as a child of the current node, before refNode in the
        list of child nodes. Raises ValueError if refNode is not a child of
        the current node

        :arg node: the node to insert

        :arg refNode: the child node to insert the node before

        r)   )r   r,   ÚrefNoder   r   r   r0   M   r2   zNode.insertBeforec                 C   r(   )zhRemove node from the children of the current node

        :arg node: the child node to remove

        r)   r+   r   r   r   ÚremoveChildY   r.   zNode.removeChildc                 C   s    | j D ]}| |¡ qg | _ dS )zôMove all the children of the current node to newParent.
        This is needed so that trees that don't store text as nodes move the
        text in the correct way

        :arg newParent: the node to move all this node's children to

        N)r   r-   )r   Ú	newParentÚchildr   r   r   ÚreparentChildrena   s   
	
zNode.reparentChildrenc                 C   r(   )zŠReturn a shallow copy of the current node i.e. a node with the same
        name and attributes but with no parent or child nodes
        r)   r&   r   r   r   Ú	cloneNoden   s   zNode.cloneNodec                 C   r(   )zFReturn true if the node has children or text, false otherwise
        r)   r&   r   r   r   Ú
hasContentt   s   zNode.hasContent©N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r%   r'   r-   r1   r0   r4   r7   r8   r9   r   r   r   r   r      s    	
r   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚActiveFormattingElementsc                 C   sf   d}|t kr+| d d d… D ]}|t kr n|  ||¡r|d7 }|dkr*|  |¡  nqt | |¡ d S )Nr   éÿÿÿÿé   é   )ÚMarkerÚ
nodesEqualÚremover   Úappend)r   r,   Ú
equalCountÚelementr   r   r   rF   {   s   
þzActiveFormattingElements.appendc                 C   s$   |j |j ksdS |j|jksdS dS )NFT)Ú	nameTupler   )r   Únode1Únode2r   r   r   rD   ˆ   s
   z#ActiveFormattingElements.nodesEqualN)r;   r<   r=   rF   rD   r   r   r   r   r?   z   s    r?   c                   @   sÖ   e Zd ZdZdZdZdZdZdZdd„ Z	dd„ Z
d+dd„Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zd+dd„Zdd„ Zdd„ Zdd„ ZeeeƒZdd„ Zdd„ Zd+dd „Zd!d"„ Zd+d#d$„Zd%d&„ Zd'd(„ Zd)d*„ ZdS ),ÚTreeBuildera  Base treebuilder implementation

    * documentClass - the class to use for the bottommost node of a document
    * elementClass - the class to use for HTML Elements
    * commentClass - the class to use for comments
    * doctypeClass - the class to use for doctypes

    Nc                 C   s   |rd| _ nd| _ |  ¡  dS )zmCreate a TreeBuilder

        :arg namespaceHTMLElements: whether or not to namespace HTML elements

        zhttp://www.w3.org/1999/xhtmlN)ÚdefaultNamespaceÚreset)r   ÚnamespaceHTMLElementsr   r   r   r   ¬   s   zTreeBuilder.__init__c                 C   s.   g | _ tƒ | _d | _d | _d| _|  ¡ | _d S )NF)ÚopenElementsr?   ÚactiveFormattingElementsÚheadPointerÚformPointerÚinsertFromTableÚdocumentClassÚdocumentr&   r   r   r   rN   ¸   s   zTreeBuilder.resetc                 C   sŒ   t |dƒ}|st|tƒrtd |f}t|tƒsJ ‚t| \}}t| jƒD ]}|r/||kr/ dS |s9|j|kr9 dS ||j|v A rC dS q$J ‚)NrI   r
   TF)	ÚhasattrÚ
isinstancer   r	   ÚtupleÚlistElementsMapÚreversedrP   rI   )r   ÚtargetÚvariantÚ	exactNodeÚlistElementsÚinvertr,   r   r   r   ÚelementInScopeÄ   s   

ÿzTreeBuilder.elementInScopec                 C   sÐ   | j sd S t| j ƒd }| j | }|tks|| jv rd S |tkr>|| jvr>|dkr,d}n|d8 }| j | }|tkr>|| jvs%	 |d7 }| j | }| ¡ }|  d|j|j|jdœ¡}|| j |< || j d krgd S q?)NrA   r   r@   TÚStartTag)Útyper   Ú	namespacer/   )	rQ   ÚlenrC   rP   r8   ÚinsertElementr   rd   r   )r   ÚiÚentryÚclonerH   r   r   r   Ú#reconstructActiveFormattingElementsÚ   s4   

ù	
ý
íz/TreeBuilder.reconstructActiveFormattingElementsc                 C   s@   | j  ¡ }| j r|tkr| j  ¡ }| j r|tksd S d S d S d S r:   )rQ   ÚpoprC   )r   rh   r   r   r   ÚclearActiveFormattingElements  s   

ÿz)TreeBuilder.clearActiveFormattingElementsc                 C   s:   | j ddd… D ]}|tkr dS |j|kr|  S qdS )zšCheck if an element exists between the end of the active
        formatting elements and the last marker. If it does, return it, else
        return falseNr@   F)rQ   rC   r   )r   r   Úitemr   r   r   Ú!elementInActiveFormattingElements  s   
þÿz-TreeBuilder.elementInActiveFormattingElementsc                 C   s&   |   |¡}| j |¡ | j |¡ d S r:   )ÚcreateElementrP   rF   rV   r-   )r   ÚtokenrH   r   r   r   Ú
insertRoot  s   
zTreeBuilder.insertRootc                 C   s6   |d }|d }|d }|   |||¡}| j |¡ d S )Nr   ÚpublicIdÚsystemId)ÚdoctypeClassrV   r-   )r   rp   r   rr   rs   Údoctyper   r   r   ÚinsertDoctype   s
   zTreeBuilder.insertDoctypec                 C   s*   |d u r	| j d }| |  |d ¡¡ d S )Nr@   r/   )rP   r-   ÚcommentClass)r   rp   r   r   r   r   ÚinsertComment(  s   
zTreeBuilder.insertCommentc                 C   s0   |d }|  d| j¡}|  ||¡}|d |_|S )z.Create an element but don't insert it anywherer   rd   r/   )ÚgetrM   ÚelementClassr   ©r   rp   r   rd   rH   r   r   r   ro   -  s
   
zTreeBuilder.createElementc                 C   ó   | j S r:   )Ú_insertFromTabler&   r   r   r   Ú_getInsertFromTable5  s   zTreeBuilder._getInsertFromTablec                 C   s"   || _ |r| j| _dS | j| _dS )zsSwitch the function used to insert an element from the
        normal one to the misnested table one and back againN)r}   ÚinsertElementTablerf   ÚinsertElementNormal)r   r   r   r   r   Ú_setInsertFromTable8  s   zTreeBuilder._setInsertFromTablec                 C   sb   |d }t |tƒsJ d| ƒ‚| d| j¡}|  ||¡}|d |_| jd  |¡ | j |¡ |S )Nr   zElement %s not unicoderd   r/   r@   )	rX   r   ry   rM   rz   r   rP   r-   rF   r{   r   r   r   r€   C  s   
zTreeBuilder.insertElementNormalc                 C   s`   |   |¡}| jd jtvr|  |¡S |  ¡ \}}|du r"| |¡ n| ||¡ | j |¡ |S )z-Create an element and insert it into the treer@   N)	ro   rP   r   r   r€   ÚgetTableMisnestedNodePositionr-   r0   rF   )r   rp   rH   r   r0   r   r   r   r   M  s   

zTreeBuilder.insertElementTablec                 C   sX   |du r	| j d }| jr| jr| j d jtvr| |¡ dS |  ¡ \}}| ||¡ dS )zInsert text data.Nr@   )rP   rT   r   r   r1   r‚   )r   r/   r   r0   r   r   r   r1   ]  s   

ÿzTreeBuilder.insertTextc                 C   s€   d}d}d}| j ddd… D ]}|jdkr|} nq|r7|jr(|j}|}||fS | j | j  |¡d  }||fS | j d }||fS )zsGet the foster parent element, and sibling to insert before
        (or None) when inserting a misnested table nodeNr@   r   rA   r   )rP   r   r   Úindex)r   Ú	lastTableÚfosterParentr0   Úelmr   r   r   r‚   l  s&   
þüÿ
ÿz)TreeBuilder.getTableMisnestedNodePositionc                 C   s@   | j d j}|tdƒv r||kr| j  ¡  |  |¡ d S d S d S )Nr@   )ÚddÚdtÚlir   r   ÚpÚrpÚrt)rP   r   Ú	frozensetrk   ÚgenerateImpliedEndTags)r   Úexcluder   r   r   r   rŽ   †  s   
ûz"TreeBuilder.generateImpliedEndTagsc                 C   r|   )zReturn the final tree)rV   r&   r   r   r   ÚgetDocument�  s   zTreeBuilder.getDocumentc                 C   s   |   ¡ }| jd  |¡ |S )zReturn the final fragmentr   )ÚfragmentClassrP   r7   )r   Úfragmentr   r   r   ÚgetFragment”  s   zTreeBuilder.getFragmentc                 C   r(   )z‰Serialize the subtree of node in the format required by unit tests

        :arg node: the node from which to start serializing

        r)   r+   r   r   r   ÚtestSerializer›  r.   zTreeBuilder.testSerializerr:   )r;   r<   r=   r>   rU   rz   rw   rt   r‘   r   rN   ra   rj   rl   rn   rq   rv   rx   ro   r~   r�   ÚpropertyrT   r€   r   r1   r‚   rŽ   r�   r“   r”   r   r   r   r   rL   ’   s8    
.

	



rL   )Ú
__future__r   r   r   Úpip._vendor.sixr   Ú	constantsr   r   r	   rC   r�   rZ   Úobjectr   r   r?   rL   r   r   r   r   Ú<module>   s2    

ÿÿ
ÿÿ
ÿÿùc