o
    ¯b+  ã                   @   sr   d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	m
Z
 G dd„ dƒZG dd	„ d	eeƒZG d
d„ deeƒZdS )zH
Specific tests for (some of) the methods in L{twisted.web.domhelpers}.
é    )ÚAnyÚOptional)Úminidom)ÚTestCase)Ú
domhelpersÚmicrodomc                   @   sŠ   e Zd ZU dZdZee ed< dd„ Zdd„ Z	dd	„ Z
d
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚDOMHelpersTestsMixinz¥
    A mixin for L{TestCase} subclasses which defines test methods for
    domhelpers functionality based on a DOM creation function provided by a
    subclass.
    NÚdomc                 C   s”  | j  d¡}t |d¡d j}d}|  ||¡ |j}t |d¡d j}|  ||¡ d}| j  |¡}t |d¡}d dd„ |D ƒ¡}d}|  ||¡ |j}t |d¡}d d	d„ |D ƒ¡}|  ||¡ d
}	| j  |	¡}
t |
d¡}d dd„ |D ƒ¡}d}|  ||¡ |
j}t |d¡}d dd„ |D ƒ¡}|  ||¡ d}| j  |¡}t |d¡}|j}||jd jd g}|  ||¡ t |d¡}|  ||¡ d S )Nz<foo/>Úfoor   z<a><foo in="a"/><b><foo in="b"/></b><c><foo in="c"/></c><foo in="d"/><foo in="ef"/><g><foo in="g"/><h><foo in="h"/></h></g></a>Ú c                 S   ó   g | ]}|  d ¡‘qS ©Úin©ÚgetAttribute©Ú.0Únode© r   úB/usr/lib/python3/dist-packages/twisted/web/test/test_domhelpers.pyÚ
<listcomp>%   ó    zBDOMHelpersTestsMixin.test_getElementsByTagName.<locals>.<listcomp>Úabcdefghc                 S   r   r   r   r   r   r   r   r   *   r   aH  
<a><foo in="a"/>
    <b><foo in="b"/>
        <d><foo in="d"/>
            <g><foo in="g"/></g>
            <h><foo in="h"/></h>
        </d>
        <e><foo in="e"/>
            <i><foo in="i"/></i>
        </e>
    </b>
    <c><foo in="c"/>
        <f><foo in="f"/>
            <j><foo in="j"/></j>
        </f>
    </c>
</a>c                 S   r   r   r   r   r   r   r   r   @   r   Ú
abdgheicfjc                 S   r   r   r   r   r   r   r   r   E   r   z'<foo><bar></bar><baz><foo/></baz></foo>éÿÿÿÿ)	r	   ÚparseStringr   ÚgetElementsByTagNameÚnodeNameÚassertEqualÚdocumentElementÚjoinÚ
childNodes)ÚselfÚdoc1ÚactualÚexpectedÚel1Údoc2_xmlÚdoc2Útag_listÚel2Údoc3_xmlÚdoc3Úel3Údoc4_xmlÚdoc4Úrootr   r   r   Útest_getElementsByTagName   sF   z.DOMHelpersTestsMixin.test_getElementsByTagNamec                 C   sÆ   | j  d¡}t |¡}d}|  ||¡ t |j¡}|  ||¡ d}| j  |¡}t |¡}d}|  ||¡ t |j¡}|  ||¡ d}| j  |¡}t |¡}d}|  ||¡ t |j¡}|  ||¡ d S )Nz
<a>foo</a>r
   z+<a>a<b>b</b><c>c</c>def<g>g<h>h</h></g></a>r   zP<a>a<b>b<d>d<g>g</g><h>h</h></d><e>e<i>i</i></e></b><c>c<f>f<j>j</j></f></c></a>r   ©r	   r   r   ÚgatherTextNodesr   r   )r"   r#   r$   r%   r'   r(   r+   r,   r   r   r   Útest_gatherTextNodesQ   s*   

ÿ
z)DOMHelpersTestsMixin.test_gatherTextNodesc                 C   s–   | j  d¡}|j}t |¡ |  | ¡ | j  d¡ ¡ ¡ | j  d¡}|jjd }t |¡ |j ¡ }| j  d¡}| 	| j  d¡¡ |  || ¡ ¡ d S )Nz<a><b><c><d/></c></b></a>Úar   Úb)
r	   r   r   r   Ú	clearNoder   ÚtoxmlÚElementr!   ÚappendChild)r"   r#   Úa_noder(   Úb_noder$   r%   r   r   r   Útest_clearNodel   s   


