
    V=^8              	       >   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y)    )absolute_importdivisionunicode_literals)	text_type   )scopingElementstableInsertModeElements
namespacesNFhtmlbuttonolultableoptgroupoptionT)Nr   listr   selectc                   N    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y)NodezRepresents an item in the treec                 X    || _         d| _        d| _        i | _        g | _        g | _        y)zRCreates a Node

        :arg name: The tag name associated with the node

        N)nameparentvalue
attributes
childNodes_flags)selfr   s     </usr/lib/python3/dist-packages/html5lib/treebuilders/base.py__init__zNode.__init__   s0     	
     c           
          dj                  | j                  j                         D cg c]  \  }}|d|d c}}      }|rd| j                  d|dS d| j                  z  S c c}}w )N z=""<><%s>)joinr   itemsr   )r   r   r   attributesStrs       r   __str__zNode.__str__-   sd    "&//"7"7"9";&1dE 26u"= "; <  $		=99TYY''";s   A'
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

        r/   )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

        r/   )r   r2   refNodes      r   r7   zNode.insertBeforeM   r9   r    c                     t         )zhRemove node from the children of the current node

        :arg node: the child node to remove

        r/   r1   s     r   removeChildzNode.removeChildY   r4   r    c                 V    | j                   D ]  }|j                  |        g | _         y)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   r3   )r   	newParentchilds      r   reparentChildrenzNode.reparentChildrena   s,     __ 	)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
        r/   r,   s    r   	cloneNodezNode.cloneNoden   s
     "!r    c                     t         )zFReturn true if the node has children or text, false otherwise
        r/   r,   s    r   
hasContentzNode.hasContentt   s
     "!r    N)__name__
