o
    MK&hC?                     @   s   d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	m
Z
 ddlmZ ddlmZ G dd	 d	eZG d
d deZG dd deZG dd deZdS )z*Tests of the bs4.element.PageElement class    N)BeautifulSoup)AttributeValueListComment)SoupStrainer   )SoupTestc                   @   sX   e Zd Z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 )TestEncodingz0Test the ability to encode objects into strings.c                 C   s.   d}|  |}|jjdddksJ d S )N
   <b>☃</b>utf-8   ☃)soupbstringencodeselfhtmlr    r   M/var/www/html/venv/lib/python3.10/site-packages/bs4/tests/test_pageelement.py"test_unicode_string_can_be_encoded      
 z/TestEncoding.test_unicode_string_can_be_encodedc                 C   s,   d}|  |}|jd|dksJ d S )Nr	   r
   r   r   r   r   r   r   r   1test_tag_containing_unicode_string_can_be_encoded   s   
z>TestEncoding.test_tag_containing_unicode_string_can_be_encodedc                 C   s&   d}|  |}|jddksJ d S )Nr	   asciis   <b>&#9731;</b>r   r   r   r   r   <test_encoding_substitutes_unrecognized_characters_by_default!   s   
zITestEncoding.test_encoding_substitutes_unrecognized_characters_by_defaultc                 C   sL   d}|  |}tt |jddd W d    d S 1 sw   Y  d S )Nr	   r   strict)errors)r   pytestraisesUnicodeEncodeErrorr   r   r   r   r    test_encoding_can_be_made_strict&   s
   
"z-TestEncoding.test_encoding_can_be_made_strictc                 C   s$   d}|  |}d|j ksJ d S )Nr	   r   )r   r   decode_contentsr   r   r   r   test_decode_contents,   s   
z!TestEncoding.test_decode_contentsc                 C   s.   d}|  |}dd|jjddksJ d S )Nr	   r   utf8)encoding)r   r   r   encode_contentsr   r   r   r   test_encode_contents1   r   z!TestEncoding.test_encode_contentsc                 C   s<   t  d }d| }| |}| }||dksJ d S )Nr   <span>s   <span>)sysgetrecursionlimitr   r   count)r   limitmarkupr   encodedr   r   r   "test_encode_deeply_nested_document6   s
   
z/TestEncoding.test_encode_deeply_nested_documentc                 C   s   d}|  |}tjdd}|  dd|j ksJ W d    n1 s(w   Y  dd |D }tdd	 |D s?J d S )
Nr	   T)recordr   r#   c                 S   s   g | ]}t |jqS r   )strmessage).0warningr   r   r   
<listcomp>F   s    z?TestEncoding.test_deprecated_renderContents.<locals>.<listcomp>c                 s   s    | ]}|d kV  qdS )zjCall to deprecated method renderContents. (Replaced by encode_contents) -- Deprecated since version 4.0.0.Nr   )r2   xr   r   r   	<genexpr>G   s    
z>TestEncoding.test_deprecated_renderContents.<locals>.<genexpr>)r   warningscatch_warningsrenderContentsr   r   all)r   r   r   wmsgsr   r   r   test_deprecated_renderContents@   s   
z+TestEncoding.test_deprecated_renderContentsc                 C   s"   d}|  |}|t|ksJ d S Nr	   )r   reprr   r   r   r   	test_reprM   s   
zTestEncoding.test_reprN)__name__
__module____qualname____doc__r   r   r   r    r"   r&   r.   r=   r@   r   r   r   r   r      s    
r   c                   @   s   e Zd Z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d Zdd Zdd Zd d! Zd"d# Zd$S )%TestFormatterszrTest the formatting feature, used by methods like decode() and
    prettify(), and the formatters themselves.
    c                 C   0   d}|  |}|jdd}|| dksJ d S N#   <b>&lt;&lt;Sacré bleu!&gt;&gt;</b>minimal	formatterr   decodedocument_forr   r,   r   decodedr   r   r   !test_default_formatter_is_minimalX      