z#DOMHelpersTestsMixin.test_clearNodec                 C   s¢   | j  d¡}| j  ¡ }t |d¡}| ¡ }| d¡}| dd¡ |  || ¡ ¡ t |d¡}| ¡ }| d¡}| dd¡ |  || ¡ ¡ |  	tj
tj|d¡ d S )	Nú$<a><b id="bar"/><c class="foo"/></a>r
   ÚcÚclassÚbarr6   ÚidÚpzork)r	   r   ÚDocumentr   Úgetr8   ÚcreateElementÚsetAttributer   ÚassertRaisesÚNodeLookupError©r"   r#   Údocr   r$   r%   r   r   r   Útest_getz   s   


zDOMHelpersTestsMixin.test_getc                 C   sl   | j  d¡}| j  ¡ }t |d¡}| ¡ }| d¡}| dd¡ |  || ¡ ¡ t |d¡}|  	|d ¡ d S )Nr>   r
   r?   r@   rC   )
r	   r   rD   r   ÚgetIfExistsr8   rF   rG   r   ÚassertIdenticalrJ   r   r   r   Útest_getIfExists‹   s   

z%DOMHelpersTestsMixin.test_getIfExistsc                 C   sT   | j  d¡}| j  ¡ }t |d¡}| ¡ }| d¡}| dd¡ |  || ¡ ¡ d S )Nz<a><b id="foo"><c></c></b></a>r
   r6   rB   )	r	   r   rD   r   ÚgetAndClearr8   rF   rG   r   rJ   r   r   r   Útest_getAndClear—   s   

z%DOMHelpersTestsMixin.test_getAndClearc                 C   sÈ   | j  d¡}| j  ¡ }tj|jdddd}d dd„ |D ƒ¡}| d	¡}| dd¡ | d
¡}| dd¡ | 	|¡ |  
|| ¡ ¡ tj|jdddd}d dd„ |D ƒ¡}|  
|| ¡ | ¡  ¡ d S )Nz:<a><b foo="olive"><c foo="olive"/></b><d foo="poopy"/></a>r
   Úoliveé   )Ú	noNestingr   c                 S   ó   g | ]}|  ¡ ‘qS r   ©r8   r   r   r   r   r   ¦   ó    z9DOMHelpersTestsMixin.test_locateNodes.<locals>.<listcomp>r6   r?   r   c                 S   rU   r   rV   r   r   r   r   r   °   rW   )r	   r   rD   r   ÚlocateNodesr!   r    rF   rG   r:   r   r8   )r"   r#   rK   Ú	node_listr$   r%   r?   r   r   r   Útest_locateNodes    s   ÿ



