o
    Rh7                     @   s   d 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	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dS )z*Tests of the bs4.element.PageElement class    N)BeautifulSoup)Comment	ResultSet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   \/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/bs4/tests/test_pageelement.py"test_unicode_string_can_be_encoded   s   
 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_contents.   s
   
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_document5   s
   
z/TestEncoding.test_encode_deeply_nested_documentc                 C   s2   d}|  |}|  dd|j ksJ d S )Nr	   r   r"   )r   renderContentsr   r   r   r   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_reprE   s   
zTestEncoding.test_reprN)__name__
__module____qualname____doc__r   r   r   r   r!   r%   r-   r/   r2   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_minimalP      
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   r<   z.<br/><b>&lt;&lt;Sacr&eacute; bleu!&gt;&gt;</b>r>   rA   r   r   r   test_formatter_htmlY      
z"TestFormatters.test_formatter_htmlc                 C   rE   )NrF   html5r<   z-<br><b>&lt;&lt;Sacr&eacute; bleu!&gt;&gt;</b>r>   rA   r   r   r   test_formatter_html5a   rH   z#TestFormatters.test_formatter_html5c                 C   r8   r9   r>   rA   r   r   r   test_formatter_minimali   rD   z%TestFormatters.test_formatter_minimalc                 C   s0   d}|  |}|jd d}|| dksJ d S )Nr:   r<   u   <b><<Sacré bleu!>></b>r>   rA   r   r   r   test_formatter_nullr   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xr   r   r   <lambda>       z6TestFormatters.test_formatter_custom.<locals>.<lambda>r<   z<b><FOO></b><b>BAR</b><br/>r>   rA   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>r;   r<   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   rM   rN   rO   rQ   r   r   r   rS      rT   zJTestFormatters.test_formatter_is_run_on_attribute_values.<locals>.<lambda>)r   ar?   )r   r+   r   rV   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   r[   )NzF
  <style type="text/css">
   console.log("< < hey > > ");
  </style>
r\   r]   r^   r_   r   r   r   1test_formatter_skips_style_tag_for_html_documents   rb   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>
rd   )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>r\   c                 S   rM   rN   rO   rQ   r   r   r   rS      rT   zITestFormatters.test_prettify_accepts_formatter_function.<locals>.<lambda>r<   FOO)r   rf   )r   r   prettyr   r   r   (test_prettify_accepts_formatter_function   s   
z7TestFormatters.test_prettify_accepts_formatter_functionc                 C   s"   |  d}tt| ksJ d S )N<a></a>)r   strtyperf   rg   r   r   r   (test_prettify_outputs_unicode_by_default   s   
z7TestFormatters.test_prettify_outputs_unicode_by_defaultc                 C   s$   |  d}tt|dksJ d S )Nrn   r
   )r   bytesrp   rf   rg   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   r?   )r   meta_tagr   utf_8rv   	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)r3   r4   r5   r6   rC   rG   rJ   rK   rL   rU   rZ   ra   rc   rh   rj   rm   rq   rs   rt   r|   r   r   r   r   r   r7   K   s&    		
		r7   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   r?   )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 rN   )copydeepcopyr   r?   )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   r    test_copy_deeply_nested_document  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>r\   u	   <p> </p>)r   original_encoding__copy__ro   )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_information2  s   z7TestPersistence.test_copy_preserves_builder_informationc                 C   s>   d}|  |}t|tj}t|}| | ksJ d S r0   )r   r   r   HIGHEST_PROTOCOLr   r?   )r   r   r   r   r   r   r   r   test_unicode_pickleE  s
   

z#TestPersistence.test_unicode_picklec                 C   sz   d}|  |}|jdd}t|}||ksJ d |jksJ d |jks&J d |jks-J d |jks4J d |jk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_treeM  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_typeY  s   

z@TestPersistence.test_copy_navigablestring_subclass_has_same_typec                 C   s(   d}|  |}t|}||ksJ d S )N)<div><b>Foo<a></a></b><b>Bar</b></div>end)r   r   )r   r   r   	soup_copyr   r   r   test_copy_entire_soupa  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ks,J d |jks3J d |jddjks>J d |jddjksIJ d S )Nr   Barr   )r   re   r   ro   r   r   r   r   )r   r   r   re   div_copyr   r   r   test_copy_tag_copies_contentsg  s   

z-TestPersistence.test_copy_tag_copies_contentsN)r3   r4   r5   r6   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s    r   )r6   r   r   r   r'   bs4r   bs4.elementr   r   r    r   r   r7   r   r   r   r   r   <module>   s    : 2