o
    »Ùøi,>  ã                   @   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                   @   ó   e Zd Zdd„ ZdS )ÚActiveFormattingElementsc                 C   sv   d}|t ur3| ddd… D ]%}|t u r n|j|jko |j|jk}|r'|d7 }|dkr2|  |¡  nqt | |¡ dS )z#Append node to the end of the list.r   Néÿÿÿÿr   é   )ÚMarkerÚ
name_tupleÚ
attributesÚremover   Úappend)ÚselfÚnodeÚequal_countÚelementÚnodes_equal© r&   úb/home/develop/edictos_py/central_edictos/env/lib/python3.10/site-packages/tinyhtml5/treebuilder.pyr       s   
þ
þzActiveFormattingElements.appendN)Ú__name__Ú
__module__Ú__qualname__r    r&   r&   r&   r'   r      ó    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 rt| 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&   r5   r&   r&   r'   r0   :   s   zElement._get_etree_tagc                 C   s   | j jS r-   )r1   Úattrib©r!   r&   r&   r'   Ú_get_attributes=   s   zElement._get_attributesc                 C   s\   | j j}| ¡  |r*| ¡ D ]\}}t|tƒr#d|d › d|d › �n|}|||< qd S d S )Nr7   é   r8   r   )r1   r9   ÚclearÚitemsÚ
isinstanceÚtuple)r!   r   Úelement_attributesÚkeyÚvaluer.   r&   r&   r'   Ú_set_attributes@   s   &
ûzElement._set_attributesc                 C   ó   | j S r-   )r3   r:   r&   r&   r'   Ú_get_childrenM   ó   zElement._get_childrenc                 C   s*   | j d d …= g | _|D ]}|  |¡ qd S r-   )r1   r3   Úinsert_child)r!   rC   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.)Úboolr1   ÚtextÚlenr:   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)r3   r    r1   r4   ©r!   r"   r&   r&   r'   Úappend_child^   ó   
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   r1   ÚindexÚinsertr4   )r!   r"   Ú	referencerQ   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)r3   r   r1   r4   rN   r&   r&   r'   Úremove_childn   rP   zElement.remove_childc                 C   sä   t | jƒs| jjsd| j_| j j|7  _dS |du r3| jd js'd| jd _| jd  j|7  _dS t| jƒ}| |j¡}|dkr`| j|d  jsRd| j|d  _| j|d   j|7  _dS | jjshd| 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   )rL   r1   rK   Útailr   rQ   )r!   rK   rT   ÚchildrenrQ   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/   r1   r9   r   )r!   r$   r&   r&   r'   Úclone“   s   zElement.clonec                 C   sz   |j r|j d j j| jj7  _n|jjsd|j_| jjdur)|j j| jj7  _d| j_| j D ]}| |¡ q0g | _ 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   rV   N)rX   r1   rW   rK   rO   )r!   r4   Úchildr&   r&   r'   Úreparent_childrenž   s   

zElement.reparent_childrenr-   )r(   r)   r*   r6   r0   r;   rD   Úpropertyr   rF   rI   rX   rM   rO   rT   rU   rY   r[   r]   r&   r&   r&   r'   r,   ,   s     




