
    Vh,                         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y)zH
Specific tests for (some of) the methods in L{twisted.web.domhelpers}.
    )reload)AnyOptional)minidom)TestCase)
domhelpersmicrodomc                   t    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y)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                   j                  d      }t        j                  |d      d   j                  }d}| j                  ||       |j                  }t        j                  |d      d   j                  }| j                  ||       d}| j                   j                  |      }t        j                  |d      }dj                  |D cg c]  }|j                  d       c}      }d}| j                  ||       |j                  }	t        j                  |	d      }dj                  |D cg c]  }|j                  d       c}      }| j                  ||       d}
| j                   j                  |
      }t        j                  |d      }dj                  |D cg c]  }|j                  d       c}      }d	}| j                  ||       |j                  }t        j                  |d      }dj                  |D cg c]  }|j                  d       c}      }| j                  ||       d
}| j                   j                  |      }t        j                  |d      }|j                  }||j                  d   j                  d   g}| j                  ||       t        j                  |d      }| j                  ||       y c c}w c c}w c c}w c c}w )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> inabcdefghaH  
<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>
abdgheicfjz'<foo><bar></bar><baz><foo/></baz></foo>)
r   parseStringr   getElementsByTagNamenodeNameassertEqualdocumentElementjoingetAttribute
childNodes)selfdoc1actualexpectedel1doc2_xmldoc2tag_listnodeel2doc3_xmldoc3el3doc4_xmldoc4roots                   P/home/dcms/DCMS/lib/python3.12/site-packages/twisted/web/test/test_domhelpers.pytest_getElementsByTagNamez.DOMHelpersTestsMixin.test_getElementsByTagName   s   xx##H-00u=a@II*""00e<Q?HH* Uxx##H-224?hGd$++D1GH*""223>hGd$++D1GH*" xx##H-224?hGd$++D1GH*""223>hGd$++D1GH*<xx##H-00u=##$//"-88;<*00u=*U H
 H, H
 Hs   K:K-K$K)c                    | j                   j                  d      }t        j                  |      }d}| j	                  ||       t        j                  |j
                        }| j	                  ||       d}| j                   j                  |      }t        j                  |      }d}| j	                  ||       t        j                  |j
                        }| j	                  ||       d}| j                   j                  |      }t        j                  |      }d}| j	                  ||       t        j                  |j
                        }| j	                  ||       y )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'   s           r,   test_gatherTextNodesz)DOMHelpersTestsMixin.test_gatherTextNodesQ   s0   xx##L1++D1*++D,@,@A*@xx##H-++D1*++D,@,@A*- 	 xx##H-++D1*++D,@,@A*    c                    | j                   j                  d      }|j                  }t        j                  |       | j                  |j                         | j                   j                  d      j                                | j                   j                  d      }|j                  j                  d   }t        j                  |       |j                  j                         }| j                   j                  d      }|j                  | j                   j                  d             | j                  ||j                                y )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    xx##$?@%%V$)9)9#)>)D)D)FGxx##$?@%%003V$%%++-88##C(TXX--c23!12r2   c                 r   | j                   j                  d      }| j                   j                         }t        j                  |d      }|j                         }|j                  d      }|j                  dd       | j                  ||j                                t        j                  |d      }|j                         }|j                  d      }|j                  dd       | j                  ||j                                | j                  t        j                  t        j                  |d       y )	N$<a><b id="bar"/><c class="foo"/></a>r   cclassbarr5   idpzork)r   r   Documentr   getr7   createElementsetAttributer   assertRaisesNodeLookupErrorr   r   docr$   r   r   s         r,   test_getzDOMHelpersTestsMixin.test_getz   s    xx##$JKhh!~~dE*$$S)gu-!12~~dE*$$S)dE*!12*44jnndGTr2   c                    | j                   j                  d      }| j                   j                         }t        j                  |d      }|j                         }|j                  d      }|j                  dd       | j                  ||j                                t        j                  |d      }| j                  |d        y )Nr>   r   r?   r@   rC   )
r   r   rD   r   getIfExistsr7   rF   rG   r   assertIdenticalrJ   s         r,   test_getIfExistsz%DOMHelpersTestsMixin.test_getIfExists   s    xx##$JKhh!%%dE2$$S)gu-!12%%dG4T4(r2   c                 @   | j                   j                  d      }| j                   j                         }t        j                  |d      }|j                         }|j                  d      }|j                  dd       | j                  ||j                                y )Nz<a><b id="foo"><c></c></b></a>r   r5   rB   )	r   r   rD   r   getAndClearr7   rF   rG   r   rJ   s         r,   test_getAndClearz%DOMHelpersTestsMixin.test_getAndClear   s}    xx##$DEhh!%%dE2$$S)dE*!12r2   c                    | j                   j                  d      }| j                   j                         }t        j                  |j
                  ddd      }dj                  |D cg c]  }|j                          c}      }|j                  d      }|j                  dd       |j                  d      }|j                  dd       |j                  |       | j                  ||j                                t        j                  |j
                  ddd	      }dj                  |D cg c]  }|j                          c}      }| j                  ||j                         |j                         z          y c c}w c c}w )
