
    Ng8              	          d dl mZmZmZ d dlmZ ddlmZmZm	Z	 dZ
 ee          df eee	d         dfhz            df eee	d         d	fe	d         d
fhz            df ee	d         dfe	d         dfg          df ee	d         dfe	d         dfg          dfdZ G d de          Z G d de          Z G d de          ZdS )    )absolute_importdivisionunicode_literals)	text_type   )scopingElementstableInsertModeElements
namespacesNFhtmlbuttonolultableoptgroupoptionT)Nr   listr   selectc                   P    e Zd ZdZd Zd Zd Zd ZddZd Z	d	 Z
d
 Zd Zd ZdS )NodezRepresents an item in the treec                 Z    || _         d| _        d| _        i | _        g | _        g | _        dS )zRCreates a Node

        :arg name: The tag name associated with the node

        N)nameparentvalue
attributes
childNodes_flags)selfr   s     V/var/www/html/ai-engine/env/lib/python3.11/site-packages/html5lib/treebuilders/base.py__init__zNode.__init__   s4     	
     c                     d                     d | j                                        D                       }|rd| j        d|dS d| j        z  S )N c                 $    g | ]\  }}|d |dS )z="" ).0r   r   s      r   
<listcomp>z Node.__str__.<locals>.<listcomp>.   s9     "; "; ";&1dE 26uuu"= "; "; ";r    <><%s>)joinr   itemsr   )r   attributesStrs     r   __str__zNode.__str__-   sp     "; ";"&/"7"7"9"9"; "; "; < <  	( 	( $			===99TY''r    c                     d| j         z  S )Nr*   )r   r   s    r   __repr__zNode.__repr__6   s    ##r    c                     t           )z[Insert node as a child of the current node

        :arg node: the node to insert

        NotImplementedErrorr   nodes     r   appendChildzNode.appendChild9   
     "!r    Nc                     t           )aB  Insert data as text in the current node, positioned before the
        start of node insertBefore or to the end of the node's text.

        :arg data: the data to insert

        :arg insertBefore: True if you want to insert the text before the node
            and False if you want to insert it after the node

        r3   )r   datainsertBefores      r   
insertTextzNode.insertTextA   
     "!r    c                     t           )a  Insert node as a child of the current node, before refNode in the
        list of child nodes. Raises ValueError if refNode is not a child of
        the current node

        :arg node: the node to insert

        :arg refNode: the child node to insert the node before

        r3   )r   r6   refNodes      r   r;   zNode.insertBeforeM   r=   r    c                     t           )zhRemove node from the children of the current node

        :arg node: the child node to remove

        r3   r5   s     r   removeChildzNode.removeChildY   r8   r    c                 R    | j         D ]}|                    |           g | _         dS )zMove all the children of the current node to newParent.
        This is needed so that trees that don't store text as nodes move the
        text in the correct way

        :arg newParent: the node to move all this node's children to

        N)r   r7   )r   	newParentchilds      r   reparentChildrenzNode.reparentChildrena   s7     _ 	) 	)E!!%((((r    c                     t           )zReturn a shallow copy of the current node i.e. a node with the same
        name and attributes but with no parent or child nodes
        r3   r0   s    r   	cloneNodezNode.cloneNoden   s
     "!r    c                     t           )zFReturn true if the node has children or text, false otherwise
        r3   r0   s    r   
hasContentzNode.hasContentt   s
     "!r    N)__name__
__module____qualname____doc__r   r.   r1   r7   r<   r;   rA   rE   rG   rI   r%   r    r   r   r      s        ((  (( ( ($ $ $" " "
" 
" 
" 
"
" 
" 
"" " "  " " "" " " " "r    r   c                       e Zd Zd Zd ZdS )ActiveFormattingElementsc                     d}|t           k    rS| d d d         D ]G}|t           k    r n9|                     ||          r|dz  }|dk    r|                     |            nHt                              | |           d S )Nr         )Marker
nodesEqualremover   append)r   r6   
equalCountelements       r   rX   zActiveFormattingElements.append{   s    
6>>":  f$$E??7D11 $!OJ??KK(((E # 	D$r    c                 N    |j         |j         k    sdS |j        |j        k    sdS dS )NFT)	nameTupler   )r   node1node2s      r   rV   z#ActiveFormattingElements.nodesEqual   s1    %/1155#3335tr    N)rK   rL   rM   rX   rV   r%   r    r   rP   rP   z   s2                 r    rP   c                       e Zd ZdZdZdZdZdZdZd Z	d Z
ddZd Zd Zd Zd	 Zd
 Zd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 Zd Zd ZdS )TreeBuildera  Base treebuilder implementation

    * documentClass - the class to use for the bottommost node of a document
    * elementClass - the class to use for HTML Elements
    * commentClass - the class to use for comments
    * doctypeClass - the class to use for doctypes

    Nc                 P    |rd| _         nd| _         |                                  dS )zmCreate a TreeBuilder

        :arg namespaceHTMLElements: whether or not to namespace HTML elements

        zhttp://www.w3.org/1999/xhtmlN)defaultNamespacereset)r   namespaceHTMLElementss     r   r   zTreeBuilder.__init__   s0     ! 	)$BD!!$(D!

