m O,Ac@spdklZdklZdklZlZlZy dkZWne j odkZnXde fdYZ dZ d%d Z d Zd ZdZdZddZdZdZdZdddZdeifdYZddZddZddZdZdZddZd Z d!Z!d"Z"d#Z#d$Z$dS(&(s nested_scopes(smicrodom(sgetElementsByTagNamesescapesunescapeNtNodeLookupErrorcBstZRS(N(t__name__t __module__(((tn/home/radix/Projects/Twisted/branches/releases/twisted-core-2.5.x-2329-2/Twisted.exp/twisted/web/domhelpers.pyRscCsixb|iD]W}t|do1|io'|idt|i|i|nt|||q WdS(s Look through the given node's children for strings, and attempt to do string substitution with the given parameter. t nodeValueiN( tnodet childNodestchildthasattrRt replaceDatatlentsubst substitutetrequest(R RR R((RR s  'tidtclasstmodeltpatterncCst|doF|io9x6|D]*}t|i||jo|Sq$q$Wn|iort|i do|i i }nt |i }x=t |D]+}t|i ||}|o|SqqWndS(sx (internal) Get a node with the specified C{nodeId} as any of the C{class}, C{id} or C{pattern} attributes. t hasAttributestlengthN(RRRt nodeAttrstnodeAttrtstrt getAttributetnodeIdt hasChildNodesRRR trangetchildNumt_gettresult(RRRRRRR((RRs  cCs+t||}|o|Snt|dS(s 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}. N(RRRRR(RRR((Rtget1s cCs t||S(s 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 C{None}. N(RRR(RR((Rt getIfExists;scCs(t||}|ot|n|S(sGet 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. N(RRRRt clearNode(RRR((Rt getAndClearCs cCsg|i(dS(s2 Remove all children from the given node. N(RR(R((RR MsicCsg}t|tgpt|i|||Snx||D]t}t |dpq=nt |i ||jo|i ||oq=qn|it||||q=W|S(s\ Find subnodes in the given node where the given attribute has the given value. RN(t returnListt isinstancetnodeListttypet locateNodesRtkeytvaluet noNestingt childNodeRRRtappendtextend(R$R'R(R)R"R*((RR&Ss   cCsat|dpdSn|i|||io(x%|iD]}t|||q?WndS(Nt setAttribute( RRR-R'R(RRRtsuperSetAttribute(RR'R(R((RR.es  cCst|dpdSn|i|}|o|i||d|n|i|||io(x%|iD]}t |||qqWndS(NR-t/( RRRR'toldR-R(RRRtsuperPrependAttribute(RR'R(R0R((RR1ls  cCst|dpdSn|i|}|o|i||d|n|i|||io(x%|iD]}t |||qqWndS(NR-R/( RRRR'R0R-R(RRRtsuperAppendAttribute(RR'R(R0R((RR2ws  itcCsg}|i}|g}xt|djoq|id}t|doA|i dj o1|ot |i }n |i }||n|i|d*qW|i|S(sMVisit 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: str iRN(tgatheredR+tgathered_appendtiNodetsliceR tpoptcRRtNonet dounescapetunescapetvalRtjoinWithtjoin(R6R;R>R9R7R=R5R4((RtgatherTextNodess     tRawTextcBs)tZdZddddeedZRS(s7This 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. R3icCs!|id||i|fdS(Ns%s%s%s(twritertwritetindenttselftdatatnewl(RERBRDt addindentRGtstript nsprefixest namespace((Rtwritexmls(RRt__doc__R:RL(((RRAs cCsr|djo g}n|ip|Snx?|iD]4}||o|i|nt|||q6W|S(N( taccumR:tparentRRRtmatcherR+t findNodes(RORPRNR((RRQs     cCs|djo g}n|ip|SnxS|iD]H}||o|i|n||ot ||||q6q6W|S(N( RNR:RORRRRPR+trecurseMatchertfindNodesShallowOnMatch(RORPRRRNR((RRSs      cCsr|djo g}n|ip|Snx?|iD]4}||o|i|q6t|||q6W|S(N( RNR:RORRRRPR+RQ(RORPRNR((RtfindNodesShallows     cst|dS(Ncs t|tio |iS(N(R#tntmicrodomtElementt hasAttributet attribute(RU(RY(Rts(RTRO(RORY((RYRt findElementsWithAttributeShallowscCst||dS(NcCst|tio ||S(N(R#RURVRWRP(RURP((RRZs(RQRORP(RORP((Rt findElementsscCs8|ot|||dSnt||dSdS(NcCs#|i|o|i||jS(N(RURXRYRR((RURYR(((RRZscCs |i|S(N(RURXRY(RURY((RRZs(R(R\RORY(RORYR(((RtfindElementsWithAttributes cCst||dS(NcCs |i|jS(N(RUtnodeNametname(RUR_((RRZs(RQROR_(ROR_((RtfindNodesNamedscCsOxH|iD]=}t|do|it|iq t||q WdS(NRF( RRtsubnodeRtoldioRCRRFt writeNodeData(RRbRa((RRcs  cCs#ti}t|||iS(N(tStringIORbRcRtgetvalue(RRb((Rt getNodeTexts  cCs/g}x"|o|i||i}q W|S(N(tlRR+t parentNode(RRg((Rt getParentss  cCs?g}|iD]*}t|dd|jo ||qq~S(snnamedChildren(parent, nodeName) -> children (not descendants) of parent that have tagName == nodeName ttagNameR3N(t_[1]RORRUtgetattrR^(ROR^RkRU((Rt namedChildrens(sidsclassRspattern(%t __future__t nested_scopest twisted.webRVtgetElementsByTagNametescapeR<t cStringIORdt ImportErrort ExceptionRR RRRR!R R&R.R1R2R@tTextRAR:RQRSRTR[R\R]R`RcRfRiRm(R@RARTRrR RR1R[RSR&RiRcRQR!RoRR\RqR.RRR`RmRfR]R<RdR R2RV((Rt?s<