
    Bh(>              	       ~   S r SSKJr  SSKJr  SSKJrJrJr  Sr\S   r	\
" \5      S4\
" \\	S	41-  5      S4\
" \\	S
4\	S41-  5      S4\
" \	S4\	S4/5      S4\
" \	S4\	S4/5      S4S.r\
" S5      r " S S\5      r " S S5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S5      rg) 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                       \ rS rSrS rSrg)ActiveFormattingElements   c                 4   SnU[         Lax  U SSS2    Hl  nU[         L a    ObUR                  UR                  :H  =(       a    UR                  UR                  :H  nU(       a  US-  nUS:X  d  M[  U R                  U5          O   [        R                  X5        g)z#Append node to the end of the list.r   Nr      )Marker
name_tuple
attributesremover   append)selfnodeequal_countelementnodes_equals        G/var/www/html/env/lib/python3.13/site-packages/tinyhtml5/treebuilder.pyr#   ActiveFormattingElements.append   s    v":f$&&$//9 :&&$//9  1$K!#KK( & 	D     N)__name__
__module____qualname____firstlineno__r#   __static_attributes__r,   r+   r)   r   r      s     r+   r   c                       \ rS rSrSS jrS rS rS r\" \\5      r	S r
S r\" \
\5      rS	 rS
 rS rS rSS jrS rS rSrg)Element,   Nc                     Xl         X l        [        R                  " U R	                  X5      5      U l        Uc  [        U R                   4U l        OU R                  U R                   4U l        / U l        S U l	        g N)
name	namespacer   r3   _get_etree_tag_element_htmlr    	_childrenparentr$   r7   r8   s      r)   __init__Element.__init__-   sc    	"#++D,?,?,PQ#TYY.DO"nndii7DO r+   c                     Uc  U$ SU SU 3$ )N{}r,   r>   s      r)   r9   Element._get_etree_tag:   s     (tF9+Rv.FFr+   c                 .    U R                   R                  $ r6   )r:   attribr$   s    r)   _get_attributesElement._get_attributes=   s    }}###r+   c                     U R                   R                  nUR                  5         U(       aC  UR                  5        H.  u  p4[	        U[
        5      (       a  SUS    SUS    3OUnXBU'   M0     g g )NrB      rC   r   )r:   rF   clearitems
isinstancetuple)r$   r!   element_attributeskeyvaluer7   s         r)   _set_attributesElement._set_attributes@   sm    !]]11  " )..0
2<S%2H2HCF82c!fX.c+04( 1 r+   c                     U R                   $ r6   )r<   rG   s    r)   _get_childrenElement._get_childrenM   s    ~~r+   c                 d    U R                   S S 2	 / U l        U H  nU R                  U5        M     g r6   )r:   r<   insert_child)r$   rR   r'   s      r)   _set_childrenElement._set_childrenP   s-    MM!Gg& r+   c                 x    [        U R                  R                  =(       d    [        U R                  5      5      $ )z>Return True if the node has children or text, False otherwise.)boolr:   textlenrG   s    r)   has_contentElement.has_contentZ   s%    DMM&&<#dmm*<==r+   c                     U R                   R                  U5        U R                  R                  UR                  5        Xl        g)z+Insert node as a child of the current node.N)r<   r#   r:   r=   r$   r%   s     r)   append_childElement.append_child^   s/    d#T]]+r+   c                     [        U R                  5      R                  UR                  5      nU R                  R                  X1R                  5        Xl        g)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insertr=   )r$   r%   	referencerg   s       r)   insert_beforeElement.insert_befored   s>     T]]#)))*<*<=UMM2r+   c                     U R                   R                  U5        U R                  R                  UR                  5        SUl        g)z2Remove node from the children of the current node.N)r<   r"   r:   r=   rc   s     r)   remove_childElement.remove_childn   s1    d#T]]+r+   c                 >   [        U R                  5      (       dL  U R                  R                  (       d  SU R                  l        U R                  =R                  U-  sl        gUcU  U R                  S   R                  (       d  SU R                  S   l        U R                  S   =R                  U-  sl        g[	        U R                  5      nUR                  UR                  5      nUS:  a^  U R                  US-
     R                  (       d  SU R                  US-
     l        U R                  US-
     =R                  U-  sl        gU R                  R                  (       d  SU R                  l        U R                  =R                  U-  sl        g)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   )r_   r:   r^   tailr   rg   )r$   r^   rj   childrenrg   s        r)   insert_text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                     [        U 5      " U R                  U R                  5      nU R                  R                  (       a.  [        U R                  R                  5      UR                  l        U$ )zsReturn a shallow copy of the current node.

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

)typer7   r8   r:   rF   r   )r$   r'   s     r)   cloneElement.clone   sK     t*TYY7==&*4==+?+?&@G#r+   c                    UR                   (       aA  UR                   S   R                  =R                  U R                  R                  -  sl        OvUR                  R                  (       d  SUR                  l        U R                  R                  b3  UR                  =R                  U R                  R                  -  sl        SU R                  l        U R                    H  nUR	                  U5        M     / U l         g)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   rp   N)rr   r:   rq   r^   rd   )r$   r=   childs      r)   reparent_childrenElement.reparent_children   s     ??OOB((--1C1CC-??''')$}}!!-$$(:(::$]]E& #r+   )r<   r:   rr   r7   r    r8   r=   r6   )r-   r.   r/   r0   r?   r9   rH   rS   propertyr!   rV   rZ   rr   r`   rd   rj   rm   rs   rw   r{   r1   r,   r+   r)   r3   r3   ,   s`    G$1 /?;J' }5H>+>	r+   r3   c                       \ rS rSrS rSrg)Comment   c                 V    [         R                  " U5      U l        S U l        / U l        g r6   )r   r   r:   r=   r<   )r$   datas     r)   r?   Comment.__init__   s$     $++D1r+   )r<   r:   r=   Nr-   r.   r/   r0   r?   r1   r,   r+   r)   r   r      s    r+   r   c                       \ rS rSrS rSrg)DocumentType   c                     [         R                  U S5        XR                  l        U R                  R	                  SU5        U R                  R	                  SU5        X l        X0l        g )Nz
<!DOCTYPE>publicIdsystemId)r3   r?   r:   r^   set	public_id	system_id)r$   r7   r   r   s       r)   r?   DocumentType.__init__   sN    |,!*i0*i0""r+   )r   r   Nr   r,   r+   r)   r   r      s    #r+   r   c                       \ rS rSrS rSrg)Document   c                 0    [         R                  U S5        g )NDOCUMENT_ROOTr3   r?   rG   s    r)   r?   Document.__init__   s    /r+   r,   Nr   r,   r+   r)   r   r      s    0r+   r   c                       \ rS rSrS rSrg)DocumentFragment   c                 0    [         R                  U S5        g )NDOCUMENT_FRAGMENTr   rG   s    r)   r?   DocumentFragment.__init__   s    23r+   r,   Nr   r,   r+   r)   r   r      s    4r+   r   c                       \ rS rSrSrS rS rSS jrS rS r	S	 r
S
 rS rS rS rS rS r\" \\5      rS rS rSS jrS rSS jrSS jrS rSrg)TreeBuilder   r   c                 P    U(       a  SU l         OSU l         U R                  5         g)zUCreate 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?   TreeBuilder.__init__   s      #%CD"%)D"

