o bf"@sdZddlmZddlmZddlmZmZmZeeGddde Z ddZ d9d d Z d d Z ddZddZddZd:ddZddZddZddZd;ddZGd d!d!ejZd A library for performing interesting tasks with DOM objects. )StringIO)microdom)escapegetElementsByTagNameunescapec@s eZdZdS)NodeLookupErrorN)__name__ __module__ __qualname__r r 8/usr/lib/python3/dist-packages/twisted/web/domhelpers.pyrsrcCsF|jD]}t|dr|jr|dt|j|j|t|||qdS)z Look through the given node's children for strings, and attempt to do string substitution with the given parameter. nodeValuerN) childNodeshasattrr replaceDatalen substitute)requestnodesubschildr r r rs ridclassmodelpatterncCst|dr|r|D]}t|||kr|Sq |rDt|jdr*|jj}nt|j}t|D]}t |j||}|rC|Sq3dSdS)zx (internal) Get a node with the specified C{nodeId} as any of the C{class}, C{id} or C{pattern} attributes. hasAttributeslengthN) rrstr getAttribute hasChildNodesrrrrange_get)rnodeId nodeAttrsnodeAttrrchildNumresultr r r r""s     r"cCst||}|r |St|)z Get a node with the specified C{nodeId} as any of the C{class}, C{id} or C{pattern} attributes. If there is no such node, raise L{NodeLookupError}. )r"rrr#r'r r r get7s r)cCs t||S)z Get a node with the specified C{nodeId} as any of the C{class}, C{id} or C{pattern} attributes. If there is no such node, return L{None}. )r")rr#r r r getIfExistsCs r*cCst||}|r t||S)zGet a node with the specified C{nodeId} as any of the C{class}, C{id} or C{pattern} attributes. If there is no such node, raise L{NodeLookupError}. Remove all child nodes before returning. )r) clearNoder(r r r getAndClearLs r,cCsg|jdd<dS)z2 Remove all children from the given node. Nr)rr r r r+Wsr+cCsrg}t|tgst|j|||S|D]#}t|dsqt|||kr,|||r,q|t||||q|S)z\ Find subnodes in the given node where the given attribute has the given value. r) isinstancetype locateNodesrrrrappendextend)nodeListkeyvalue noNesting returnList childNoder r r r1^s  r1cCsBt|dsdS||||r|jD] }t|||qdSdS)N setAttribute)rr:r rsuperSetAttribute)rr5r6rr r r r;qs   r;cCsft|dsdS||}|r|||d|n||||r/|jD] }t|||q&dSdSNr:/)rrr:r rsuperPrependAttributerr5r6oldrr r r r>z    r>cCsft|dsdS||}|r|||d|n||||r/|jD] }t|||q&dSdSr<)rrr:r rsuperAppendAttributer?r r r rBrArBcCs|g}|j}|g}t|dkr9|d}t|dr,|jdur,|r%t|j}n|j}|||j|dd<t|dks||S)ahVisit each child node and collect its text data, if any, into a string. For example:: >>> doc=microdom.parseString('1234') >>> gatherTextNodes(doc.documentElement) '1234' With dounescape=1, also convert entities back into normal characters. @return: the gathered nodes as a single string @rtype: strrr N)r2rpoprr rrjoin)iNode dounescapejoinWithgatheredgathered_appendslicecvalr r r gatherTextNodess     rNc@s&eZdZdZ      dddZdS)RawTexta7This is an evil and horrible speed hack. Basically, if you have a big chunk of XML that you want to insert into the DOM, but you don't want to incur the cost of parsing it, you can construct one of these and insert it into the DOM. This will most certainly only work with microdom as the API for converting nodes to xml is different in every DOM implementation. This could be improved by making this class a Lazy parser, so if you inserted this into the DOM and then later actually tried to mutate this node, it would be parsed then. rCrNcCs|||j|dSN)writedata)selfwriterindent addindentnewlstrip nsprefixes namespacer r r writexmls zRawText.writexml)rCrCrCrNN)rr r __doc__r[r r r r rOsrONcCsF|durg}|s |S|jD]}||r||t|||q|SrPr rr2 findNodesparentmatcheraccumrr r r r^s  r^cCsP|durg}|s |S|jD]}||r||||r%t||||q|SrP)r rr2findNodesShallowOnMatch)r`rarecurseMatcherrbrr r r rcs  rccCsH|durg}|s |S|jD]}||r||qt|||q|SrPr]r_r r r findNodesShallows  recst|fddS)z Return an iterable of the elements which are direct children of C{parent} and which have the C{attribute} attribute. cst|ddduo |SNtagName)getattr hasAttribute)n attributer r sz2findElementsWithAttributeShallow..)re)r`rlr rkr findElementsWithAttributeShallow rncCt||fddS)z Return an iterable of the elements which are children of C{parent} for which the predicate C{matcher} returns true. cSst|ddduo ||Srfrh)rjrar r r rmszfindElements..r^)r`rar r r findElementsrorscCs*|r t|||fddSt||fddS)NcSs||o |||kSrP)rir)rjrlr6r r r rms z+findElementsWithAttribute..cSs ||SrP)ri)rjrlr r r rm  )rs)r`rlr6r r r findElementsWithAttributes  rucCrp)NcSs |j|kSrPnodeName)rjnamer r r rmrtz findNodesNamed..rr)r`rxr r r findNodesNamedsrycCs6|jD]}t|dr|d|jqt||qdS)NrRrC)rrrQrR writeNodeData)roldiosubnoder r r rzs   rzcCst}t|||SrP)rrzgetvalue)rr{r r r getNodeTexts r~cCs g}|r|||j}|s|SrP)r2 parentNode)rlr r r getParents#s  rcsfdd|jDS)znnamedChildren(parent, nodeName) -> children (not descendants) of parent that have tagName == nodeName cs g|] }t|ddkr|qS)rgrCrq).0rjrvr r /s z!namedChildren..r-)r`rwr rvr namedChildren+sr)r)r.)rrCrP)#r\ior twisted.webrtwisted.web.microdomrrr Exceptionrrr"r)r*r,r+r1r;r>rBrNTextrOr^rcrernrsruryrzr~rrr r r r s<