3 �Pfv6�@s�ddlmZmZmZddlmZddlmZmZm Z dZ e e�dfe ee e ddfg�B�dfe ee e dd fe dd fg�B�dfe e ddfe dd fg�dfe e dd fe dd fg�dfd�Z Gdd�de�ZGdd�de�ZGdd�de�ZdS)�)�absolute_import�division�unicode_literals)� text_type�)�scopingElements�tableInsertModeElements� namespacesNF�html�buttonZolZul�table�optgroup�optionT)Nr �listr Zselectc@s^eZdZdd�Zdd�Zdd�Zdd�Zdd d �Zd d �Zdd�Z dd�Z dd�Z dd�Z d S)�NodecCs(||_d|_d|_i|_g|_g|_dS)a6Node representing an item in the tree. name - The tag name associated with the node parent - The parent of the current node (or None for the document node) value - The value of the current node (applies to text nodes and comments attributes - a dict holding name, value pairs for attributes of the node childNodes - a list of child nodes of the current node. This must include all elements but not necessarily other node types _flags - A list of miscellaneous flags that can be set on the node N)�name�parent�value� attributes� childNodesZ_flags)�selfr�r�/usr/lib/python3.6/base.py�__init__s z Node.__init__cCs:djdd�|jj�D��}|r,d|j|fSd|jSdS)N� cSsg|]\}}d||f�qS)z%s="%s"r)�.0rrrrr� +sz Node.__str__..z<%s %s>z<%s>)�joinr�itemsr)rZ attributesStrrrr�__str__*s  z Node.__str__cCs d|jS)Nz<%s>)r)rrrr�__repr__3sz Node.__repr__cCst�dS)z3Insert node as a child of the current node N)�NotImplementedError)r�noderrr� appendChild6szNode.appendChildNcCst�dS)z�Insert data as text in the current node, positioned before the start of node insertBefore or to the end of the node's text. N)r!)r�data� insertBeforerrr� insertText;szNode.insertTextcCst�dS)z�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 nodeN)r!)rr"ZrefNoderrrr%AszNode.insertBeforecCst�dS)z:Remove node from the children of the current node N)r!)rr"rrr� removeChildGszNode.removeChildcCs$x|jD]}|j|�qWg|_dS)z�Move 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 N)rr#)rZ newParentZchildrrr�reparentChildrenLs zNode.reparentChildrencCst�dS)z�Return a shallow copy of the current node i.e. a node with the same name and attributes but with no parent or child nodes N)r!)rrrr� cloneNodeVszNode.cloneNodecCst�dS)zFReturn true if the node has children or text, false otherwise N)r!)rrrr� hasContent\szNode.hasContent)N) �__name__� __module__� __qualname__rrr r#r&r%r'r(r)r*rrrrrs   rc@seZdZdd�Zdd�ZdS)�ActiveFormattingElementscCsfd}|tkrVxH|ddd�D]6}|tkr*P|j||�r>|d7}|dkr|j|�PqWtj||�dS)Nr�������)�Marker� nodesEqual�remover�append)rr"Z equalCount�elementrrrr5cs  zActiveFormattingElements.appendcCs$|j|jksdS|j|jks dSdS)NFT)� nameTupler)rZnode1Znode2rrrr3ps   z#ActiveFormattingElements.nodesEqualN)r+r,r-r5r3rrrrr.bs r.c@s�eZdZdZdZdZdZdZdZdd�Z dd�Z d+dd�Z d d �Z d d �Z d d�Zdd�Zdd�Zd,dd�Zdd�Zdd�Zdd�Zeee�Zdd�Zdd�Zd-dd �Zd!d"�Zd.d#d$�Zd%d&�Zd'd(�Zd)d*�ZdS)/� TreeBuilderaBase 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 NcCs|r d|_nd|_|j�dS)Nzhttp://www.w3.org/1999/xhtml)�defaultNamespace�reset)rZnamespaceHTMLElementsrrrr�szTreeBuilder.__init__cCs.g|_t�|_d|_d|_d|_|j�|_dS)NF)� openElementsr.�activeFormattingElementsZ headPointerZ formPointer�insertFromTable� documentClass�document)rrrrr:�s zTreeBuilder.resetcCs�t|d�}|s2t|t�r$td|f}t|t�s2t�t|\}}xHt|j�D]:}|r^||kr^dS| rr|j |krrdS||j |kArJdSqJWds�t�dS)Nr7r TF) �hasattr� isinstancerr �tuple�AssertionError�listElementsMap�reversedr;r7)r�targetZvariantZ exactNodeZ listElements�invertr"rrr�elementInScope�s     zTreeBuilder.elementInScopecCs�|js dSt|j�d}|j|}|tks4||jkr8dSx6|tkrn||jkrn|dkrZd}P|d8}|j|}q:WxR|d7}|j|}|j�}|jd|j|j|jd��}||j|<||jdkrrPqrWdS)Nr/rZStartTag)�typer� namespacer$r1r1) r<�lenr2r;r)� insertElementrrJr)r�i�entryZcloner6rrr�#reconstructActiveFormattingElements�s.    z/TreeBuilder.reconstructActiveFormattingElementscCs,|jj�}x|jr&|tkr&|jj�}q WdS)N)r<�popr2)rrNrrr�clearActiveFormattingElements�s z)TreeBuilder.clearActiveFormattingElementscCs8x2|jddd�D]}|tkr"Pq|j|kr|SqWdS)z�Check if an element exists between the end of the active formatting elements and the last marker. If it does, return it, else return falseNr/Fr1)r<r2r)rr�itemrrr�!elementInActiveFormattingElements�s  z-TreeBuilder.elementInActiveFormattingElementscCs&|j|�}|jj|�|jj|�dS)N)� createElementr;r5r?r#)r�tokenr6rrr� insertRoot�s  zTreeBuilder.insertRootcCs6|d}|d}|d}|j|||�}|jj|�dS)Nr�publicId�systemId)� doctypeClassr?r#)rrUrrWrXZdoctyperrr� insertDoctypes zTreeBuilder.insertDoctypecCs*|dkr|jd}|j|j|d��dS)Nr/r$r1)r;r#� commentClass)rrUrrrr� insertComment s zTreeBuilder.insertCommentcCs0|d}|jd|j�}|j||�}|d|_|S)z.Create an element but don't insert it anywhererrJr$)�getr9� elementClassr)rrUrrJr6rrrrTs   zTreeBuilder.createElementcCs|jS)N)�_insertFromTable)rrrr�_getInsertFromTableszTreeBuilder._getInsertFromTablecCs ||_|r|j|_n|j|_dS)zsSwitch the function used to insert an element from the normal one to the misnested table one and back againN)r_�insertElementTablerL�insertElementNormal)rrrrr�_setInsertFromTables zTreeBuilder._setInsertFromTablecCsb|d}t|t�std|��|jd|j�}|j||�}|d|_|jdj|�|jj |�|S)NrzElement %s not unicoderJr$r/r1) rArrCr]r9r^rr;r#r5)rrUrrJr6rrrrb$s   zTreeBuilder.insertElementNormalcCs`|j|�}|jdjtkr$|j|�S|j�\}}|dkrD|j|�n |j||�|jj|�|S)z-Create an element and insert it into the treer/Nr1) rTr;rrrb�getTableMisnestedNodePositionr#r%r5)rrUr6rr%rrrra.s      zTreeBuilder.insertElementTablecCsX|dkr|jd}|j s0|jr<|jdjtkr<|j|�n|j�\}}|j||�dS)zInsert text data.Nr/r1r1)r;r=rrr&rd)rr$rr%rrrr&>s    zTreeBuilder.insertTextcCsvd}d}d}x(|jddd�D]}|jdkr|}PqW|rd|jrL|j}|}qn|j|jj|�d}n |jd}||fS)zsGet the foster parent element, and sibling to insert before (or None) when inserting a misnested table nodeNr/r rr1)r;rr�index)rZ lastTableZ fosterParentr%ZelmrrrrdMs  z)TreeBuilder.getTableMisnestedNodePositionc Cs8|jd j}|td �kr4||kr4|jj�|j|�dS) Nr/�dd�dt�lirr �p�rp�rtr1)rfrgrhrr rirjrk)r;r� frozensetrP�generateImpliedEndTags)r�excluderrrrrmgs    z"TreeBuilder.generateImpliedEndTagscCs|jS)zReturn the final tree)r?)rrrr� getDocumentqszTreeBuilder.getDocumentcCs|j�}|jdj|�|S)zReturn the final fragmentr)� fragmentClassr;r()rZfragmentrrr� getFragmentuszTreeBuilder.getFragmentcCst�dS)zzSerialize the subtree of node in the format required by unit tests node - the node from which to start serializingN)r!)rr"rrr�testSerializer|szTreeBuilder.testSerializer)N)N)N)N)r+r,r-�__doc__r>r^r[rYrprr:rHrOrQrSrVrZr\rTr`rc�propertyr=rbrar&rdrmrorqrrrrrrr8zs6 .     r8)Z __future__rrrZpip._vendor.sixrZ constantsrrr r2rl�setrD�objectrrr.r8rrrr�s    K