a
    [Xh(>                     @   s  d Z ddlmZ ddlmZ ddlmZmZmZ dZed Z	e
edfe
ee	d	fhB dfe
ee	d
fe	dfhB dfe
e	dfe	dfgdfe
e	dfe	dfgdfdZe
dZG dd deZG dd dZG dd deZG dd deZG dd deZG dd deZG dd dZdS ) Tree builder.    )copy)ElementTree   )
namespacesscoping_elementstable_insert_mode_elementsNhtmlFbuttonolultableoptgroupoptionT)Nr
   listr   select)dddtlir   r   prprtc                   @   s   e Zd Zdd ZdS )ActiveFormattingElementsc                 C   sv   d}|t urf| ddd D ]J}|t u r* qf|j|jko@|j|jk}|rN|d7 }|dkr| |  qfqt| | dS )z#Append node to the end of the list.r   Nr      )Marker
name_tuple
attributesremover   append)selfnodeZequal_countelementZnodes_equal r#   Z/var/www/viveiro_mudafortebrasil/venv/lib/python3.9/site-packages/tinyhtml5/treebuilder.pyr      s    

zActiveFormattingElements.appendN)__name__
__module____qualname__r   r#   r#   r#   r$   r      s   r   c                   @   s   e Z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
ee	e
Zdd Zdd Zdd Zdd ZdddZdd Zdd ZdS )ElementNc                 C   sT   || _ || _t| ||| _|d u r6t| j f| _n| j| j f| _g | _d | _	d S N)
name	namespacer   r(   _get_etree_tag_element_htmlr   	_childrenparentr    r*   r+   r#   r#   r$   __init__-   s    zElement.__init__c                 C   s   |d u r|S d| d| S )N{}r#   r1   r#   r#   r$   r,   :   s    zElement._get_etree_tagc                 C   s   | j jS r)   )r-   attribr    r#   r#   r$   _get_attributes=   s    zElement._get_attributesc                 C   sX   | j j}|  |rT| D ]6\}}t|trFd|d  d|d  n|}|||< qd S )Nr3      r4   r   )r-   r5   clearitems
isinstancetuple)r    r   Zelement_attributeskeyvaluer*   r#   r#   r$   _set_attributes@   s    &zElement._set_attributesc                 C   s   | j S r)   )r/   r6   r#   r#   r$   _get_childrenM   s    zElement._get_childrenc                 C   s*   | j d d = g | _|D ]}| | qd S r)   )r-   r/   Zinsert_child)r    r>   r"   r#   r#   r$   _set_childrenP   s    zElement._set_childrenc                 C   s   t | jjpt| jS )z>Return True if the node has children or text, False otherwise.)boolr-   textlenr6   r#   r#   r$   has_contentZ   s    zElement.has_contentc                 C   s$   | j | | j|j | |_dS )z+Insert node as a child of the current node.N)r/   r   r-   r0   r    r!   r#   r#   r$   append_child^   s    zElement.append_childc                 C   s,   t | j|j}| j||j | |_dS )zInsert node as a child of the current node, before reference.

        Raise ValueError if reference is not a child of the current node.

        N)r   r-   indexinsertr0   )r    r!   	referencerH   r#   r#   r$   insert_befored   s    zElement.insert_beforec                 C   s$   | j | | j|j d|_dS )z2Remove node from the children of the current node.N)r/   r   r-   r0   rF   r#   r#   r$   remove_childn   s    zElement.remove_childc                 C   s   t | js,| jjsd| j_| j j|7  _n|du rb| jd jsLd| jd _| jd  j|7  _nxt| j}||j}|dkr| j|d  jsd| j|d  _| j|d   j|7  _n | jjsd| j_| j j|7  _dS )zInsert data as text in the current node.

        Text is positioned before the start of node insert_before or to the end
        of the node's text.

        If insert_before is a node, insert the text before this node.

         Nr   r   r   )rD   r-   rC   tailr   rH   )r    rC   rK   childrenrH   r#   r#   r$   insert_textt   s"    	

