
    tf,                         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          Z G d	 d
ee	          Z G 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                   |    e Zd ZU dZdZee         ed<   d Zd Z	d Z
d Zd Zd	 Zd
 Zd Zd Zd Zd Zd Z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                 .   | j                             d          }t          j        |d          d         j        }d}|                     ||           |j        }t          j        |d          d         j        }|                     ||           d}| j                             |          }t          j        |d          }d                    d |D                       }d}|                     ||           |j        }t          j        |d          }d                    d |D                       }|                     ||           d	}	| j                             |	          }
t          j        |
d          }d                    d
 |D                       }d}|                     ||           |
j        }t          j        |d          }d                    d |D                       }|                     ||           d}| j                             |          }t          j        |d          }|j        }||j        d         j        d         g}|                     ||           t          j        |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                 8    g | ]}|                     d           S ingetAttribute.0nodes     _/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/web/test/test_domhelpers.py
<listcomp>zBDOMHelpersTestsMixin.test_getElementsByTagName.<locals>.<listcomp>%   &    GGGd$++D11GGG    abcdefghc                 8    g | ]}|                     d           S r   r   r   s     r   r   zBDOMHelpersTestsMixin.test_getElementsByTagName.<locals>.<listcomp>*   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                 8    g | ]}|                     d           S r   r   r   s     r   r   zBDOMHelpersTestsMixin.test_getElementsByTagName.<locals>.<listcomp>@   r   r   
abdgheicfjc                 8    g | ]}|                     d           S r   r   r   s     r   r   zBDOMHelpersTestsMixin.test_getElementsByTagName.<locals>.<listcomp>E   r   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roots                  r   test_getElementsByTagNamez.DOMHelpersTestsMixin.test_getElementsByTagName   s   x##H--0u==a@I***"0e<<Q?H*** Ux##H--24??GGhGGGHH***"23>>GGhGGGHH***" x##H--24??GGhGGGHH***"23>>GGhGGGHH***<x##H--0u==#$/"-8;<***0u==*****r   c                    | j                             d          }t          j        |          }d}|                     ||           t          j        |j                  }|                     ||           d}| j                             |          }t          j        |          }d}|                     ||           t          j        |j                  }|                     ||           d}| j                             |          }t          j        |          }d}|                     ||           t          j        |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/   r2   r3   s           r   test_gatherTextNodesz)DOMHelpersTestsMixin.test_gatherTextNodesQ   sT   x##L11+D11***+D,@AA***@x##H--+D11***+D,@AA***- 	 x##H--+D11***+D,@AA*****r   c                    | j                             d          }|j        }t          j        |           |                     |                                | j                             d                                                     | j                             d          }|j        j        d         }t          j        |           |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,   s          r   test_clearNodez#DOMHelpersTestsMixin.test_clearNodel   s   x##$?@@%V$$$)9)9#)>)>)D)D)F)FGGGx##$?@@%03V$$$%++--8##C((TX--c22333!1!122222r   c                    | j                             d          }| j                                         }t          j        |d          }|                                }|                    d          }|                    dd           |                     ||                                           t          j        |d          }|                                }|                    d          }|                    dd           |                     ||                                           | 	                    t          j
        t          j        |d           d S )	N$<a><b id="bar"/><c class="foo"/></a>r   cclassbarr?   idpzork)r   r"   Documentr   getrA   createElementsetAttributer%   assertRaisesNodeLookupErrorr)   r*   docr   r+   r,   s         r   test_getzDOMHelpersTestsMixin.test_getz   s   x##$JKKh!!~dE**$$S))gu---!1!1222~dE**$$S))dE***!1!1222*4jndGTTTTTr   c                    | j                             d          }| j                                         }t          j        |d          }|                                }|                    d          }|                    dd           |                     ||                                           t          j        |d          }| 	                    |d            d S )NrH   r   rI   rJ   rM   )
