
    ,g(>              	       ~   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z        df e
ee	d
fe	dfhz        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e      Z G d d      Z G d de      Z G d de      Z G d de      Z G d de      Z G d d      Zy) 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 Zy)ActiveFormattingElementsc                    d}|t         urj| ddd   D ]_  }|t         u r nU|j                  |j                  k(  xr |j                  |j                  k(  }|r|dz  }|dk(  sN| j                  |        n t        j                  | |       y)z#Append node to the end of the list.r   Nr      )Marker
name_tuple
attributesremover   append)selfnodeequal_countelementnodes_equals        m/home/viktor/gitlab-persoonlijk/factuur-applicatie/venv/lib/python3.12/site-packages/tinyhtml5/treebuilder.pyr"   zActiveFormattingElements.append   s    v": 
f$&&$//9 :&&$//9  1$K!#KK(
 	D$    N)__name__
__module____qualname__r"    r)   r(   r   r      s     r)   r   c                       e Zd ZddZd Zd Zd Z eee      Zd Z	d Z
 ee	e
      Zd Zd	 Zd
 Zd ZddZd Zd Zy)ElementNc                     || _         || _        t        j                  | j	                  ||            | _        |t        | j                   f| _        n| j                  | j                   f| _        g | _        d | _	        y N)
name	namespacer   r/   _get_etree_tag_element_htmlr   	_childrenparentr#   r2   r3   s      r(   __init__zElement.__init__-   si    	"#++D,?,?i,PQ#TYY.DO"nndii7DO r)   c                     ||S d| d| S )N{}r-   r9   s      r(   r4   zElement._get_etree_tag:   s     (tF9+Rv.FFr)   c                 .    | j                   j                  S r1   )r5   attribr#   s    r(   _get_attributeszElement._get_attributes=   s    }}###r)   c                     | j                   j                  }|j                          |r>|j                         D ]*  \  }}t	        |t
              rd|d    d|d    n|}|||<   , y y )Nr<      r=   r   )r5   r?   clearitems
isinstancetuple)r#   r    element_attributeskeyvaluer2   s         r(   _set_attributeszElement._set_attributes@   st    !]]11  " )..0 1
U2<S%2HCF82c!fX.c+0"4(1 r)   c                     | j                   S r1   )r7   r@   s    r(   _get_childrenzElement._get_childrenM   s    ~~r)   c                 `    | j                   d d = g | _        |D ]  }| j                  |        y r1   )r5   r7   insert_child)r#   rJ   r&   s      r(   _set_childrenzElement._set_childrenP   s2    MM! 	'Gg&	'r)   c                 n    t        | j                  j                  xs t        | j                              S )z>Return True if the node has children or text, False otherwise.)boolr5   textlenr@   s    r(   has_contentzElement.has_contentZ   s%    DMM&&<#dmm*<==r)   c                     | j                   j                  |       | j                  j                  |j                         | |_        y)z+Insert node as a child of the current node.N)r7   r"   r5   r8   r#   r$   s     r(   append_childzElement.append_child^   1    d#T]]+r)   c                     t        | j                        j                  |j                        }| j                  j                  ||j                         | |_        y)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   r5   indexinsertr8   )r#   r$   	referencer[   s       r(   insert_beforezElement.insert_befored   sB     T]]#)))*<*<=UDMM2r)   c                     | j                   j                  |       | j                  j                  |j                         d|_        y)z2Remove node from the children of the current node.N)r7   r!   r5   r8   rW   s     r(   remove_childzElement.remove_childn   rY   r)   c                    t        | j                        sG| j                  j                  sd| j                  _        | j                  xj                  |z  c_        y|P| j                  d   j                  sd| j                  d   _        | j                  d   xj                  |z  c_        yt	        | j                        }|j                  |j                        }|dkD  rY| j                  |dz
     j                  sd| j                  |dz
     _        | j                  |dz
     xj                  |z  c_        y| j                  j                  sd| j                  _        | j                  xj                  |z  c_        y)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   )rT   r5   rS   tailr   r[   )r#   rS   r^   childrenr[   s        r(   insert_textzElement.insert_textt   s#    4==!==%%%'"MM$&"==$)))+b!&MM"""d*" DMM*HNN=#9#9:Eqy}}UQY/4446DMM%!),1eai(--5-}})))+DMM&""d*"r)   c                      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.

        )typer2   r3   r5   r?   r   )r#   r&   s     r(   clonezElement.clone   sM     $t*TYY7==&*4==+?+?&@G#r)   c                    |j                   rA|j                   d   j                  xj                  | j                  j                  z  c_        np|j                  j                  sd|j                  _        | j                  j                  3|j                  xj                  | j                  j                  z  c_        d| j                  _        | j                   D ]  }|j	                  |        g | _         y)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   rb   N)rd   r5   rc   rS   rX   )r#   r8   childs      r(   reparent_childrenzElement.reparent_children   s     ??OOB((--1C1CC-??''')$}}!!-$$(:(::$]] 	'E&	'r)   r1   )r*   r+   r,   r:   r4   rA   rK   propertyr    rM   rP   rd   rU   rX   r^   r`   re   rh   rk   r-   r)   r(   r/   r/   ,   s`    G$1 /?;J' }5H>+>	r)   r/   c                       e Zd Zd Zy)Commentc                 T    t        j                  |      | _        d | _        g | _        y r1   )r   rn   r5   r8   r7   )r#   datas     r(   r:   zComment.__init__   s$     $++D1r)   Nr*   r+   r,   r:   r-   r)   r(   rn   rn      s    r)   rn   c                       e Zd Zd Zy)DocumentTypec                     t         j                  | d       || j                  _        | j                  j	                  d|       | j                  j	                  d|       || _        || _        y )Nz
