U
    
Ha8                     @   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   J/tmp/pip-unpacked-wheel-tx790h60/pip/_vendor/html5lib/treebuilders/base.py__init__   s    zNode.__init__c                 C   s:   d dd | j D }|r,d| j|f S d| j S d 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   s   t dS )z[Insert node as a child of the current node

        :arg node: the node to insert

        NNotImplementedErrorr   noder   r   r   appendChild9   s    zNode.appendChildNc                 C   s   t dS )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

        Nr(   )r   datainsertBeforer   r   r   
insertTextA   s    
zNode.insertTextc                 C   s   t dS )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

        Nr(   )r   r+   refNoder   r   r   r.   M   s    
zNode.insertBeforec                 C   s   t dS )zhRemove node from the children of the current node

        :arg node: the child node to remove

        Nr(   r*   r   r   r   removeChildY   s    zNode.removeChildc                 C   s    | j D ]}|| qg | _ dS )zMove 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   s   t dS )zReturn a shallow copy of the current node i.e. a node with the same
        name and attributes but with no parent or child nodes
        Nr(   r&   r   r   r   	cloneNoden   s    zNode.cloneNodec                 C   s   t dS )zFReturn true if the node has children or text, false otherwise
        Nr(   r&   r   r   r   
hasContentt   s    zNode.hasContent)N)__name__
__module____qualname____doc__r   r%   r'   r,   r/   r.   r1   r4   r5   r6   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rV| d d d D ]:}|t kr* qV| ||r>|d7 }|dkr| |  qVqt| | d S )Nr         )Marker
nodesEqualremover   append)r   r+   
equalCountelementr   r   r   rB   {   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   r@      s
    z#ActiveFormattingElements.nodesEqualN)r7   r8   r9   rB   r@   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   rJ      s    zTreeBuilder.resetc                 C   s   t |d}|s2t|tr$td |f}t|ts2tt| \}}t| jD ]>}|r^||kr^ dS |sr|j	|krr dS ||j	|kA rH dS qHdstd S )NrE   r
   TF)
hasattr
isinstancer   r	   tupleAssertionErrorlistElementsMapreversedrL   rE   )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s4|| jkr8d S |tkrl|| jkrl|dkrXd}ql|d8 }| j | }q8|d7 }| j | }| }| d|j|j|jd}|| j |< || j d krlqqld S )Nr=   r   r<   StartTag)typer   	namespacer-   )	rM   lenr?   rL   r5   insertElementr   ra   r   )r   ientryclonerD   r   r   r   #reconstructActiveFormattingElements   s.    


z/TreeBuilder.reconstructActiveFormattingElementsc                 C   s(   | j  }| j r$|tkr$| j  }q
d S N)rM   popr?   )r   re   r   r   r   clearActiveFormattingElements  s    
z)TreeBuilder.clearActiveFormattingElementsc                 C   s:   | j ddd D ]$}|tkr" q6q|j|kr|  S qdS )zCheck 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)rM   r?   r   )r   r   itemr   r   r   !elementInActiveFormattingElements  s    

z-TreeBuilder.elementInActiveFormattingElementsc                 C   s&   |  |}| j| | j| d S rh   )createElementrL   rB   rR   r,   )r   tokenrD   r   r   r   
insertRoot  s    
zTreeBuilder.insertRootc                 C   s6   |d }|d }|d }|  |||}| j| d S )Nr   publicIdsystemId)doctypeClassrR   r,   )r   rn   r   rp   rq   doctyper   r   r   insertDoctype   s
    zTreeBuilder.insertDoctypec                 C   s*   |d kr| j d }|| |d  d S )Nr<   r-   )rL   r,   commentClass)r   rn   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   ra   r-   )getrI   elementClassr   r   rn   r   ra   rD   r   r   r   rm   -  s
    
zTreeBuilder.createElementc                 C   s   | j S rh   )_insertFromTabler&   r   r   r   _getInsertFromTable5  s    zTreeBuilder._getInsertFromTablec                 C   s    || _ |r| j| _n| j| _dS )zsSwitch the function used to insert an element from the
        normal one to the misnested table one and back againN)rz   insertElementTablerc   insertElementNormal)r   r   r   r   r   _setInsertFromTable8  s    
zTreeBuilder._setInsertFromTablec                 C   sb   |d }t |tstd| |d| j}| ||}|d |_| jd | | j	| |S )Nr   zElement %s not unicodera   r-   r<   )
rT   r   rV   rw   rI   rx   r   rL   r,   rB   ry   r   r   r   r}   C  s    
zTreeBuilder.insertElementNormalc                 C   s`   |  |}| jd jtkr$| |S |  \}}|dkrD|| n||| | j| |S )z-Create an element and insert it into the treer<   N)	rm   rL   r   r   r}   getTableMisnestedNodePositionr,   r.   rB   )r   rn   rD   r   r.   r   r   r   r|   M  s    

zTreeBuilder.insertElementTablec                 C   sV   |dkr| j d }| jr.| jr:| j d jtkr:|| n|  \}}||| dS )zInsert text data.Nr<   )rL   rP   r   r   r/   r   )r   r-   r   r.   r   r   r   r/   ]  s    

zTreeBuilder.insertTextc                 C   st   d}d}d}| j ddd D ]}|jdkr|} q4q|rb|jrJ|j}|}ql| j | j |d  }n
| j d }||fS )zsGet the foster parent element, and sibling to insert before
        (or None) when inserting a misnested table nodeNr<   r   r=   r   )rL   r   r   index)r   	lastTablefosterParentr.   elmr   r   r   r   l  s     

z)TreeBuilder.getTableMisnestedNodePositionc                 C   s8   | j d j}|tdkr4||kr4| j   | | d S )Nr<   )dddtlir   r   prprt)rL   r   	frozensetri   generateImpliedEndTags)r   excluder   r   r   r   r     s    
z"TreeBuilder.generateImpliedEndTagsc                 C   s   | j S )zReturn the final tree)rR   r&   r   r   r   getDocument  s    zTreeBuilder.getDocumentc                 C   s   |   }| jd | |S )zReturn the final fragmentr   )fragmentClassrL   r4   )r   fragmentr   r   r   getFragment  s    zTreeBuilder.getFragmentc                 C   s   t dS )zSerialize the subtree of node in the format required by unit tests

        :arg node: the node from which to start serializing

        Nr(   r*   r   r   r   testSerializer  s    zTreeBuilder.testSerializer)N)N)N)N)r7   r8   r9   r:   rQ   rx   ru   rr   r   r   rJ   r^   rg   rj   rl   ro   rt   rv   rm   r{   r~   propertyrP   r}   r|   r/   r   r   r   r   r   r   r   r   r   rH      s6   
.
	




rH   )
__future__r   r   r   Zpip._vendor.sixr   	constantsr   r   r	   r?   r   rW   objectr   r   r;   rH   r   r   r   r   <module>   s0   



c