z0TestFormatters.test_default_formatter_is_minimalc                 C   0   d}|  |}|jdd}|| dksJ d S )N'   <br><b>&lt;&lt;Sacré bleu!&gt;&gt;</b>r   rJ   z.<br/><b>&lt;&lt;Sacr&eacute; bleu!&gt;&gt;</b>rL   rO   r   r   r   test_formatter_htmla      
z"TestFormatters.test_formatter_htmlc                 C   rS   )NrT   html5rJ   z-<br><b>&lt;&lt;Sacr&eacute; bleu!&gt;&gt;</b>rL   rO   r   r   r   test_formatter_html5k   rV   z#TestFormatters.test_formatter_html5c                 C   rF   rG   rL   rO   r   r   r   test_formatter_minimalu   rR   z%TestFormatters.test_formatter_minimalc                 C   s0   d}|  |}|jd d}|| dksJ d S )NrH   rJ   u   <b><<Sacré bleu!>></b>rL   rO   r   r   r   test_formatter_null~   s   
z"TestFormatters.test_formatter_nullc                 C   s4   d}|  |}|jdd d}|| dksJ d S )Nz!<b>&lt;foo&gt;</b><b>bar</b><br/>c                 S      |   S Nupperr5   r   r   r   <lambda>       z6TestFormatters.test_formatter_custom.<locals>.<lambda>rJ   z<b><FOO></b><b>BAR</b><br/>rL   rO   r   r   r   test_formatter_custom   s   
z$TestFormatters.test_formatter_customc                 C   s   d}|  |}|j}d}|| ksJ ||jddksJ d}||jddks*J ||jd dks4J d}||jdd	 dksBJ d S )
Nu%   <a href="http://a.com?a=b&c=é">e</a>u)   <a href="http://a.com?a=b&amp;c=é">e</a>rI   rJ   z/<a href="http://a.com?a=b&amp;c=&eacute;">e</a>r   u%   <a href="HTTP://A.COM?A=B&C=É">E</a>c                 S   r[   r\   r]   r_   r   r   r   r`      ra   zJTestFormatters.test_formatter_is_run_on_attribute_values.<locals>.<lambda>)r   arM   )r   r,   r   rc   expect_minimalexpect_htmlexpect_upperr   r   r   )test_formatter_is_run_on_attribute_values   s   
z8TestFormatters.test_formatter_is_run_on_attribute_valuesc                 C   "   d}t |d }d|v sJ d S )NzO
  <script type="text/javascript">
   console.log("< < hey > > ");
  </script>
html.parser   < < hey > >r   r   r   docr-   r   r   r   2test_formatter_skips_script_tag_for_html_documents      zATestFormatters.test_formatter_skips_script_tag_for_html_documentsc                 C   rh   )NzF
  <style type="text/css">
   console.log("< < hey > > ");
  </style>
ri   rj   rk   rl   r   r   r   1test_formatter_skips_style_tag_for_html_documents   ro   z@TestFormatters.test_formatter_skips_style_tag_for_html_documentsc                 C   s    |  d}d|j ksJ d S )NzN<div>  foo  <pre>  	bar
  
  </pre>  baz  <textarea> eee
fff	</textarea></div>zP<div>
 foo
 <pre>  	bar
  
  </pre>
 baz
 <textarea> eee
fff	</textarea>
</div>
r   divprettifyr   r   r   r   r   ,test_prettify_leaves_preformatted_text_alone   s   z;TestFormatters.test_prettify_leaves_preformatted_text_alonec                 C   s(   d}d}|  |}||j ksJ d S )NzR<div><pre><code>some
<script><pre>code</pre></script> for you 
</code></pre></div>zV<div>
 <pre><code>some
<script><pre>code</pre></script> for you 
</code></pre>
</div>
rq   )r   r,   expectr   r   r   r   0test_prettify_handles_nested_string_literal_tags   s   
z?TestFormatters.test_prettify_handles_nested_string_literal_tagsc                 C   s*   t dd}|jdd d}d|v sJ d S )Nz<html><body>foo</body></html>ri   c                 S   r[   r\   r]   r_   r   r   r   r`      ra   zITestFormatters.test_prettify_accepts_formatter_function.<locals>.<lambda>rJ   FOO)r   rs   )r   r   prettyr   r   r   (test_prettify_accepts_formatter_function   s   
z7TestFormatters.test_prettify_accepts_formatter_functionc                 C   s"   |  d}tt| u sJ d S )N<a></a>)r   r0   typers   rt   r   r   r   (test_prettify_outputs_unicode_by_default   s   
z7TestFormatters.test_prettify_outputs_unicode_by_defaultc                 C   s$   |  d}tt|du sJ d S )Nr{   r
   )r   bytesr|   rs   rt   r   r   r   test_prettify_can_encode_data   s   