<!DOCTYPE>publicIdsystemId)r/   r:   r5   rS   set	public_id	system_id)r#   r2   rx   ry   s       r(   r:   zDocumentType.__init__   sT    |,!*i0*i0""r)   Nrq   r-   r)   r(   rs   rs      s    #r)   rs   c                       e Zd Zd Zy)Documentc                 0    t         j                  | d       y )NDOCUMENT_ROOTr/   r:   r@   s    r(   r:   zDocument.__init__   s    /r)   Nrq   r-   r)   r(   r{   r{      s    0r)   r{   c                       e Zd Zd Zy)DocumentFragmentc                 0    t         j                  | d       y )NDOCUMENT_FRAGMENTr~   r@   s    r(   r:   zDocumentFragment.__init__   s    23r)   Nrq   r-   r)   r(   r   r      s    4r)   r   c                       e Zd ZdZd Zd ZddZd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd Z eee      Zd Zd ZddZd ZddZddZd Zy)TreeBuilderr   c                 F    |rd| _         nd| _         | j                          y)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_elementss     r(   r:   zTreeBuilder.__init__   s      #%CD"%)D"

r)   c                 x    g | _         t               | _        d | _        d | _        d| _        t               | _        y )NF)open_elementsr   active_formatting_elementshead_elementform_elementinsert_from_tabler{   documentr@   s    r(   r   zTreeBuilder.reset   s6    *B*D'  !& 
r)   Nc                     t        |d      }|s*t        |t              rt        |f}t        |t              sJ t
        |   \  }}t        | j                        D ]1  }|r||k(  r y|s|j                  |k(  r y||j                  |v z  s1 y J )Nr   TF)	hasattrrF   strr6   rG   _list_elementsreversedr   r   )r#   targetvariant
exact_nodelist_elementsinvertr$   s          r(   element_in_scopezTreeBuilder.element_in_scope   s     V\2
&#&fe,,, .w 7vT//0 	DdfnDOOv$=DOO}<=	 	ur)   c                 "   | j                   sy t        | j                         dz
  }| j                   |   }|t        u s|| j                  v ry |t        urA|| j                  vr3|dk(  rd}n+|dz  }| j                   |   }|t        ur|| j                  vr3	 |dz  }| j                   |   }|j	                         }| j                  d|j                  |j                  |j                  d      }|| j                   |<   || j                   d   k(  ry {)Nr   r   r   StartTag)rg   r2   r3   rp   )	r   rT   r   r   rh   insert_elementr2   r3   r    )r#   ientryrh   r&   s        r(   &reconstruct_active_formatting_elementsz2TreeBuilder.reconstruct_active_formatting_elements   s/    .. //014//2F?et'9'99 6!e43E3E&EAvFA33A6E 6!e43E3E&E FA 33A6EKKME ))"