r,   c                   @   r   )ÚCommentc                 C   s   t  |¡| _d | _g | _d S r-   )r   r_   r1   r4   r3   )r!   Údatar&   r&   r'   r6   ³   s   
zComment.__init__N©r(   r)   r*   r6   r&   r&   r&   r'   r_   ²   r+   r_   c                   @   r   )ÚDocumentTypec                 C   s@   t  | d¡ || j_| j d|¡ | j d|¡ || _|| _d S )Nz
<!DOCTYPE>ÚpublicIdÚsystemId)r,   r6   r1   rK   ÚsetÚ	public_idÚ	system_id)r!   r.   rf   rg   r&   r&   r'   r6   ¼   s   
zDocumentType.__init__Nra   r&   r&   r&   r'   rb   »   r+   rb   c                   @   r   )ÚDocumentc                 C   ó   t  | d¡ d S )NÚDOCUMENT_ROOT©r,   r6   r:   r&   r&   r'   r6   Æ   ó   zDocument.__init__Nra   r&   r&   r&   r'   rh   Å   r+   rh   c                   @   r   )ÚDocumentFragmentc                 C   ri   )NÚDOCUMENT_FRAGMENTrk   r:   r&   r&   r'   r6   Ë   rl   zDocumentFragment.__init__Nra   r&   r&   r&   r'   rm   Ê   r+   rm   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'   r6   Ò   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_tablerh   Údocumentr:   r&   r&   r'   rq   Þ   s   zTreeBuilder.resetNc                 C   sˆ   t |dƒ}|st|tƒrt|f}t|tƒsJ ‚t| \}}t| jƒD ]}|r-||kr- dS |s7|j|kr7 dS ||j|v A rA dS q"t	‚)Nr   TF)
Úhasattrr?   Ústrr2   r@   Ú_list_elementsÚreversedrs   r   Ú
ValueError)r!   ÚtargetÚvariantÚ
exact_nodeÚ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 s|| jv rd S |tur>|| jvr>|dkr,d}n|d8 }| j | }|tur>|| jvs%	 |d7 }| j | }| ¡ }|  d|j|j|jdœ¡}|| j |< || j d krgd S q?)Nr   r   r   TÚStartTag)rZ   r.   r/   r`   )	rt   rL   r   rs   r[   Úinsert_elementr.   r/   r   )r!   ÚiÚentryr[   r$   r&   r&   r'   Ú&reconstruct_active_formatting_elementsÿ   s6   

ù	
ü
ëz2TreeBuilder.reconstruct_active_formatting_elementsc                 C   s@   | j  ¡ }| j r|tur| j  ¡ }| j r|tusd S d S d S d S r-   )rt   Úpopr   )r!   r‡   r&   r&   r'   Ú clear_active_formatting_elements/  s   

ÿz,TreeBuilder.clear_active_formatting_elementsc                 C   s:   | j ddd… D ]}|tu r dS |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)rt   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_elementrs   r    rx   rO   )r!   Útokenr$   r&   r&   r'   Úinsert_rootC  s   
zTreeBuilder.insert_rootc                 C   s4   |d }|d }|d }t |||ƒ}| j |¡ d S )Nr.   rc   rd   )rb   rx   rO   )r!   rŽ   r.   rf   rg   Údoctyper&   r&   r'   Úinsert_doctypeH  s
   zTreeBuilder.insert_doctypec                 C   s   |  t|d ƒ¡ d S )Nr`   )rO   r_   )r!   rŽ   r4   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/   r`   )Úgetrp   r,   r   ©r!   rŽ   r.   r/   r$   r&   r&   r'   r�   S  s
   

zTreeBuilder.create_elementc                 C   rE   r-   )Ú_insert_from_tabler:   r&   r&   r'   Ú_get_insert_from_table[  rG   z"TreeBuilder._get_insert_from_tablec                 C   s"   || _ |r| j| _dS | j| _dS )z.Switch the function used to insert an element.N)r•   Úinsert_element_tabler…   Úinsert_element_normal)r!   rC   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/   r`   r   )	r?   rz   r“   rp   r,   r   rs   rO   r    r”   r&   r&   r'   r˜   h  s   

z!TreeBuilder.insert_element_normalc                 C   s`   |   |¡}| jd jtvr|  |¡S |  ¡ \}}|du r"| |¡ n| ||¡ | j |¡ |S )z.Create an element and insert it into the tree.r   N)	r�   rs   r.   r   r˜   Ú!get_table_misnested_node_positionrO   rT   r    )r!   rŽ   r$   r4   rT   r&   r&   r'   r—   r  s   

z TreeBuilder.insert_element_tablec                 C   sV   |du r	| j d }| jo| j d jtv }|r$|  ¡ \}}| ||¡ dS | |¡ dS )zInsert text data.Nr   )rs   rw   r.   r   rš   rY   )r!   r`   r4   Úin_tablerT   r&   r&   r'   rY   ‚  s   
þzTreeBuilder.insert_textc                 C   s„   d}d}d}| j ddd… D ]}|jdkr|} nq|r9|jr(|j}|}||fS | j  |¡d }| j | }||fS | j d }||fS )zAGet foster parent element and sibling (or None) to insert before.Nr   r   r   r   )rs   r.   r4   rQ   )r!   Ú
last_tableÚfoster_parentrT   r$   rQ   r&   r&   r'   rš   ’  s$   
þü

ÿz-TreeBuilder.get_table_misnested_node_positionc                 C   s<   | j d j}|tv r||kr| j  ¡  |  |¡ d S d S d S )Nr   )rs   r.   Ú_implied_end_tagsr‰   Úgenerate_implied_end_tags)r!   Úexcluder.   r&   r&   r'   rŸ   ­  s
   
üz%TreeBuilder.generate_implied_end_tagsFc                 C   s4   |r| j jS | j j | jdu rd¡S d| j› d�¡S )zReturn the final tree.Nr	   r7   z}html)rx   r1   Úfindrp   )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   )rm   rs   r]   r1   )r!   Úfragmentr&   r&   r'   Úget_fragment½  s   zTreeBuilder.get_fragmentr-   )F)r(   r)   r*   Ú__doc__r6   rq   rƒ   rˆ   rŠ   rŒ   r�   r‘   r’   r�   r–   r™   r^   rw   r˜   r—   rY   rš   rŸ   r£   r¥   r&   r&   r&   r'   ro   Ï   s,    
0




ro   )r¦   r   Ú	xml.etreer   Ú	constantsr   r   r   r   r2   Ú	frozensetr{   rž   r   r   r,   r_   rb   rh   rm   ro   r&   r&   r&   r'   Ú<module>   s*    
û 	