z,TestFormatters.test_prettify_can_encode_datac                 C   s0   d}|  |}|jd}||dksJ d S )Nu   <b>Sacré bleu!</b>r
   r   )r   r,   r   r-   r   r   r   ,test_html_entity_substitution_off_by_default   s   
z;TestFormatters.test_html_entity_substitution_off_by_defaultc                 C   s   d}|  |}|jd dksJ |d}d|v sJ |d}d|v s&J |d}d	|v s1J |d
d
}d|v s?J d S )NzE<meta content="text/html; charset=x-sjis" http-equiv="Content-type"/>contentztext/html; charset=x-sjisr
   s   charset=utf-8euc_jps   charset=euc_jpz	shift-jiss   charset=shift-jiszutf-16zcharset=utf-16)r   metar   rM   )r   meta_tagr   utf_8r   	shift_jisutf_16_ur   r   r   test_encoding_substitution   s   



z)TestFormatters.test_encoding_substitutionc                 C   s2   d}t d}| j||d}|jd jdksJ d S )Nz`<head><meta content="text/html; charset=x-sjis" http-equiv="Content-type"/></head><pre>foo</pre>pre)
parse_onlyr   )r   r   contentsname)r   r,   strainerr   r   r   r   ;test_encoding_substitution_doesnt_happen_if_tag_is_strained  s
   zJTestFormatters.test_encoding_substitution_doesnt_happen_if_tag_is_strainedN)rA   rB   rC   rD   rQ   rU   rX   rY   rZ   rb   rg   rn   rp   ru   rw   rz   r}   r   r   r   r   r   r   r   r   rE   S   s&    	

	
		rE   c                   @   sh   e Zd Z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 )TestPersistencez*Testing features like pickle and deepcopy.c                 C   s   d| _ | | j | _d S )Nay  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Beautiful Soup: We called him Tortoise because he taught us.</title>
<link rev="made" href="mailto:leonardr@segfault.org">
<meta name="Description" content="Beautiful Soup: an HTML parser optimized for screen-scraping.">
<meta name="generator" content="Markov Approximation 1.4 (module: leonardr)">
<meta name="author" content="Leonard Richardson">
</head>
<body>
<a href="foo">foo</a>
<a href="foo"><b>bar</b></a>
</body>
</html>)pager   tree)r   r   r   r   setup_method  s   zTestPersistence.setup_methodc                 C   s@   t | jd}t |}|jtksJ | | j ksJ d S )N   )pickledumpsr   loads	__class__r   rM   )r   dumpedloadedr   r   r   !test_pickle_and_unpickle_identity&  s   
z1TestPersistence.test_pickle_and_unpickle_identityc                 C   s&   t | j}| | j ksJ d S r\   )copydeepcopyr   rM   )r   copiedr   r   r   test_deepcopy_identity.  s   z&TestPersistence.test_deepcopy_identityc                 C   s6   t  d }d| }| |}t| t| d S )Nr   r'   )r(   r)   r   r   r   )r   r+   r,   r   r   r   r    test_copy_deeply_nested_document3  s
   

z0TestPersistence.test_copy_deeply_nested_documentc                 C   s:   t dd}|j}| }dt|ksJ ||jksJ d S )Ns   <p>&nbsp;</p>ri   u	   <p> </p>)r   original_encoding__copy__r0   )r   r   r$   r   r   r   r   test_copy_preserves_encoding?  s
   
z,TestPersistence.test_copy_preserves_encodingc                 C   s   |  dj}d|_d|_| }|j|jksJ |j|jks J |j|jks(J |j|jks0J |j|jks8J |j|jks@J d S )Nz<p></p>
   !   )	r   p
sourceline	sourceposr   can_be_empty_elementcdata_list_attributespreserve_whitespace_tagsinteresting_string_types)r   tagr   r   r   r   'test_copy_preserves_builder_informationF  s   z7TestPersistence.test_copy_preserves_builder_informationc                 C   s>   d}|  |}t|tj}t|}| | ksJ d S r>   )r   r   r   HIGHEST_PROTOCOLr   rM   )r   r   r   r   r   r   r   r   test_unicode_pickleX  s
   

z#TestPersistence.test_unicode_picklec                 C   sz   d}|  |}|jdd}t|}||ksJ d |ju sJ d |ju s&J d |jus-J d |ju s4J d |ju s;J d S )Nz<b>Foo<a></a></b><b>Bar</b>Foor   )r   findr   parentnext_elementnext_siblingprevious_elementr   r   r   s1s2r   r   r   1test_copy_navigablestring_is_not_attached_to_tree`  s   