r   r"   rN   r   getIfExistsrA   rP   rQ   r%   assertIdenticalrT   s         r   test_getIfExistsz%DOMHelpersTestsMixin.test_getIfExists   s    x##$JKKh!!%dE22$$S))gu---!1!1222%dG44T4(((((r   c                 d   | j                             d          }| j                                         }t          j        |d          }|                                }|                    d          }|                    dd           |                     ||                                           d S )Nz<a><b id="foo"><c></c></b></a>r   r?   rL   )	r   r"   rN   r   getAndClearrA   rP   rQ   r%   rT   s         r   test_getAndClearz%DOMHelpersTestsMixin.test_getAndClear   s    x##$DEEh!!%dE22$$S))dE***!1!122222r   c                    | j                             d          }| j                                         }t          j        |j        ddd          }d                    d |D                       }|                    d          }|                    dd           |                    d	          }|                    dd           |	                    |           | 
                    ||                                           t          j        |j        ddd
          }d                    d |D                       }| 
                    ||                                |                                z              d S )Nz:<a><b foo="olive"><c foo="olive"/></b><d foo="poopy"/></a>r   olive   )	noNestingr   c                 6    g | ]}|                                 S  rA   r   s     r   r   z9DOMHelpersTestsMixin.test_locateNodes.<locals>.<listcomp>        ===4$**,,===r   r?   rI   r   c                 6    g | ]}|                                 S rc   rd   r   s     r   r   z9DOMHelpersTestsMixin.test_locateNodes.<locals>.<listcomp>   re   r   )r   r"   rN   r   locateNodesr(   r'   rP   rQ   rC   r%   rA   )r)   r*   rU   	node_listr+   r,   rI   s          r   test_locateNodesz%DOMHelpersTestsMixin.test_locateNodes   s`   x##H
 
 h!!*4?E7VWXXX	==9===>>$$S))eW---c""	ug&&&Q!1!1222*4?E7VWXXX	==9===>>!1!1AGGII!=>>>>>r   c                    | j                             d          }t          j        |j        d         j        d         j        d                   }d                    d |D                       }|                     |d           d S )Nz!<a><b><c><d/></c><e/></b><f/></a>r   r   c                 <    g | ]}t          |d           |j        S tagName)hasattrrm   r   s     r   r   z8DOMHelpersTestsMixin.test_getParents.<locals>.<listcomp>   s)    LLLd743K3KLT\LLLr   cba)r   r"   r   
getParentsr(   r'   r%   r)   r*   rh   r+   s       r   test_getParentsz$DOMHelpersTestsMixin.test_getParents   s    x##$GHH)OA)!,7:
 
	 LLiLLL
 
 	'''''r   c                 d   | j                             d          }t          j        |d          }d                    d |D                       }|                     |d           t          j        |dd          }d                    d |D                       }|                     |d           d S )	Nz+<a foo="1"><b foo="2"/><c foo="1"/><d/></a>r   r   c                     g | ]	}|j         
S rc   rl   r   s     r   r   zGDOMHelpersTestsMixin.test_findElementsWithAttribute.<locals>.<listcomp>       ===4$,===r   abc1c                     g | ]	}|j         
S rc   rl   r   s     r   r   zGDOMHelpersTestsMixin.test_findElementsWithAttribute.<locals>.<listcomp>   ru   r   ac)r   r"   r   findElementsWithAttributer'   r%   rq   s       r   test_findElementsWithAttributez3DOMHelpersTestsMixin.test_findElementsWithAttribute   s    x##$QRR8uEE	==9===>>'''8ucJJ	==9===>>&&&&&r   c                     | j                             d          }t          j        |d          }t	          |          }|                     |d           d S )Nz#<doc><foo/><bar/><foo>a</foo></doc>r      )r   r"   r   findNodesNamedlenr%   rq   s       r   test_findNodesNamedz(DOMHelpersTestsMixin.test_findNodesNamed   sQ    x##$IJJ-dE::	Y#####r   c                 ^    d}d}|                      t          j        |          |           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,   s      r   test_escapez DOMHelpersTestsMixin.test_escape   s8    HV 	 	*1--x88888r   c                 ^    d}d}|                      t          j        |          |           d S )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   unescaper   s      r   test_unescapez"DOMHelpersTestsMixin.test_unescape   s8    bT 	 	,Q//:::::r   c                     | j                             d          }|                     t          j        |          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   s     r   test_getNodeTextz%DOMHelpersTestsMixin.test_getNodeText   sA    
 x##$NOO/55yAAAAAr   )__name__