__module____qualname____doc__r   r*   r-   r3   r8   r7   r=   rA   rC   rE    r    r   r   r      s7    ((($"
"
""""r    r   c                       e Zd Zd Zd Zy)ActiveFormattingElementsc                     d}|t         k7  rG| d d d   D ]<  }|t         k(  r n1| j                  ||      r|dz  }|dk(  s+| j                  |        n t        j	                  | |       y )Nr         )Marker
nodesEqualremover   append)r   r2   
equalCountelements       r   rU   zActiveFormattingElements.append{   sm    
6>": f$??7D1!OJ?KK( 	D$r    c                 l    |j                   |j                   k(  sy|j                  |j                  k(  syy)NFT)	nameTupler   )r   node1node2s      r   rS   z#ActiveFormattingElements.nodesEqual   s/    %//15#3#33r    N)rG   rH   rI   rU   rS   rK   r    r   rM   rM   z   s     r    rM   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y)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                 F    |rd| _         nd| _         | j                          y)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__   s      !$BD!$(D!

r    c                     g | _         t               | _        d | _        d | _        d| _        | j                         | _        y )NF)openElementsrM   activeFormattingElementsheadPointerformPointerinsertFromTabledocumentClassdocumentr,   s    r   r`   zTreeBuilder.reset   s?    (@(B%  $**,r    c                 &   t        |d      }|s-t        |t              rt        d   |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 )NrY   r   TF)	hasattr
isinstancer   r
   tuplelistElementsMapreversedrc   rY   )r   targetvariant	exactNodelistElementsinvertr2   s          r   elementInScopezTreeBuilder.elementInScope   s     FK0	&),$V,f5fe,,,.w7fT../ 	DTV^4>>V#;DNNl:;	 	ur    c                 (   | j                   sy t        | j                         dz
  }| j                   |   }|t        k(  s|| j                  v ry |t        k7  rB|| j                  vr4|dk(  rd}n,|dz  }| j                   |   }|t        k7  r|| j                  vr4	 |dz  }| j                   |   }|j	                         }| j                  d|j                  |j                  |j                  d      }|| j                   |<   || j                   d   k(  ry {)NrP   r   rO   StartTag)typer   	namespacer6   )	rd   lenrR   rc   rC   insertElementr   ry   r   )r   ientryclonerW   s        r   #reconstructActiveFormattingElementsz/TreeBuilder.reconstructActiveFormattingElements   s/    ,, --.2--a0F?et'8'88 vo%t/@/@"@AvFA11!4E vo%t/@/@"@ FA 11!4EOO%E ((*27**7<272B2B*D EG 07D))!, $77;;' r    c                     | j                   j                         }| j                   r<|t        k7  r2| j                   j                         }| j                   r|t        k7  r0y y y y rF   )rd   poprR   )r   r}   s     r   clearActiveFormattingElementsz)TreeBuilder.clearActiveFormattingElements  sR    --113++11557E ++++r    c                 n    | j                   ddd   D ]   }|t        k(  r y|j                  |k(  s|c S  y)zCheck if an element exists between the end of the active
        formatting elements and the last marker. If it does, return it, else
        return falseNrO   F)rd   rR   r   )r   r   items      r   !elementInActiveFormattingElementsz-TreeBuilder.elementInActiveFormattingElements  sK    
 11$B$7 	D v~  d"	 r    c                     | j                  |      }| j                  j                  |       | j                  j	                  |       y rF   )createElementrc   rU   ri   r3   )r   tokenrW   s      r   
insertRootzTreeBuilder.insertRoot  s8    $$U+  )!!'*r    c                 ~    |d   }|d   }|d   }| j                  |||      }| j                  j                  |       y )Nr   publicIdsystemId)doctypeClassri   r3   )r   r   r   r   r   doctypes         r   insertDoctypezTreeBuilder.insertDoctype   sE    V}$$##D(H=!!'*r    c                 l    || j                   d   }|j                  | j                  |d                y )NrO   r6   )rc   r3   commentClass)r   r   r   s      r   insertCommentzTreeBuilder.insertComment(  s4    >&&r*F4,,U6];<r    c                     |d   }|j                  d| j                        }| j                  ||      }|d   |_        |S )z.Create an element but don't insert it anywherer   ry   r6   )getr_   elementClassr   r   r   r   ry   rW   s        r   r   zTreeBuilder.createElement-  sD    V}IIk4+@+@A	##D)4"6]r    c                     | j                   S rF   )_insertFromTabler,   s    r   _getInsertFromTablezTreeBuilder._getInsertFromTable5  s    $$$r    c                 \    || _         |r| j                  | _        y| j                  | _        y)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  s+     !&!%!8!8D!%!9!9Dr    c                 &   |d   }t        |t              s
J d|z         |j                  d| j                        }| j	                  ||      }|d   |_        | j                  d   j                  |       | j                  j                  |       |S )Nr   zElement %s not unicodery   r6   rO   )	rl   r   r   r_   r   r   rc   r3   rU   r   s        r   r   zTreeBuilder.insertElementNormalC  s    V}$	*K,Dt,KK*IIk4+@+@A	##D)4"6]"))'2  )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 treerO   )	r   rc   r   r	   r   getTableMisnestedNodePositionr3   r7   rU   )r   r   rW   r   r7   s        r   r   zTreeBuilder.insertElementTableM  s    $$U+R %%-DD++E22 $(#E#E#G FL#""7+##G\:$$W-r    c                    || j                   d   }| j                  r+| j                  r1| j                   d   j                  t        vr|j	                  |       y| j                         \  }}|j	                  ||       y)zInsert text data.NrO   )rc   rg   r   r	   r8   r   )r   r6   r   r7   s       r   r8   zTreeBuilder.insertText]  sx    >&&r*F$$)=)=)-):):2)>)C)C0G*Hd# $(#E#E#G FLdL1r    c                 &   d}d}d}| j                   ddd   D ]  }|j                  dk(  s|} n |rM|j                  r|j                  }|}||fS | j                   | j                   j                  |      dz
     }||fS | j                   d   }||fS )zsGet the foster parent element, and sibling to insert before
        (or None) when inserting a misnested table nodeNrO   r   rP   r   )rc   r   r   index)r   	lastTablefosterParentr7   elms        r   r   z)TreeBuilder.getTableMisnestedNodePositionl  s     	$$TrT* 	Cxx7"		  (//( \))	  $00%%++I6: < \))  ,,Q/L\))r    c                     | j                   d   j                  }|t        d      v r2||k7  r,| j                   j                          | j	                  |       y y y )NrO   )dddtlir   r   prprt)rc   r   	frozensetr   generateImpliedEndTags)r   excluder   s      r   r   z"TreeBuilder.generateImpliedEndTags  sZ      $))IWXX!!# ''0	   Yr    c                     | j                   S )zReturn the final tree)ri   r,   s    r   getDocumentzTreeBuilder.getDocument  s    }}r    c                 b    | j                         }| j                  d   j                  |       |S )zReturn the final fragmentr   )fragmentClassrc   rA   )r   fragments     r   getFragmentzTreeBuilder.getFragment  s0     %%'!--h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

        r/   r1   s     r   testSerializerzTreeBuilder.testSerializer  r4   r    rF   )rG   rH   rI   rJ   rh   r   r   r   r   r   r`   ru   r   r   r   r   r   r   r   r   r   propertyrg   r   r   r8   r   r   r   r   r   rK   r    r   r]   r]      s     M L L L M

-,,\8
+
+=
%: 24GHO 2*41"r    r]   )
__future__r   r   r   sixr   	constantsr   r	   r
   rR   r   rn   objectr   r   rM   r]   rK   r    r   <module>r      s   B B  L L
 
 _%u
-Z-?,J+KKLeT:f+=t*D+5f+=t*D*F F GHMO*V,f5$V,g68 9:?A:f-z:%f-x8: ;<@B	`"6 `"Ft 0O"& O"r    