Nz:<a><b foo="olive"><c foo="olive"/></b><d foo="poopy"/></a>r   olive   )	noNestingr   r5   r?   r   )r   r   rD   r   locateNodesr   r   r7   rF   rG   r9   r   )r   r   rK   	node_listr$   r   r   r?   s           r,   test_locateNodesz%DOMHelpersTestsMixin.test_locateNodes   s0   xx##H
 hh!**4??E7VWX	9=4$**,=>$$S)eW-c"	ug&Q!12**4??E7VWX	9=4$**,=>!1AGGI!=> > >s   (E?/Fc           	      L   | j                   j                  d      }t        j                  |j                  d   j                  d   j                  d         }dj                  |D cg c]  }t        |d      s|j                   c}      }| j                  |d       y c c}w )Nz!<a><b><c><d/></c><e/></b><f/></a>r   r   tagNamecba)	r   r   r   
getParentsr   r   hasattrr\   r   r   r   rY   r$   r   s        r,   test_getParentsz$DOMHelpersTestsMixin.test_getParents   s    xx##$GH))OOA))!,77:
	 &/Ld743KT\\L
 	' Ms   'B!9B!c                    | j                   j                  d      }t        j                  |d      }dj	                  |D cg c]  }|j
                   c}      }| j                  |d       t        j                  |dd      }dj	                  |D cg c]  }|j
                   c}      }| j                  |d       y c c}w c c}w )Nz+<a foo="1"><b foo="2"/><c foo="1"/><d/></a>r   r   abc1ac)r   r   r   findElementsWithAttributer   r\   r   r`   s        r,   test_findElementsWithAttributez3DOMHelpersTestsMixin.test_findElementsWithAttribute   s    xx##$QR88uE	9=4$,,=>'88ucJ	9=4$,,=>& > >s   C Cc                     | j                   j                  d      }t        j                  |d      }t	        |      }| j                  |d       y )Nz#<doc><foo/><bar/><foo>a</foo></doc>r      )r   r   r   findNodesNamedlenr   )r   r   rY   r   s       r,   test_findNodesNamedz(DOMHelpersTestsMixin.test_findNodesNamed   sB    xx##$IJ--dE:	Y#r2   c                 V    d}d}| j                  t        j                  |      |       y )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   s,    HV 	 	**1-x8r2   c                 V    d}d}| j                  t        j                  |      |       y )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   s      r,   test_unescapez"DOMHelpersTestsMixin.test_unescape   s,    bT 	 	,,Q/:r2   c                     | j                   j                  d      }| j                  t        j                  |      d       y)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   s4    
 xx##$NO//5yAr2   )__name__
__module____qualname____doc__r   r   r   __annotations__r-   r1   r<   rL   rP   rS   rZ   ra   rg   rl   rq   rt   rx    r2   r,   r   r      s[     C#6+p+63U"
)3?&('$9;Br2   r   c                   "    e Zd ZeZd Zd Zd Zy)MicroDOMHelpersTestsc                     d}| j                   j                  |      }t        j                  |      }d}| j	                  ||       t        j                  |j
                        }| j	                  ||       y)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   r)   r*   r   r   s        r,   #test_gatherTextNodesDropsWhitespacez8MicroDOMHelpersTests.test_gatherTextNodesDropsWhitespace   sj    
 xx##H-++D1$*++D,@,@A*r2   c                     d}| j                   j                  |      }t        j                  |      }d}| j	                  ||       t        j                  |j
                        }| j	                  ||       y)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   sh     (xx##H-++D1 *++D,@,@A*r2   c                     t        t               | j                  | j                  g      }| j	                  dt        |             | j	                  d|d   d          y)z?
        An import will raise the deprecation warning.
        rV   z8twisted.web.domhelpers was deprecated at Twisted 23.10.0r   messageN)r   r   flushWarningstest_deprecationr   rk   )r   warningss     r,   r   z%MicroDOMHelpersTests.test_deprecation  sV     	z%%t'<'<&=>CM*FQK	"	
r2   N)ry   rz   r{   r	   r   r   r   r   r~   r2   r,   r   r      s    
C+(
+

r2   r   c                       e Zd ZeZd Zd Zy)MiniDOMHelpersTestsc                     d}| j                   j                  |      }t        j                  |      }d}| j	                  ||       t        j                  |j
                        }| j	                  ||       y)z=
        Minidom does decode entities in text nodes.
        r   zSouffl&Nr/   r   s        r,   test_textEntitiesDecodedz,MiniDOMHelpersTests.test_textEntitiesDecoded  sh     (xx##H-++D1*++D,@,@A*r2   c                    | j                   j                  d      }t        j                  |      }| j	                  |d       | j                  |t               | j                   j                  dj                               }t        j                  |      }| j	                  |d       | j                  |t               y)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   rw   r   assertIsInstancestrencode)r   r$   texts      r,   test_getNodeUnicodeTextz+MiniDOMHelpersTests.test_getNodeUnicodeText#  s     xx##$45%%d+u%dC(xx##$<$C$C$EF%%d+}-dC(r2   N)ry   rz   r{   r   r   r   r   r~   r2   r,   r   r     s    
C
+)r2   r   N)r|   	importlibr   typingr   r   xml.domr   twisted.trial.unittestr   twisted.webr   r	   r   r   r   r~   r2   r,   <module>r      sI   
     + ,QB QBh-
/ -
`). )r2   