Mercurial > repos > shellac > guppy_basecaller
comparison env/lib/python3.7/site-packages/bs4/tests/test_html5lib.py @ 0:26e78fe6e8c4 draft
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
| author | shellac |
|---|---|
| date | Sat, 02 May 2020 07:14:21 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:26e78fe6e8c4 |
|---|---|
| 1 """Tests to ensure that the html5lib tree builder generates good trees.""" | |
| 2 | |
| 3 import warnings | |
| 4 | |
| 5 try: | |
| 6 from bs4.builder import HTML5TreeBuilder | |
| 7 HTML5LIB_PRESENT = True | |
| 8 except ImportError as e: | |
| 9 HTML5LIB_PRESENT = False | |
| 10 from bs4.element import SoupStrainer | |
| 11 from bs4.testing import ( | |
| 12 HTML5TreeBuilderSmokeTest, | |
| 13 SoupTest, | |
| 14 skipIf, | |
| 15 ) | |
| 16 | |
| 17 @skipIf( | |
| 18 not HTML5LIB_PRESENT, | |
| 19 "html5lib seems not to be present, not testing its tree builder.") | |
| 20 class HTML5LibBuilderSmokeTest(SoupTest, HTML5TreeBuilderSmokeTest): | |
| 21 """See ``HTML5TreeBuilderSmokeTest``.""" | |
| 22 | |
| 23 @property | |
| 24 def default_builder(self): | |
| 25 return HTML5TreeBuilder | |
| 26 | |
| 27 def test_soupstrainer(self): | |
| 28 # The html5lib tree builder does not support SoupStrainers. | |
| 29 strainer = SoupStrainer("b") | |
| 30 markup = "<p>A <b>bold</b> statement.</p>" | |
| 31 with warnings.catch_warnings(record=True) as w: | |
| 32 soup = self.soup(markup, parse_only=strainer) | |
| 33 self.assertEqual( | |
| 34 soup.decode(), self.document_for(markup)) | |
| 35 | |
| 36 self.assertTrue( | |
| 37 "the html5lib tree builder doesn't support parse_only" in | |
| 38 str(w[0].message)) | |
| 39 | |
| 40 def test_correctly_nested_tables(self): | |
| 41 """html5lib inserts <tbody> tags where other parsers don't.""" | |
| 42 markup = ('<table id="1">' | |
| 43 '<tr>' | |
| 44 "<td>Here's another table:" | |
| 45 '<table id="2">' | |
| 46 '<tr><td>foo</td></tr>' | |
| 47 '</table></td>') | |
| 48 | |
| 49 self.assertSoupEquals( | |
| 50 markup, | |
| 51 '<table id="1"><tbody><tr><td>Here\'s another table:' | |
| 52 '<table id="2"><tbody><tr><td>foo</td></tr></tbody></table>' | |
| 53 '</td></tr></tbody></table>') | |
| 54 | |
| 55 self.assertSoupEquals( | |
| 56 "<table><thead><tr><td>Foo</td></tr></thead>" | |
| 57 "<tbody><tr><td>Bar</td></tr></tbody>" | |
| 58 "<tfoot><tr><td>Baz</td></tr></tfoot></table>") | |
| 59 | |
| 60 def test_xml_declaration_followed_by_doctype(self): | |
| 61 markup = '''<?xml version="1.0" encoding="utf-8"?> | |
| 62 <!DOCTYPE html> | |
| 63 <html> | |
| 64 <head> | |
| 65 </head> | |
| 66 <body> | |
| 67 <p>foo</p> | |
| 68 </body> | |
| 69 </html>''' | |
| 70 soup = self.soup(markup) | |
| 71 # Verify that we can reach the <p> tag; this means the tree is connected. | |
| 72 self.assertEqual(b"<p>foo</p>", soup.p.encode()) | |
| 73 | |
| 74 def test_reparented_markup(self): | |
| 75 markup = '<p><em>foo</p>\n<p>bar<a></a></em></p>' | |
| 76 soup = self.soup(markup) | |
| 77 self.assertEqual("<body><p><em>foo</em></p><em>\n</em><p><em>bar<a></a></em></p></body>", soup.body.decode()) | |
| 78 self.assertEqual(2, len(soup.find_all('p'))) | |
| 79 | |
| 80 | |
| 81 def test_reparented_markup_ends_with_whitespace(self): | |
| 82 markup = '<p><em>foo</p>\n<p>bar<a></a></em></p>\n' | |
| 83 soup = self.soup(markup) | |
| 84 self.assertEqual("<body><p><em>foo</em></p><em>\n</em><p><em>bar<a></a></em></p>\n</body>", soup.body.decode()) | |
| 85 self.assertEqual(2, len(soup.find_all('p'))) | |
| 86 | |
| 87 def test_reparented_markup_containing_identical_whitespace_nodes(self): | |
| 88 """Verify that we keep the two whitespace nodes in this | |
| 89 document distinct when reparenting the adjacent <tbody> tags. | |
| 90 """ | |
| 91 markup = '<table> <tbody><tbody><ims></tbody> </table>' | |
| 92 soup = self.soup(markup) | |
| 93 space1, space2 = soup.find_all(string=' ') | |
| 94 tbody1, tbody2 = soup.find_all('tbody') | |
| 95 assert space1.next_element is tbody1 | |
| 96 assert tbody2.next_element is space2 | |
| 97 | |
| 98 def test_reparented_markup_containing_children(self): | |
| 99 markup = '<div><a>aftermath<p><noscript>target</noscript>aftermath</a></p></div>' | |
| 100 soup = self.soup(markup) | |
| 101 noscript = soup.noscript | |
| 102 self.assertEqual("target", noscript.next_element) | |
| 103 target = soup.find(string='target') | |
| 104 | |
| 105 # The 'aftermath' string was duplicated; we want the second one. | |
| 106 final_aftermath = soup.find_all(string='aftermath')[-1] | |
| 107 | |
| 108 # The <noscript> tag was moved beneath a copy of the <a> tag, | |
| 109 # but the 'target' string within is still connected to the | |
| 110 # (second) 'aftermath' string. | |
| 111 self.assertEqual(final_aftermath, target.next_element) | |
| 112 self.assertEqual(target, final_aftermath.previous_element) | |
| 113 | |
| 114 def test_processing_instruction(self): | |
| 115 """Processing instructions become comments.""" | |
| 116 markup = b"""<?PITarget PIContent?>""" | |
| 117 soup = self.soup(markup) | |
| 118 assert str(soup).startswith("<!--?PITarget PIContent?-->") | |
| 119 | |
| 120 def test_cloned_multivalue_node(self): | |
| 121 markup = b"""<a class="my_class"><p></a>""" | |
| 122 soup = self.soup(markup) | |
| 123 a1, a2 = soup.find_all('a') | |
| 124 self.assertEqual(a1, a2) | |
| 125 assert a1 is not a2 | |
| 126 | |
| 127 def test_foster_parenting(self): | |
| 128 markup = b"""<table><td></tbody>A""" | |
| 129 soup = self.soup(markup) | |
| 130 self.assertEqual("<body>A<table><tbody><tr><td></td></tr></tbody></table></body>", soup.body.decode()) | |
| 131 | |
| 132 def test_extraction(self): | |
| 133 """ | |
| 134 Test that extraction does not destroy the tree. | |
| 135 | |
| 136 https://bugs.launchpad.net/beautifulsoup/+bug/1782928 | |
| 137 """ | |
| 138 | |
| 139 markup = """ | |
| 140 <html><head></head> | |
| 141 <style> | |
| 142 </style><script></script><body><p>hello</p></body></html> | |
| 143 """ | |
| 144 soup = self.soup(markup) | |
| 145 [s.extract() for s in soup('script')] | |
| 146 [s.extract() for s in soup('style')] | |
| 147 | |
| 148 self.assertEqual(len(soup.find_all("p")), 1) | |
| 149 | |
| 150 def test_empty_comment(self): | |
| 151 """ | |
| 152 Test that empty comment does not break structure. | |
| 153 | |
| 154 https://bugs.launchpad.net/beautifulsoup/+bug/1806598 | |
| 155 """ | |
| 156 | |
| 157 markup = """ | |
| 158 <html> | |
| 159 <body> | |
| 160 <form> | |
| 161 <!----><input type="text"> | |
| 162 </form> | |
| 163 </body> | |
| 164 </html> | |
| 165 """ | |
| 166 soup = self.soup(markup) | |
| 167 inputs = [] | |
| 168 for form in soup.find_all('form'): | |
| 169 inputs.extend(form.find_all('input')) | |
| 170 self.assertEqual(len(inputs), 1) | |
| 171 | |
| 172 def test_tracking_line_numbers(self): | |
| 173 # The html.parser TreeBuilder keeps track of line number and | |
| 174 # position of each element. | |
| 175 markup = "\n <p>\n\n<sourceline>\n<b>text</b></sourceline><sourcepos></p>" | |
| 176 soup = self.soup(markup) | |
| 177 self.assertEqual(2, soup.p.sourceline) | |
| 178 self.assertEqual(5, soup.p.sourcepos) | |
| 179 self.assertEqual("sourceline", soup.p.find('sourceline').name) | |
| 180 | |
| 181 # You can deactivate this behavior. | |
| 182 soup = self.soup(markup, store_line_numbers=False) | |
| 183 self.assertEqual("sourceline", soup.p.sourceline.name) | |
| 184 self.assertEqual("sourcepos", soup.p.sourcepos.name) | |
| 185 | |
| 186 def test_special_string_containers(self): | |
| 187 # The html5lib tree builder doesn't support this standard feature, | |
| 188 # because there's no way of knowing, when a string is created, | |
| 189 # where in the tree it will eventually end up. | |
| 190 pass |
