a
    ¥Q•h(>  ã                   @   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   Néÿÿÿÿr   é   )ÚMarkerÚ
name_tupleÚ
attributesÚremover   Úappend)ÚselfÚnodeZequal_countÚelementZnodes_equal© r#   úN/var/www/sistema_ama/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 )z’Insert 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s d| j|d  _| j|d   j|7  _n | jjsÊd| j_| j j|7  _dS )zæInsert 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 )zƒReturn 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 )z­Move 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 )NZ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    Ú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_elementsÚhead_elementÚform_elementÚinsert_from_tabler_   Údocumentr6   r#   r#   r$   re   Þ   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sŒJ ‚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rlq¼qld S )Nr   r   r   Z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    rw   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 )zšFind 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   rl   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   rl   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   )Úgetrd   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_tableru   Ú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;   rn   rƒ   rd   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   rk   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_tagsry   Ú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)rl   r-   Úfindrd   )r    Ú	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   re   rt   rx   rz   r|   r   r�   r‚   r}   r†   r‰   rU   rk   rˆ   r‡   rP   rŠ   rŒ   r�   r’   r#   r#   r#   r$   rc   Ï   s*   
0




rc   )r“   r   Z	xml.etreer   Ú	constantsr   r   r   r   r.   Ú	frozensetro   r‹   r   r   r(   rV   rY   r_   rb   rc   r#   r#   r#   r$   Ú<module>   s(   
û 	
