3 Pfv6@sddlmZmZmZddlmZddlmZmZm Z dZ e edfe ee e ddfgBdfe ee e dd fe dd fgBdfe e ddfe dd fgdfe e dd fe dd fgdfdZ GdddeZGdddeZGdddeZdS))absolute_importdivisionunicode_literals) text_type)scopingElementstableInsertModeElements namespacesNFhtmlbuttonZolZultableoptgroupoptionT)Nr listr Zselectc@s^eZdZddZddZddZddZdd d Zd d ZddZ ddZ ddZ ddZ 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)nameparentvalue attributes childNodesZ_flags)selfrr/usr/lib/python3.6/base.py__init__s z Node.__init__cCs:djdd|jjD}|r,d|j|fSd|jSdS)N cSsg|]\}}d||fqS)z%s="%s"r).0rrrrr +sz Node.__str__..z<%s %s>z<%s>)joinritemsr)rZ attributesStrrrr__str__*s  z Node.__str__cCs d|jS)Nz<%s>)r)rrrr__repr__3sz Node.__repr__cCstdS)z3Insert node as a child of the current node N)NotImplementedError)rnoderrr appendChild6szNode.appendChildNcCstdS)zInsert 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!)rdata insertBeforerrr insertText;szNode.insertTextcCstdS)zInsert 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.insertBeforecCstdS)z:Remove node from the children of the current node N)r!)rr"rrr removeChildGszNode.removeChildcCs$x|jD]}|j|qWg|_dS)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 N)rr#)rZ newParentZchildrrrreparentChildrenLs zNode.reparentChildrencCstdS)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 N)r!)rrrr cloneNodeVszNode.cloneNodecCstdS)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@seZdZddZddZdS)ActiveFormattingElementscCsfd}|tkrVxH|dddD]6}|tkr*P|j||r>|d7}|dkr|j|PqWtj||dS)Nr)Marker nodesEqualremoverappend)rr"Z equalCountelementrrrr5cs  zActiveFormattingElements.appendcCs$|j|jksdS|j|jks dSdS)NFT) nameTupler)rZnode1Znode2rrrr3ps   z#ActiveFormattingElements.nodesEqualN)r+r,r-r5r3rrrrr.bs r.c@seZdZdZdZdZdZdZdZddZ ddZ d+ddZ d d Z d d Z d dZddZddZd,ddZddZddZddZeeeZddZddZd-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|_|jdS)Nzhttp://www.w3.org/1999/xhtml)defaultNamespacereset)rZnamespaceHTMLElementsrrrrszTreeBuilder.__init__cCs.g|_t|_d|_d|_d|_|j|_dS)NF) openElementsr.activeFormattingElementsZ headPointerZ formPointerinsertFromTable documentClassdocument)rrrrr:s zTreeBuilder.resetcCst|d}|s2t|tr$td|f}t|ts2tt|\}}xHt|jD]:}|r^||kr^dS| rr|j |krrdS||j |kArJdSqJWdstdS)Nr7r TF) hasattr isinstancerr tupleAssertionErrorlistElementsMapreversedr;r7)rtargetZvariantZ exactNodeZ listElementsinvertr"rrrelementInScopes     zTreeBuilder.elementInScopecCs|js dSt|jd}|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)rientryZcloner6rrr#reconstructActiveFormattingElementss.    z/TreeBuilder.reconstructActiveFormattingElementscCs,|jj}x|jr&|tkr&|jj}q WdS)N)r<popr2)rrNrrrclearActiveFormattingElementss z)TreeBuilder.clearActiveFormattingElementscCs8x2|jdddD]}|tkr"Pq|j|kr|SqWdS)zCheck 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)rritemrrr!elementInActiveFormattingElementss  z-TreeBuilder.elementInActiveFormattingElementscCs&|j|}|jj||jj|dS)N) createElementr;r5r?r#)rtokenr6rrr insertRoots  zTreeBuilder.insertRootcCs6|d}|d}|d}|j|||}|jj|dS)NrpublicIdsystemId) doctypeClassr?r#)rrUrrWrXZdoctyperrr insertDoctypes zTreeBuilder.insertDoctypecCs*|dkr|jd}|j|j|ddS)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_insertElementTablerLinsertElementNormal)rrrrr_setInsertFromTables zTreeBuilder._setInsertFromTablecCsb|d}t|tstd||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;rrrbgetTableMisnestedNodePositionr#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(|jdddD]}|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;rrindex)rZ lastTableZ fosterParentr%ZelmrrrrdMs  z)TreeBuilder.getTableMisnestedNodePositionc Cs8|jd j}|td kr4||kr4|jj|j|dS) Nr/dddtlirr prprtr1)rfrgrhrr rirjrk)r;r frozensetrPgenerateImpliedEndTags)rexcluderrrrrmgs    z"TreeBuilder.generateImpliedEndTagscCs|jS)zReturn the final tree)r?)rrrr getDocumentqszTreeBuilder.getDocumentcCs|j}|jdj||S)zReturn the final fragmentr) fragmentClassr;r()rZfragmentrrr getFragmentuszTreeBuilder.getFragmentcCstdS)zzSerialize the subtree of node in the format required by unit tests node - the node from which to start serializingN)r!)rr"rrrtestSerializer|szTreeBuilder.testSerializer)N)N)N)N)r+r,r-__doc__r>r^r[rYrprr:rHrOrQrSrVrZr\rTr`rcpropertyr=rbrar&rdrmrorqrrrrrrr8zs6 .     r8)Z __future__rrrZpip._vendor.sixrZ constantsrrr r2rlsetrDobjectrrr.r8rrrrs    K