zElement.insert_textc                 C   s.   t | | j| j}| jjr*t| jj|j_|S )zReturn a shallow copy of the current node.

        The node has the same name and attributes, but no parent or children.

        )typer*   r+   r-   r5   r   )r    r"   r#   r#   r$   clone   s    zElement.clonec                 C   sz   |j r"|j d j j| jj7  _n0|jjs2d|j_| jjdurR|j j| jj7  _d| j_| j D ]}|| q`g | _ dS )zMove all the children of the current node to parent.

        This is needed so that trees that don't store text as nodes move the
        text in the correct way.

        r   rM   N)rO   r-   rN   rC   rG   )r    r0   childr#   r#   r$   reparent_children   s    
zElement.reparent_children)N)N)r%   r&   r'   r2   r,   r7   r?   propertyr   r@   rA   rO   rE   rG   rK   rL   rP   rR   rT   r#   r#   r#   r$   r(   ,   s   




r(   c                   @   s   e Zd Zdd ZdS )Commentc                 C   s   t || _d | _g | _d S r)   )r   rV   r-   r0   r/   )r    datar#   r#   r$   r2      s    zComment.__init__Nr%   r&   r'   r2   r#   r#   r#   r$   rV      s   rV   c                   @   s   e Zd Zdd ZdS )DocumentTypec                 C   s@   t | d || j_| jd| | jd| || _|| _d S )Nz
<!DOCTYPE>publicIdsystemId)r(   r2   r-   rC   set	public_id	system_id)r    r*   r]   r^   r#   r#   r$   r2      s    zDocumentType.__init__NrX   r#   r#   r#   r$   rY      s   rY   c                   @   s   e Zd Zdd ZdS )Documentc                 C   s   t | d d S )NDOCUMENT_ROOTr(   r2   r6   r#   r#   r$   r2      s    zDocument.__init__NrX   r#   r#   r#   r$   r_      s   r_   c                   @   s   e Zd Zdd ZdS )DocumentFragmentc                 C   s   t | d d S )NDOCUMENT_FRAGMENTra   r6   r#   r#   r$   r2      s    zDocumentFragment.__init__NrX   r#   r#   r#   r$   rb      s   rb   c                   @   s   e 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 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&d'Zd(d) ZdS ).TreeBuilderr   c                 C   s   |rd| _ nd| _ |   dS )zeCreate a TreeBuilder.

        If namespace_html_elements is True, namespace HTML elements.

        zhttp://www.w3.org/1999/xhtmlN)default_namespacereset)r    Znamespace_html_elementsr#   r#   r$   r2      s    zTreeBuilder.__init__c                 C   s,   g | _ t | _d | _d | _d| _t | _d S )NF)open_elementsr   active_formatting_elementsZhead_elementZform_elementinsert_from_tabler_   documentr6   r#   r#   r$   rf      s    zTreeBuilder.resetNc                 C   s   t |d}|s.t|tr t|f}t|ts.J t| \}}t| jD ]>}|rZ||krZ dS |sn|j|krn dS ||j|v A rD dS qDdsJ d S )Nr   TF)	hasattrr;   strr.   r<   _list_elementsreversedrg   r   )r    targetvariantZ
exact_nodeZlist_elementsinvertr!   r#   r#   r$   element_in_scope   s    

zTreeBuilder.element_in_scopec                 C   s   | j s
d S t| j d }| j | }|tu s4|| jv r8d S |turl|| jv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)rQ   r*   r+   rW   )	rh   rD   r   rg   rR   insert_elementr*   r+   r   )r    ientryrR   r"   r#   r#   r$   &reconstruct_active_formatting_elements   s0    


z2TreeBuilder.reconstruct_active_formatting_elementsc                 C   s(   | j  }| j r$|tur$| j  }q
d S r)   )rh   popr   )r    rv   r#   r#   r$    clear_active_formatting_elements/  s    
z,TreeBuilder.clear_active_formatting_elementsc                 C   s:   | j ddd D ]$}|tu r" q6q|j|kr|  S qdS )zFind name between end of active formatting elements and last marker.

        If an element with this name exists, return it. Else return False.

        Nr   F)rh   r   r*   )r    r*   itemr#   r#   r$   %element_in_active_formatting_elements4  s    

z1TreeBuilder.element_in_active_formatting_elementsc                 C   s&   |  |}| j| | j| d S r)   )create_elementrg   r   rj   rG   )r    tokenr"   r#   r#   r$   insert_rootC  s    
zTreeBuilder.insert_rootc                 C   s4   |d }|d }|d }t |||}| j| d S )Nr*   rZ   r[   )rY   rj   rG   )r    r}   r*   r]   r^   doctyper#   r#   r$   insert_doctypeH  s
    zTreeBuilder.insert_doctypec                 C   s   | t|d  d S )NrW   )rG   rV   )r    r}   r0   r#   r#   r$   insert_commentP  s    zTreeBuilder.insert_commentc                 C   s.   |d }| d| j}t||}|d |_|S )z/Create an element but don't insert it anywhere.r*   r+   rW   )getre   r(   r   r    r}   r*   r+   r"   r#   r#   r$   r|   S  s
    

zTreeBuilder.create_elementc                 C   s   | j S r)   )_insert_from_tabler6   r#   r#   r$   _get_insert_from_table[  s    z"TreeBuilder._get_insert_from_tablec                 C   s    || _ |r| j| _n| j| _dS )z.Switch the function used to insert an element.N)r   insert_element_tablert   insert_element_normal)r    r>   r#   r#   r$   _set_insert_from_table^  s    
z"TreeBuilder._set_insert_from_tablec                 C   sd   |d }t |ts"J d| d|d| j}t||}|d |_| jd | | j| |S )Nr*   zElement z not unicoder+   rW   r   )	r;   rl   r   re   r(   r   rg   rG   r   r   r#   r#   r$   r   h  s    

z!TreeBuilder.insert_element_normalc                 C   s`   |  |}| jd jtvr$| |S |  \}}|du rD|| n||| | j| |S )z.Create an element and insert it into the tree.r   N)	r|   rg   r*   r   r   !get_table_misnested_node_positionrG   rK   r   )r    r}   r"   r0   rK   r#   r#   r$   r   r  s    