__module____qualname____doc__r   r   r   __annotations__r8   r<   rF   rV   rZ   r]   ri   rr   r{   r   r   r   r   rc   r   r   r   r      s          C#6+ 6+ 6+p+ + +63 3 3U U U"
) 
) 
)3 3 3? ? ?&( ( (' ' '$ $ $9 9 9; ; ;B B B B Br   r   c                   $    e Zd ZeZd Zd Zd ZdS )MicroDOMHelpersTestsc                     d}| j                             |          }t          j        |          }d}|                     ||           t          j        |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
  Nr:   )r)   r5   r6   r+   r,   s        r   #test_gatherTextNodesDropsWhitespacez8MicroDOMHelpersTests.test_gatherTextNodesDropsWhitespace   sz    
 x##H--+D11$***+D,@AA*****r   c                     d}| j                             |          }t          j        |          }d}|                     ||           t          j        |j                  }|                     ||           dS )zB
        Microdom does not decode entities in text nodes.
        <x>Souffl&amp;</x>zSouffl&amp;Nr:   r)   doc5_xmldoc5r+   r,   s        r   test_textEntitiesNotDecodedz0MicroDOMHelpersTests.test_textEntitiesNotDecoded   sx     (x##H--+D11 ***+D,@AA*****r   c                     t          t                     |                     | j        g          }|                     dt          |                     |                     d|d         d                    dS )z?
        An import will raise the deprecation warning.
        r`   z8twisted.web.domhelpers was deprecated at Twisted 23.10.0r   messageN)r   r   flushWarningstest_deprecationr%   r   )r)   warningss     r   r   z%MicroDOMHelpersTests.test_deprecation  sx     	z%%t'<&=>>CMM***FQK	"	
 	
 	
 	
 	
r   N)r   r   r   r	   r   r   r   r   rc   r   r   r   r      sF        
C+ + +(
+ 
+ 
+

 

 

 

 

r   r   c                       e Zd ZeZd Zd ZdS )MiniDOMHelpersTestsc                     d}| j                             |          }t          j        |          }d}|                     ||           t          j        |j                  }|                     ||           dS )z=
        Minidom does decode entities in text nodes.
        r   zSouffl&Nr:   r   s        r   test_textEntitiesDecodedz,MiniDOMHelpersTests.test_textEntitiesDecoded  sx     (x##H--+D11***+D,@AA*****r   c                    | j                             d          }t          j        |          }|                     |d           |                     |t                     | j                             d                                          }t          j        |          }|                     |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>rK   u   <foo>☃</foo>u   ☃N)r   r"   r   r   r%   assertIsInstancestrencode)r)   r   texts      r   test_getNodeUnicodeTextz+MiniDOMHelpersTests.test_getNodeUnicodeText#  s     x##$455%d++u%%%dC(((x##$<$C$C$E$EFF%d++}---dC(((((r   N)r   r   r   r   r   r   r   rc   r   r   r   r     s7        
C
+ 
+ 
+) ) ) ) )r   r   N)r   	importlibr   typingr   r   xml.domr   twisted.trial.unittestr   twisted.webr   r	   r   r   r   rc   r   r   <module>r      s  
                              + + + + + + , , , , , , , ,QB QB QB QB QB QB QB QBh-
 -
 -
 -
 -
/ -
 -
 -
`) ) ) ) ). ) ) ) ) )r   