o
    bf"                     @   s  d Z ddlmZ ddlmZ ddlmZmZmZ e e G dd de	Z
dd Zd9d
dZdd Zdd Zdd Zdd Zd:ddZdd Zdd Zdd Zd;ddZG d d! d!ejZd<d#d$Zd<d%d&Zd<d'd(Zd)d* Zd+d, Zd<d-d.Zd/d0 Zd1d2 Zd3d4 Z d5d6 Z!d7d8 Z"d"S )=z>
A library for performing interesting tasks with DOM objects.
    )StringIO)microdom)escapegetElementsByTagNameunescapec                   @   s   e Zd ZdS )NodeLookupErrorN)__name__
__module____qualname__ r   r   8/usr/lib/python3/dist-packages/twisted/web/domhelpers.pyr      s    r   c                 C   sF   |j D ]}t|dr|jr|dt|j|j|  t| || qdS )z
    Look through the given node's children for strings, and
    attempt to do string substitution with the given parameter.
    	nodeValuer   N)
childNodeshasattrr   replaceDatalen
substitute)requestnodesubschildr   r   r   r      s
   
r   idclassmodelpatternc                 C   s   t | dr|  r|D ]}t| ||kr|   S q|  rDt | jdr*| jj}nt| j}t|D ]}t	| j| |}|rC|  S q3dS dS )zx
    (internal) Get a node with the specified C{nodeId} as any of the C{class},
    C{id} or C{pattern} attributes.
    hasAttributeslengthN)
r   r   strgetAttributehasChildNodesr   r   r   range_get)r   nodeId	nodeAttrsnodeAttrr   childNumresultr   r   r   r"   "   s    

r"   c                 C   s   t | |}|r	|S t|)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"   r   r   r#   r'   r   r   r   get7   s   
r)   c                 C   s
   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"   )r   r#   r   r   r   getIfExistsC   s   
r*   c                 C   s   t | |}|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   getAndClearL   s   
r,   c                 C   s   g | j dd< dS )z2
    Remove all children from the given node.
    Nr   )r   r   r   r   r+   W   s   r+      c                 C   sr   g }t | tg st| j|||S | D ]#}t|dsqt|||kr,|| |r,q|t|||| q|S )z\
    Find subnodes in the given node where the given attribute
    has the given value.
    r   )	
isinstancetypelocateNodesr   r   r   r   appendextend)nodeListkeyvalue	noNesting
returnList	childNoder   r   r   r1   ^   s   

r1   c                 C   sB   t | dsd S | || |  r| jD ]
}t||| qd S d S )NsetAttribute)r   r:   r    r   superSetAttribute)r   r5   r6   r   r   r   r   r;   q   s   

r;   c                 C   sf   t | dsd S | |}|r| ||d |  n| || |  r/| jD ]
}t||| q&d S d S Nr:   /)r   r   r:   r    r   superPrependAttributer   r5   r6   oldr   r   r   r   r>   z      


r>   c                 C   sf   t | dsd S | |}|r| ||d |  n| || |  r/| jD ]
}t||| q&d S d S r<   )r   r   r:   r    r   superAppendAttributer?   r   r   r   rB      rA   rB    c                 C   s|   g }|j }| g}t|dkr9|d}t|dr,|jdur,|r%t|j}n|j}|| |j|dd< t|dks||S )ah  Visit each child node and collect its text data, if any, into a string.
    For example::
        >>> doc=microdom.parseString('<a>1<b>2<c>3</c>4</b></a>')
        >>> gatherTextNodes(doc.documentElement)
        '1234'
    With dounescape=1, also convert entities back into normal characters.
    @return: the gathered nodes as a single string
    @rtype: strr   r   N)r2   r   popr   r   r   r   join)iNode
dounescapejoinWithgatheredgathered_appendslicecvalr   r   r   gatherTextNodes   s   	

	rN   c                   @   s&   e Zd ZdZ						dddZdS )RawTexta7  This 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.
    rC   r   Nc                 C   s   | | | j |  d S N)writedata)selfwriterindent	addindentnewlstrip
nsprefixes	namespacer   r   r   writexml   s   
zRawText.writexml)rC   rC   rC   r   NN)r   r	   r
   __doc__r[   r   r   r   r   rO      s    rO   Nc                 C   sF   |d u rg }|   s|S | jD ]}||r|| t||| q|S rP   r    r   r2   	findNodesparentmatcheraccumr   r   r   r   r^      s   

r^   c                 C   sP   |d u rg }|   s|S | jD ]}||r|| ||r%t|||| q|S rP   )r    r   r2   findNodesShallowOnMatch)r`   ra   recurseMatcherrb   r   r   r   r   rc      s   

rc   c                 C   sH   |d u rg }|   s|S | jD ]}||r|| qt||| q|S rP   r]   r_   r   r   r   findNodesShallow   s   
re   c                    s   t |  fddS )z
    Return an iterable of the elements which are direct children of C{parent}
    and which have the C{attribute} attribute.
    c                    s   t | dd d uo|  S NtagName)getattrhasAttribute)n	attributer   r   <lambda>   s    z2findElementsWithAttributeShallow.<locals>.<lambda>)re   )r`   rl   r   rk   r    findElementsWithAttributeShallow      
rn   c                 C      t | |fddS )z
    Return an iterable of the elements which are children of C{parent} for
    which the predicate C{matcher} returns true.
    c                 S   s   t | dd d uo|| S rf   rh   )rj   ra   r   r   r   rm      s    zfindElements.<locals>.<lambda>r^   )r`   ra   r   r   r   findElements   ro   rs   c                 C   s*   |rt | ||fddS t | |fddS )Nc                 S   s   |  |o| ||kS rP   )ri   r   )rj   rl   r6   r   r   r   rm     s   
 z+findElementsWithAttribute.<locals>.<lambda>c                 S   s
   |  |S rP   )ri   )rj   rl   r   r   r   rm        
 )rs   )r`   rl   r6   r   r   r   findElementsWithAttribute  s   ru   c                 C   rp   )Nc                 S   s
   | j |kS rP   nodeName)rj   namer   r   r   rm     rt   z findNodesNamed.<locals>.<lambda>rr   )r`   rx   r   r   r   findNodesNamed  s   ry   c                 C   s6   | j D ]}t|dr|d|j  qt|| qd S )NrR   rC   )r   r   rQ   rR   writeNodeData)r   oldiosubnoder   r   r   rz     s
   

rz   c                 C   s   t  }t| | | S rP   )r   rz   getvalue)r   r{   r   r   r   getNodeText  s   
r~   c                 C   s    g }| r| |  | j} | s|S rP   )r2   
parentNode)r   lr   r   r   
getParents#  s   
r   c                    s    fdd| j D S )znnamedChildren(parent, nodeName) -> children (not descendants) of parent
    that have tagName == nodeName
    c                    s    g | ]}t |d d kr|qS )rg   rC   rq   ).0rj   rv   r   r   
<listcomp>/  s     z!namedChildren.<locals>.<listcomp>r-   )r`   rw   r   rv   r   namedChildren+  s   r   )r   )r.   )r   rC   rP   )#r\   ior   twisted.webr   twisted.web.microdomr   r   r   	Exceptionr   r   r"   r)   r*   r,   r+   r1   r;   r>   rB   rN   TextrO   r^   rc   re   rn   rs   ru   ry   rz   r~   r   r   r   r   r   r   <module>   s<   
	
	