r    c                     g | _         t                      | _        d | _        d | _        d| _        |                                 | _        d S )NF)openElementsrP   activeFormattingElementsheadPointerformPointerinsertFromTabledocumentClassdocumentr0   s    r   rc   zTreeBuilder.reset   sI    (@(B(B%  $**,,r    c                 B   t          |d          }|s;t          |t                    rt          d         |f}t          |t                    sJ t
          |         \  }}t          | j                  D ],}|r	||k    r dS |s|j        |k    r dS ||j        |v z  r dS -J )Nr\   r   TF)	hasattr
isinstancer   r
   tuplelistElementsMapreversedrf   r\   )r   targetvariant	exactNodelistElementsinvertr6   s          r   elementInScopezTreeBuilder.elementInScope   s     FK00	 	-&),, 6$V,f5fe,,,,,.w7fT.// 	 	D TV^^tt 4>V#;#;ttDNl:; uu 	r    c                    | j         sd S t          | j                   dz
  }| j         |         }|t          k    s	|| j        v rd S |t          k    r8|| j        vr/|dk    rd}n&|dz  }| j         |         }|t          k    r	|| j        v/	 |dz  }| j         |         }|                                }|                     d|j        |j        |j        d          }|| j         |<   || j         d         k    rd S m)NrS   r   rR   TStartTag)typer   	namespacer:   )	rg   lenrU   rf   rG   insertElementr   r|   r   )r   ientryclonerZ   s        r   #reconstructActiveFormattingElementsz/TreeBuilder.reconstructActiveFormattingElements   s;    , 	F -..2-a0F??et'888F voo%t/@"@"@AvvFA1!4E voo%t/@"@"@	FA 1!4EOO%%E ((*27*7<272B*D *D E EG 07D)!, $7;;;'	r    c                     | j                                         }| j         r8|t          k    r1| j                                         }| j         r|t          k    +d S d S d S d S rJ   )rg   poprU   )r   r   s     r   clearActiveFormattingElementsz)TreeBuilder.clearActiveFormattingElements  sn    -1133+ 	815577E + 	8 	8 	8 	8 	8r    c                 d    | j         ddd         D ]}|t          k    r n|j        |k    r|c S dS )zCheck if an element exists between the end of the active
        formatting elements and the last marker. If it does, return it, else
        return falseNrR   F)rg   rU   r   )r   r   items      r   !elementInActiveFormattingElementsz-TreeBuilder.elementInActiveFormattingElements  sQ    
 1$$B$7 	 	D v~~d"" #ur    c                     |                      |          }| j                            |           | j                            |           d S rJ   )createElementrf   rX   rl   r7   )r   tokenrZ   s      r   
