3
ûâdeå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   úE/tmp/pip-build-red6nood/pip/pip/_vendor/html5lib/treebuilders/base.pyÚ__init__   s    zNode.__init__c             C   s:   dj dd„ | j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>z<%s>)Újoinr   Úitemsr   )r   ÚattributesStrr   r   r   Ú__str__-   s
    
zNode.__str__c             C   s
   d| j  S )Nz<%s>)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

        N)r&   )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

        N)r&   )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

        N)r&   )r   r'   r   r   r   ÚremoveChildY   s    zNode.removeChildc             C   s$   x| j D ]}|j|ƒ qW 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   s   t ‚dS )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
        N)r&   )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
        N)r&   )r   r   r   r   Ú
hasContentt   s    zNode.hasContent)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r$   r%   r(   r+   r*   r-   r0   r1   r2   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rVxH| d d d… D ]6}|t kr*P | j||ƒr>|d7 }|dkr| j|ƒ P qW tj| |ƒ d S )Nr   é   é   éÿÿÿÿ)ÚMarkerÚ
nodesEqualÚremover   Úappend)r   r'   Ú
equalCountÚelementr   r   r   r>   {   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)r3   r4   r5   r>   r<   r   r   r   r   r7   z   s   r7   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| _ | jƒ  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| _| jƒ | _d S )NF)ÚopenElementsr7   ÚactiveFormattingElementsÚheadPointerÚformPointerÚinsertFromTableÚdocumentClassÚdocument)r   r   r   r   rF   ¸   s    zTreeBuilder.resetc             C   s”   t |dƒ}|s2t|tƒr$td |f}t|tƒs2t‚t| \}}xHt| jƒD ]:}|r^||kr^dS | rr|j	|krrdS ||j	|kA rJdS qJW ds�t‚d S )NrA   r
   TF)
ÚhasattrÚ
isinstancer   r	   ÚtupleÚAssertionErrorÚlistElementsMapÚreversedrH   rA   )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 x6|tkrn|| jkrn|dkrZd}P |d8 }| j | }q:W xR|d7 }| j | }|jƒ }| jd|j|j|jdœƒ}|| j |< || j d krrP qrW d S )Nr8   r   ÚStartTag)Útyper   Ú	namespacer)   r:   r:   )	rI   Úlenr;   rH   r1   ÚinsertElementr   r]   r   )r   ÚiÚentryÚcloner@   r   r   r   Ú#reconstructActiveFormattingElementsÚ   s.    


z/TreeBuilder.reconstructActiveFormattingElementsc             C   s,   | j jƒ }x| j r&|tkr&| j jƒ }qW d S )N)rI   Úpopr;   )r   ra   r   r   r   ÚclearActiveFormattingElements  s    
z)TreeBuilder.clearActiveFormattingElementsc             C   s8   x2| j ddd… D ]}|tkr"P q|j|kr|S qW 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 falseNr8   Fr:   )rI   r;   r   )r   r   Úitemr   r   r   Ú!elementInActiveFormattingElements  s    
z-TreeBuilder.elementInActiveFormattingElementsc             C   s&   | j |ƒ}| jj|ƒ | jj|ƒ d S )N)ÚcreateElementrH   r>   rN   r(   )r   Útokenr@   r   r   r   Ú
insertRoot  s    
zTreeBuilder.insertRootc             C   s6   |d }|d }|d }| j |||ƒ}| jj|ƒ d S )Nr   ÚpublicIdÚsystemId)ÚdoctypeClassrN   r(   )r   ri   r   rk   rl   Údoctyper   r   r   ÚinsertDoctype   s
    zTreeBuilder.insertDoctypec             C   s*   |d kr| j d }|j| j|d ƒƒ d S )Nr8   r)   r:   )rH   r(   ÚcommentClass)r   ri   r   r   r   r   ÚinsertComment(  s    
zTreeBuilder.insertCommentc             C   s0   |d }|j d| jƒ}| j||ƒ}|d |_|S )z.Create an element but don't insert it anywherer   r]   r)   )ÚgetrE   ÚelementClassr   )r   ri   r   r]   r@   r   r   r   rh   -  s
    
zTreeBuilder.createElementc             C   s   | j S )N)Ú_insertFromTable)r   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)rt   ÚinsertElementTabler_   ÚinsertElementNormal)r   r   r   r   r   Ú_setInsertFromTable8  s    
zTreeBuilder._setInsertFromTablec             C   sb   |d }t |tƒstd| ƒ‚|jd| jƒ}| j||ƒ}|d |_| jd j|ƒ | jj	|ƒ |S )Nr   zElement %s not unicoder]   r)   r8   r:   )
rP   r   rR   rr   rE   rs   r   rH   r(   r>   )r   ri   r   r]   r@   r   r   r   rw   C  s    
zTreeBuilder.insertElementNormalc             C   s`   | j |ƒ}| jd jtkr$| j|ƒS | jƒ \}}|dkrD|j|ƒ n|j||ƒ | jj|ƒ |S )z-Create an element and insert it into the treer8   Nr:   )	rh   rH   r   r   rw   ÚgetTableMisnestedNodePositionr(   r*   r>   )r   ri   r@   r   r*   r   r   r   rv   M  s    

zTreeBuilder.insertElementTablec             C   sX   |dkr| j d }| j s0| jr<| j d jtkr<|j|ƒ n| jƒ \}}|j||ƒ dS )zInsert text data.Nr8   r:   r:   )rH   rL   r   r   r+   ry   )r   r)   r   r*   r   r   r   r+   ]  s    

zTreeBuilder.insertTextc             C   sv   d}d}d}x(| j ddd… D ]}|jdkr|}P qW |rd|jrL|j}|}qn| j | 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 nodeNr8   r   r   r:   )rH   r   r   Úindex)r   Ú	lastTableÚfosterParentr*   Úelmr   r   r   ry   l  s    

z)TreeBuilder.getTableMisnestedNodePositionc          
   C   s8   | j d
 j}|tdƒkr4||kr4| j jƒ  | j|ƒ d S )Nr8   ÚddÚdtÚlir   r   ÚpÚrpÚrtr:   )r~   r   r€   r   r   r�   r‚   rƒ   )rH   r   Ú	frozensetrd   ÚgenerateImpliedEndTags)r   Úexcluder   r   r   r   r…   †  s
    
z"TreeBuilder.generateImpliedEndTagsc             C   s   | j S )zReturn the final tree)rN   )r   r   r   r   ÚgetDocument�  s    zTreeBuilder.getDocumentc             C   s   | j ƒ }| jd j|ƒ |S )zReturn the final fragmentr   )ÚfragmentClassrH   r0   )r   Úfragmentr   r   r   ÚgetFragment”  s    zTreeBuilder.getFragmentc             C   s   t ‚dS )z‰Serialize the subtree of node in the format required by unit tests

        :arg node: the node from which to start serializing

        N)r&   )r   r'   r   r   r   ÚtestSerializer›  s    zTreeBuilder.testSerializer)N)N)N)N)r3   r4   r5   r6   rM   rs   rp   rm   rˆ   r   rF   rZ   rc   re   rg   rj   ro   rq   rh   ru   rx   ÚpropertyrL   rw   rv   r+   ry   r…   r‡   rŠ   r‹   r   r   r   r   rD   ’   s6   
.
	




rD   )Ú
__future__r   r   r   Zpip._vendor.sixr   Ú	constantsr   r   r	   r;   r„   rS   Úobjectr   r   r7   rD   r   r   r   r   Ú<module>   s   
c