zATestPersistence.test_copy_navigablestring_is_not_attached_to_treec                 C   s<   d}|  |}|j}t|}||ksJ t|tsJ d S )Nz<b><!--Foo--></b>)r   r   r   
isinstancer   r   r   r   r   0test_copy_navigablestring_subclass_has_same_typel  s   

z@TestPersistence.test_copy_navigablestring_subclass_has_same_typec                 C   s(   d}|  |}t|}||ksJ d S )Nz)<div><b>Foo<a></a></b><b>Bar</b></div>end)r   r   )r   r   r   	soup_copyr   r   r   test_copy_entire_soupt  s   

z%TestPersistence.test_copy_entire_soupc                 C   s   d}|  |}|j}t|}t|t|ksJ ||ksJ ||us%J d |ju s,J d |ju s3J d |jddju s>J d |jddjusIJ |d |d usSJ |d d d	 |d ksdJ d	 |d ksnJ t
|d tswJ d S )Nz7<div class='a b c'><b>Foo<a></a></b><b>Bar</b></div>endBarr   classdza b c dza b c)r   rr   r   r0   r   r   r   r   appendsplitr   r   )r   r   r   rr   div_copyr   r   r   test_copy_tag_copies_contentsz  s    

z-TestPersistence.test_copy_tag_copies_contentsN)rA   rB   rC   rD   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s    r   c                   @   s   e Zd Zdd Zdd ZdS )TestEqualityc                 C   s`   |  d}|d\}}|jdd\}}||ksJ ||  kr%dks(J  J |dks.J d S )N<a>string</a> <a>string</a>rc   r   r   s   string)r   find_allr   r   first_asecond_afirst_stringsecond_stringr   r   r   test_comparison  s   
zTestEquality.test_comparisonc                 C   s|   |  d}|d\}}|jdd\}}t|t|ksJ t|t|ks(J t|t|  kr9tdks<J  J d S )Nr   rc   r   r   )r   r   hashidr   r   r   r   	test_hash  s   
,zTestEquality.test_hashN)rA   rB   rC   r   r   r   r   r   r   r     s    r   )rD   r   r   r   r(   r7   bs4r   bs4.elementr   r   
bs4.filterr    r   r   rE   r   r   r   r   r   r   <module>   s     ? > 