z TreeBuilder.insert_element_tablec                 C   sT   |du r| j d }| jo&| j d jtv }|rF|  \}}||| n
|| dS )zInsert text data.Nr   )rg   ri   r*   r   r   rP   )r    rW   r0   Zin_tablerK   r#   r#   r$   rP     s    
zTreeBuilder.insert_textc                 C   sx   d}d}d}| j ddd D ]}|jdkr|} q4q|rf|jrJ|j}|}qp| j |d }| j | }n
| j d }||fS )zAGet foster parent element and sibling (or None) to insert before.Nr   r   r   r   )rg   r*   r0   rH   )r    Z
last_tableZfoster_parentrK   r"   rH   r#   r#   r$   r     s    

z-TreeBuilder.get_table_misnested_node_positionc                 C   s4   | j d j}|tv r0||kr0| j   | | d S )Nr   )rg   r*   _implied_end_tagsrx   generate_implied_end_tags)r    excluder*   r#   r#   r$   r     s    
z%TreeBuilder.generate_implied_end_tagsFc                 C   s2   |r| j jS | j j| jdu r"dnd| j dS )zReturn the final tree.Nr	   r3   z}html)rj   r-   findre   )r    Z	full_treer#   r#   r$   get_document  s    zTreeBuilder.get_documentc                 C   s   t  }| jd | |jS )zReturn the final fragment.r   )rb   rg   rT   r-   )r    fragmentr#   r#   r$   get_fragment  s    zTreeBuilder.get_fragment)N)N)N)F)r%   r&   r'   __doc__r2   rf   rr   rw   ry   r{   r~   r   r   r|   r   r   rU   ri   r   r   rP   r   r   r   r   r#   r#   r#   r$   rd      s*   
0




rd   )r   r   Z	xml.etreer   	constantsr   r   r   r   r.   	frozensetrm   r   r   r   r(   rV   rY   r_   rb   rd   r#   r#   r#   r$   <module>   s(   
 	