z%DOMHelpersTestsMixin.test_locateNodesc                 C   sL   | j  d¡}t |jd jd jd ¡}d dd„ |D ƒ¡}|  |d¡ d S )Nz!<a><b><c><d/></c><e/></b><f/></a>r   r   c                 S   s   g | ]
}t |d ƒr|j‘qS ©ÚtagName)Úhasattrr\   r   r   r   r   r   ¹   s    z8DOMHelpersTestsMixin.test_getParents.<locals>.<listcomp>Úcba)r	   r   r   Ú
getParentsr!   r    r   ©r"   r#   rY   r$   r   r   r   Útest_getParents³   s   ÿÿz$DOMHelpersTestsMixin.test_getParentsc                 C   sj   | j  d¡}t |d¡}d dd„ |D ƒ¡}|  |d¡ t |dd¡}d dd„ |D ƒ¡}|  |d	¡ d S )
Nz+<a foo="1"><b foo="2"/><c foo="1"/><d/></a>r
   r   c                 S   ó   g | ]}|j ‘qS r   r[   r   r   r   r   r   À   ó    zGDOMHelpersTestsMixin.test_findElementsWithAttribute.<locals>.<listcomp>ÚabcÚ1c                 S   rb   r   r[   r   r   r   r   r   Ä   rc   Úac)r	   r   r   ÚfindElementsWithAttributer    r   r`   r   r   r   Útest_findElementsWithAttribute½   s   z3DOMHelpersTestsMixin.test_findElementsWithAttributec                 C   s0   | j  d¡}t |d¡}t|ƒ}|  |d¡ d S )Nz#<doc><foo/><bar/><foo>a</foo></doc>r
   é   )r	   r   r   ÚfindNodesNamedÚlenr   r`   r   r   r   Útest_findNodesNamedÇ   s   z(DOMHelpersTestsMixin.test_findNodesNamedc                 C   ó   d}d}|   t |¡|¡ d S )Nz9this string " contains many & characters> xml< won't likezHthis string &quot; contains many &amp; characters&gt; xml&lt; won't like)r   r   Úescape©r"   Újr%   r   r   r   Útest_escapeÍ   ó   ÿz DOMHelpersTestsMixin.test_escapec                 C   rm   )NzTthis string &quot; has &&amp; entities &gt; &lt; and some characters xml won't like<zEthis string " has && entities > < and some characters xml won't like<)r   r   Úunescapero   r   r   r   Útest_unescapeÔ   rr   z"DOMHelpersTestsMixin.test_unescapec                 C   s"   | j  d¡}|  t |¡d¡ dS )z|
        L{getNodeText} returns the concatenation of all the text data at or
        beneath the node passed to it.
        z(<foo><bar>baz</bar><bar>quux</bar></foo>ÚbazquuxN)r	   r   r   r   ÚgetNodeText)r"   r   r   r   r   Útest_getNodeTextÛ   s   z%DOMHelpersTestsMixin.test_getNodeText)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r   Ú__annotations__r1   r4   r=   rL   rO   rQ   rZ   ra   rh   rl   rq   rt   rw   r   r   r   r   r      s    
 8	

r   c                   @   ó    e Zd ZeZdd„ Zdd„ ZdS )ÚMicroDOMHelpersTestsc                 C   óF   d}| j  |¡}t |¡}d}|  ||¡ t |j¡}|  ||¡ dS )z£
        Microdom discards whitespace-only text nodes, so L{gatherTextNodes}
        returns only the text from nodes which had non-whitespace characters.
        z?<html>
  <head>
  </head>
  <body>
    stuff
  </body>
</html>
z
    stuff
  Nr2   )r"   r.   r/   r$   r%   r   r   r   Ú#test_gatherTextNodesDropsWhitespaceç   s   
z8MicroDOMHelpersTests.test_gatherTextNodesDropsWhitespacec                 C   r   )zB
        Microdom does not decode entities in text nodes.
        ú<x>Souffl&amp;</x>zSouffl&amp;Nr2   ©r"   Údoc5_xmlÚdoc5r$   r%   r   r   r   Útest_textEntitiesNotDecodedû   ó   
z0MicroDOMHelpersTests.test_textEntitiesNotDecodedN)rx   ry   rz   r   r	   r€   r…   r   r   r   r   r~   ä   s    r~   c                   @   r}   )ÚMiniDOMHelpersTestsc                 C   r   )z=
        Minidom does decode entities in text nodes.
        r   zSouffl&Nr2   r‚   r   r   r   Útest_textEntitiesDecoded  r†   z,MiniDOMHelpersTests.test_textEntitiesDecodedc                 C   sd   | j  d¡}t |¡}|  |d¡ |  |t¡ | j  d ¡ ¡}t |¡}|  |d¡ |  |t¡ dS )zÇ
        L{domhelpers.getNodeText} returns a C{unicode} string when text
        nodes are represented in the DOM with unicode, whether or not there
        are non-ASCII characters present.
        z<foo>bar</foo>rA   u   <foo>â˜ƒ</foo>u   â˜ƒN)r	   r   r   rv   r   ÚassertIsInstanceÚstrÚencode)r"   r   Útextr   r   r   Útest_getNodeUnicodeText  s   

z+MiniDOMHelpersTests.test_getNodeUnicodeTextN)rx   ry   rz   r   r	   rˆ   r   r   r   r   r   r‡     s    r‡   N)r{   Útypingr   r   Úxml.domr   Útwisted.trial.unittestr   Útwisted.webr   r   r   r~   r‡   r   r   r   r   Ú<module>   s    U$