"__((	+ G 29D++A. $99"==+ r)   c                     | j                   j                         }| j                   r:|t        ur1| j                   j                         }| j                   r
|t        ur/y y y y r1   )r   popr   )r#   r   s     r(    clear_active_formatting_elementsz,TreeBuilder.clear_active_formatting_elements/  sU    //335--%v2E33779E --%v2E-2E-r)   c                 l    | j                   ddd   D ]  }|t        u r y|j                  |k(  s|c S  y)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)r   r   r2   )r#   r2   items      r(   %element_in_active_formatting_elementsz1TreeBuilder.element_in_active_formatting_elements4  sK     33DbD9 	D v~  d"	 r)   c                     | j                  |      }| j                  j                  |       | j                  j	                  |       y r1   )create_elementr   r"   r   rX   )r#   tokenr&   s      r(   insert_rootzTreeBuilder.insert_rootC  s8    %%e,!!'*""7+r)   c                 r    |d   }|d   }|d   }t        |||      }| j                  j                  |       y )Nr2   ru   rv   )rs   r   rX   )r#   r   r2   rx   ry   doctypes         r(   insert_doctypezTreeBuilder.insert_doctypeH  s?    V}*%	*%	tY	:""7+r)   c                 >    |j                  t        |d                y )Nrp   )rX   rn   )r#   r   r8   s      r(   insert_commentzTreeBuilder.insert_commentP  s    GE&M23r)   c                 t    |d   }|j                  d| j                        }t        ||      }|d   |_        |S )z/Create an element but don't insert it anywhere.r2   r3   rp   )getr   r/   r    r#   r   r2   r3   r&   s        r(   r   zTreeBuilder.create_elementS  s>    V}IIk4+A+AB	$	*"6]r)   c                     | j                   S r1   )_insert_from_tabler@   s    r(   _get_insert_from_tablez"TreeBuilder._get_insert_from_table[  s    &&&r)   c                 \    || _         |r| j                  | _        y| j                  | _        y)z.Switch the function used to insert an element.N)r   insert_element_tabler   insert_element_normal)r#   rJ   s     r(   _set_insert_from_tablez"TreeBuilder._set_insert_from_table^  s)    "'"&";";D"&"<"<Dr)   c                    |d   }t        |t              sJ d| d       |j                  d| j                        }t	        ||      }|d   |_        | j                  d   j                  |       | j                  j                  |       |S )Nr2   zElement z not unicoder3   rp   r   )	rF   r   r   r   r/   r    r   rX   r"   r   s        r(   r   z!TreeBuilder.insert_element_normalh  s    V}$$Cl&CC$IIk4+A+AB	$	*"6]2++G4!!'*r)   c                 0   | j                  |      }| j                  d   j                  t        vr| j	                  |      S | j                         \  }}||j                  |       n|j                  ||       | j                  j                  |       |S )z.Create an element and insert it into the tree.r   )	r   r   r2   r	   r   !get_table_misnested_node_positionrX   r^   r"   )r#   r   r&   r8   r^   s        r(   r   z TreeBuilder.insert_element_tabler  s    %%e,b!&&.HH--e44 %)$J$J$L!FM$##G,$$Wm<%%g.r)   c                     || j                   d   }| j                  xr | j                   d   j                  t        v }|r&| j	                         \  }}|j                  ||       y|j                  |       y)zInsert text data.Nr   )r   r   r2   r	   r   re   )r#   rp   r8   in_tabler^   s        r(   re   zTreeBuilder.insert_text  s    >''+F "" Fr"''+EE 	  %)$J$J$L!FMt]3t$r)   c                 *   d}d}d}| j                   ddd   D ]  }|j                  dk(  s|} n |rO|j                  r|j                  }|}||fS | j                   j                  |      dz
  }| j                   |   }||fS | j                   d   }||fS )zAGet foster parent element and sibling (or None) to insert before.Nr   r   r   r   )r   r2   r8   r[   )r#   
last_tablefoster_parentr^   r&   r[   s         r(   r   z-TreeBuilder.get_table_misnested_node_position  s     
))$B$/ 	G||w&$
	     * 1 1 * m++	 **00<q@ $ 2 25 9 m++ !..q1Mm++r)   c                     | j                   d   j                  }|t        v r2||k7  r,| j                   j                          | j	                  |       y y y )Nr   )r   r2   _implied_end_tagsr   generate_implied_end_tags)r#   excluder2   s      r(   r   z%TreeBuilder.generate_implied_end_tags  sQ    !!"%**$$""$ **73	 *9$r)   c                     |r| j                   j                  S | j                   j                  j                  | j                  d      S d| j                   d      S )zReturn the final tree.r
   r<   z}html)r   r5   findr   )r#   	full_trees     r(   get_documentzTreeBuilder.get_document  s]    ==)))}}%%**,,4F1 	1''(/1 	1r)   c                 j    t               }| j                  d   j                  |       |j                  S )zReturn the final fragment.r   )r   r   rk   r5   )r#   fragments     r(   get_fragmentzTreeBuilder.get_fragment  s/    #%1//9   r)   r1   )F)r*   r+   r,   __doc__r:   r   r   r   r   r   r   r   r   r   r   r   rl   r   r   r   re   r   r   r   r   r-   r)   r(   r   r      sv    
	#,.`:
,
,4'= !!79OP % ,641!r)   r   )r   r   	xml.etreer   	constantsr   r   r	   r   r6   	frozensetr   r   r   r   r/   rn   rs   r{   r   r   r-   r)   r(   <module>r      s     ! O O
 
6%&
.)eX->,??@%H'E4=5$-*HHI5Q%5'*:;<eD5*-x/@ABDI WX  t  &C CLg #7 #0w 0
4w 4
r! r!r)   