r+   c                 x    / U l         [        5       U l        S U l        S U l        SU l        [        5       U l        g )NF)open_elementsr   active_formatting_elementshead_elementform_elementinsert_from_tabler   documentrG   s    r)   r   TreeBuilder.reset   s6    *B*D'  !& 
r+   Nc                 `   [        US5      nU(       d4  [        U[        5      (       a  [        U4n[        U[        5      (       d   e[
        U   u  pE[        U R                  5       HB  nU(       a  Xa:X  a    gU(       d  UR                  U:X  a    gXVR                  U;   -  (       d  MB    g    e)Nr    TF)	hasattrrN   strr;   rO   _list_elementsreversedr   r    )r$   targetvariant
exact_nodelist_elementsinvertr%   s          r)   element_in_scopeTreeBuilder.element_in_scope   s     V\2
&#&&fe,,,, .w 7T//0DdnDOOv$=OO}<== 1 	ur+   c                 <   U R                   (       d  g [        U R                   5      S-
  nU R                   U   nU[        L d  X R                  ;   a  g U[        LaF  X R                  ;  a7  US:X  a  SnO.US-  nU R                   U   nU[        La  X R                  ;  a  M7   US-  nU R                   U   nUR	                  5       nU R                  SUR                  UR                  UR                  S.5      nX@R                   U'   X@R                   S   :X  a  g M{  )Nr   r   r   StartTag)rv   r7   r8   r   )	r   r_   r   r   rw   insert_elementr7   r8   r!   )r$   ientryrw   r'   s        r)   &reconstruct_active_formatting_elements2TreeBuilder.reconstruct_active_formatting_elements   s%    .. //014//2F?e'9'99 6!e3E3E&EAvFA33A6E 6!e3E3E&E FA 33A6EKKME ))"

"__((	+ G 29++A. 99"==+ r+   c                     U R                   R                  5       nU R                   (       aB  U[        La8  U R                   R                  5       nU R                   (       a  U[        La  M6  g g g g r6   )r   popr   )r$   r   s     r)    clear_active_formatting_elements,TreeBuilder.clear_active_formatting_elements/  sU    //335--%v2E33779E --%v2E-2E-r+   c                 t    U R                   SSS2    H"  nU[        L a    gUR                  U:X  d  M   Us  $    g)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   r7   )r$   r7   items      r)   %element_in_active_formatting_elements1TreeBuilder.element_in_active_formatting_elements4  sF     33DbD9D v~  d" : r+   c                     U R                  U5      nU R                  R                  U5        U R                  R	                  U5        g r6   )create_elementr   r#   r   rd   )r$   tokenr'   s      r)   insert_rootTreeBuilder.insert_rootC  s8    %%e,!!'*""7+r+   c                 p    US   nUS   nUS   n[        X#U5      nU R                  R                  U5        g )Nr7   r   r   )r   r   rd   )r$   r   r7   r   r   doctypes         r)   insert_doctypeTreeBuilder.insert_doctypeH  s=    V}*%	*%	t	:""7+r+   c                 >    UR                  [        US   5      5        g )Nr   )rd   r   )r$   r   r=   s      r)   insert_commentTreeBuilder.insert_commentP  s    GE&M23r+   c                 r    US   nUR                  SU R                  5      n[        X#5      nUS   Ul        U$ )z/Create an element but don't insert it anywhere.r7   r8   r   )getr   r3   r!   r$   r   r7   r8   r'   s        r)   r   TreeBuilder.create_elementS  s<    V}IIk4+A+AB	$*"6]r+   c                     U R                   $ r6   )_insert_from_tablerG   s    r)   _get_insert_from_table"TreeBuilder._get_insert_from_table[  s    &&&r+   c                 d    Xl         U(       a  U R                  U l        gU R                  U l        g)z.Switch the function used to insert an element.N)r   insert_element_tabler   insert_element_normal)r$   rR   s     r)   _set_insert_from_table"TreeBuilder._set_insert_from_table^  s'    "'"&";";D"&"<"<Dr+   c                 $   US   n[        U[        5      (       d   SU S35       eUR                  SU R                  5      n[	        X#5      nUS   Ul        U R                  S   R                  U5        U R                  R                  U5        U$ )Nr7   zElement z not unicoder8   r   r   )	rN   r   r   r   r3   r!   r   rd   r#   r   s        r)   r   !TreeBuilder.insert_element_normalh  s    V}$$$Cl&CC$IIk4+A+AB	$*"6]2++G4!!'*r+   c                 2   U R                  U5      nU R                  S   R                  [        ;  a  U R	                  U5      $ U R                  5       u  p4Uc  UR                  U5        OUR                  X$5        U R                  R                  U5        U$ )z.Create an element and insert it into the tree.r   )	r   r   r7   r	   r   !get_table_misnested_node_positionrd   rj   r#   )r$   r   r'   r=   rj   s        r)   r    TreeBuilder.insert_element_tabler  s    %%e,b!&&.HH--e44 %)$J$J$L!F$##G,$$W<%%g.r+   c                    Uc  U R                   S   nU R                  =(       a     U R                   S   R                  [        ;   nU(       a$  U R	                  5       u  p$UR                  X5        gUR                  U5        g)zInsert text data.Nr   )r   r   r7   r	   r   rs   )r$   r   r=   in_tablerj   s        r)   rs   TreeBuilder.insert_text  s{    >''+F "" Fr"''+EE 	  %)$J$J$L!Ft3t$r+   c                 @   SnSnSnU R                   SSS2    H  nUR                  S:X  d  M  Un  O   U(       aS  UR                  (       a  UR                  nUnX#4$ U R                   R                  U5      S-
  nU R                   U   n X#4$ U R                   S   nX#4$ )zAGet foster parent element and sibling (or None) to insert before.Nr   r   r   r   )r   r7   r=   rg   )r$   
last_tablefoster_parentrj   r'   rg   s         r)   r   -TreeBuilder.get_table_misnested_node_position  s     
))$B$/G||w&$
 0     * 1 1 * ++	 **00<q@ $ 2 25 9 ++ !..q1M++r+   c                     U R                   S   R                  nU[        ;   a2  X!:w  a,  U R                   R                  5         U R	                  U5        g g g )Nr   )r   r7   _implied_end_tagsr   generate_implied_end_tags)r$   excluder7   s      r)   r   %TreeBuilder.generate_implied_end_tags  sO    !!"%**$$""$ **73	 *9$r+   c                     U(       a  U R                   R                  $ U R                   R                  R                  U R                  c  S5      $ SU R                   S35      $ )zReturn the final tree.r
   rB   z}html)r   r:   findr   )r$   	full_trees     r)   get_documentTreeBuilder.get_document  s]    ==)))}}%%**,,4F1 	1''(/1 	1r+   c                 j    [        5       nU R                  S   R                  U5        UR                  $ )zReturn the final fragment.r   )r   r   r{   r:   )r$   fragments     r)   get_fragmentTreeBuilder.get_fragment  s/    #%1//9   r+   )	r   r   r   r   r   r   r   r   r   r6   )F)r-   r.   r/   r0   __doc__r?   r   r   r   r   r   r   r   r   r   r   r   r}   r   r   r   rs   r   r   r   r   r1   r,   r+   r)   r   r      sv    
	#,.`:
,
,4'= !!79OP % ,641!r+   r   )r   r   	xml.etreer   	constantsr   r   r	   r   r;   	frozensetr   r   r   r   r3   r   r   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+   