insertRootzTreeBuilder.insertRoot  sH    $$U++  )))!!'*****r    c                     |d         }|d         }|d         }|                      |||          }| j                            |           d S )Nr   publicIdsystemId)doctypeClassrl   r7   )r   r   r   r   r   doctypes         r   insertDoctypezTreeBuilder.insertDoctype   sR    V}$$##D(H==!!'*****r    c                     || j         d         }|                    |                     |d                              d S )NrR   r:   )rf   r7   commentClass)r   r   r   s      r   insertCommentzTreeBuilder.insertComment(  s@    >&r*F4,,U6];;<<<<<r    c                     |d         }|                     d| j                  }|                     ||          }|d         |_        |S )z.Create an element but don't insert it anywherer   r|   r:   )getrb   elementClassr   r   r   r   r|   rZ   s        r   r   zTreeBuilder.createElement-  sG    V}IIk4+@AA	##D)44"6]r    c                     | j         S rJ   )_insertFromTabler0   s    r   _getInsertFromTablezTreeBuilder._getInsertFromTable5  s    $$r    c                 L    || _         |r| j        | _        dS | j        | _        dS )zsSwitch the function used to insert an element from the
        normal one to the misnested table one and back againN)r   insertElementTabler~   insertElementNormal)r   r   s     r   _setInsertFromTablezTreeBuilder._setInsertFromTable8  s6     !& 	:!%!8D!%!9Dr    c                 J   |d         }t          |t                    sJ d|z              |                    d| j                  }|                     ||          }|d         |_        | j        d                             |           | j                            |           |S )Nr   zElement %s not unicoder|   r:   rR   )	ro   r   r   rb   r   r   rf   r7   rX   r   s        r   r   zTreeBuilder.insertElementNormalC  s    V}$	**KK,Dt,KKKKIIk4+@AA	##D)44"6]"))'222  )))r    c                 J   |                      |          }| j        d         j        t          vr|                     |          S |                                 \  }}||                    |           n|                    ||           | j                            |           |S )z-Create an element and insert it into the treerR   )	r   rf   r   r	   r   getTableMisnestedNodePositionr7   r;   rX   )r   r   rZ   r   r;   s        r   r   zTreeBuilder.insertElementTableM  s    $$U++R %-DDD++E222 $(#E#E#G#G FL#""7++++##G\:::$$W---r    c                     || j         d         }| j        r | j        r0| j         d         j        t          vr|                    |           dS |                                 \  }}|                    ||           dS )zInsert text data.NrR   )rf   rj   r   r	   r<   r   )r   r:   r   r;   s       r   r<   zTreeBuilder.insertText]  s    >&r*F$ 	2)= 	2)-):2)>)C0G*H *Hd##### $(#E#E#G#G FLdL11111r    c                     d}d}d}| j         ddd         D ]}|j        dk    r|} n|r:|j        r
|j        }|}n6| j         | j                             |          dz
           }n| j         d         }||fS )zsGet the foster parent element, and sibling to insert before
        (or None) when inserting a misnested table nodeNrR   r   rS   r   )rf   r   r   index)r   	lastTablefosterParentr;   elms        r   r   z)TreeBuilder.getTableMisnestedNodePositionl  s     	$TTrT* 	 	Cx7""	 #  
	0  <(/(#0%++I66: <  ,Q/L\))r    c                     | j         d         j        }|t          d          v r6||k    r2| j                                          |                     |           d S d S d S )NrR   )dddtlir   r   prprt)rf   r   	frozensetr   generateImpliedEndTags)r   excluder   s      r   r   z"TreeBuilder.generateImpliedEndTags  sn     $)IWXXXX!!### ''00000 YXr    c                     | j         S )zReturn the final tree)rl   r0   s    r   getDocumentzTreeBuilder.getDocument  s
    }r    c                 n    |                                  }| j        d                             |           |S )zReturn the final fragmentr   )fragmentClassrf   rE   )r   fragments     r   getFragmentzTreeBuilder.getFragment  s6     %%''!--h777r    c                     t           )zSerialize the subtree of node in the format required by unit tests

        :arg node: the node from which to start serializing

        r3   r5   s     r   testSerializerzTreeBuilder.testSerializer  r8   r    rJ   )rK   rL   rM   rN   rk   r   r   r   r   r   rc   rx   r   r   r   r   r   r   r   r   r   propertyrj   r   r   r<   r   r   r   r   r   r%   r    r   r`   r`      s         M L L L M
 
 

- 
- 
-   ,, , ,\8 8 8
  + + +
+ + += = = =
  % % %: : : h24GHHO     2 2 2 2* * *41 1 1 1    " " " " "r    r`   )
__future__r   r   r   sixr   	constantsr   r	   r
   rU   r   rq   objectr   r   rP   r`   r%   r    r   <module>r      s   B B B B B B B B B B       L L L L L L L L L L
 
 9_%%u
-yZ-?,J+KKLLeTY:f+=t*D+5f+=t*D*F F G GHMOi*V,f5$V,g68 9 9:?Ay:f-z:%f-x8: ; ;<@B	 	`" `" `" `" `"6 `" `" `"F    t   0O" O" O" O" O"& O" O" O" O" O"r    