o
    ¼>hº,  ã                   @   s~   d Z ddlm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}.
é    )Úreload)Ú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   ú€/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-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Úbarr7   ÚidÚpzork)r
   r   ÚDocumentr   Úgetr9   Ú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@   rA   rD   )
r
   r   rE   r   ÚgetIfExistsr9   rG   rH   r   ÚassertIdenticalrK   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   r7   rC   )	r
   r   rE   r   ÚgetAndClearr9   rG   rH   r   rK   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   ©r9   r   r   r   r   r   ¦   ó    z9DOMHelpersTestsMixin.test_locateNodes.<locals>.<listcomp>r7   r@   r   c                 S   rV   r   rW   r   r   r   r   r   °   rX   )r
   r   rE   r   ÚlocateNodesr"   r!   rG   rH   r;   r   r9   )r#   r$   rL   Ú	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$   rZ   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   rc   r   r\   r   r   r   r   r   Ä   rd   Úac)r
   r   r   ÚfindElementsWithAttributer!   r   ra   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   ra   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   rn   )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   Úunescaperp   r   r   r   Útest_unescapeÔ   rs   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__r2   r5   r>   rM   rP   rR   r[   rb   ri   rm   rr   ru   rx   r   r   r   r   r	      s    
 8	

r	   c                   @   s(   e Zd ZeZdd„ 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
  Nr3   )r#   r/   r0   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;Nr3   ©r#   Údoc5_xmlÚdoc5r%   r&   r   r   r   Útest_textEntitiesNotDecodedû   ó   
z0MicroDOMHelpersTests.test_textEntitiesNotDecodedc                 C   s>   t tƒ |  | jg¡}|  dt|ƒ¡ |  d|d d ¡ dS )z?
        An import will raise the deprecation warning.
        rT   z8twisted.web.domhelpers was deprecated at Twisted 23.10.0r   ÚmessageN)r   r   ÚflushWarningsÚtest_deprecationr   rl   )r#   Úwarningsr   r   r   r‰     s   
þz%MicroDOMHelpersTests.test_deprecationN)ry   rz   r{   r   r
   r€   r…   r‰   r   r   r   r   r~   ä   s
    r~   c                   @   s    e Zd ZeZdd„ Zdd„ ZdS )ÚMiniDOMHelpersTestsc                 C   r   )z=
        Minidom does decode entities in text nodes.
        r   zSouffl&Nr3   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>rB   u   <foo>â˜ƒ</foo>u   â˜ƒN)r
   r   r   rw   r   ÚassertIsInstanceÚstrÚencode)r#   r   Útextr   r   r   Útest_getNodeUnicodeText#  s   

z+MiniDOMHelpersTests.test_getNodeUnicodeTextN)ry   rz   r{   r   r
   rŒ   r‘   r   r   r   r   r‹     s    r‹   N)r|   Ú	importlibr   Útypingr   r   Úxml.domr   Útwisted.trial.unittestr   Útwisted.webr   r   r	   r~   r‹   r   r   r   r   Ú<module>   s    U0