Path: blob/master/venv/Lib/site-packages/bs4/tests/test_html5lib.py
808 views
"""Tests to ensure that the html5lib tree builder generates good trees."""12import warnings34try:5from bs4.builder import HTML5TreeBuilder6HTML5LIB_PRESENT = True7except ImportError as e:8HTML5LIB_PRESENT = False9from bs4.element import SoupStrainer10from bs4.testing import (11HTML5TreeBuilderSmokeTest,12SoupTest,13skipIf,14)1516@skipIf(17not HTML5LIB_PRESENT,18"html5lib seems not to be present, not testing its tree builder.")19class HTML5LibBuilderSmokeTest(SoupTest, HTML5TreeBuilderSmokeTest):20"""See ``HTML5TreeBuilderSmokeTest``."""2122@property23def default_builder(self):24return HTML5TreeBuilder2526def test_soupstrainer(self):27# The html5lib tree builder does not support SoupStrainers.28strainer = SoupStrainer("b")29markup = "<p>A <b>bold</b> statement.</p>"30with warnings.catch_warnings(record=True) as w:31soup = self.soup(markup, parse_only=strainer)32self.assertEqual(33soup.decode(), self.document_for(markup))3435self.assertTrue(36"the html5lib tree builder doesn't support parse_only" in37str(w[0].message))3839def test_correctly_nested_tables(self):40"""html5lib inserts <tbody> tags where other parsers don't."""41markup = ('<table id="1">'42'<tr>'43"<td>Here's another table:"44'<table id="2">'45'<tr><td>foo</td></tr>'46'</table></td>')4748self.assertSoupEquals(49markup,50'<table id="1"><tbody><tr><td>Here\'s another table:'51'<table id="2"><tbody><tr><td>foo</td></tr></tbody></table>'52'</td></tr></tbody></table>')5354self.assertSoupEquals(55"<table><thead><tr><td>Foo</td></tr></thead>"56"<tbody><tr><td>Bar</td></tr></tbody>"57"<tfoot><tr><td>Baz</td></tr></tfoot></table>")5859def test_xml_declaration_followed_by_doctype(self):60markup = '''<?xml version="1.0" encoding="utf-8"?>61<!DOCTYPE html>62<html>63<head>64</head>65<body>66<p>foo</p>67</body>68</html>'''69soup = self.soup(markup)70# Verify that we can reach the <p> tag; this means the tree is connected.71self.assertEqual(b"<p>foo</p>", soup.p.encode())7273def test_reparented_markup(self):74markup = '<p><em>foo</p>\n<p>bar<a></a></em></p>'75soup = self.soup(markup)76self.assertEqual("<body><p><em>foo</em></p><em>\n</em><p><em>bar<a></a></em></p></body>", soup.body.decode())77self.assertEqual(2, len(soup.find_all('p')))787980def test_reparented_markup_ends_with_whitespace(self):81markup = '<p><em>foo</p>\n<p>bar<a></a></em></p>\n'82soup = self.soup(markup)83self.assertEqual("<body><p><em>foo</em></p><em>\n</em><p><em>bar<a></a></em></p>\n</body>", soup.body.decode())84self.assertEqual(2, len(soup.find_all('p')))8586def test_reparented_markup_containing_identical_whitespace_nodes(self):87"""Verify that we keep the two whitespace nodes in this88document distinct when reparenting the adjacent <tbody> tags.89"""90markup = '<table> <tbody><tbody><ims></tbody> </table>'91soup = self.soup(markup)92space1, space2 = soup.find_all(string=' ')93tbody1, tbody2 = soup.find_all('tbody')94assert space1.next_element is tbody195assert tbody2.next_element is space29697def test_reparented_markup_containing_children(self):98markup = '<div><a>aftermath<p><noscript>target</noscript>aftermath</a></p></div>'99soup = self.soup(markup)100noscript = soup.noscript101self.assertEqual("target", noscript.next_element)102target = soup.find(string='target')103104# The 'aftermath' string was duplicated; we want the second one.105final_aftermath = soup.find_all(string='aftermath')[-1]106107# The <noscript> tag was moved beneath a copy of the <a> tag,108# but the 'target' string within is still connected to the109# (second) 'aftermath' string.110self.assertEqual(final_aftermath, target.next_element)111self.assertEqual(target, final_aftermath.previous_element)112113def test_processing_instruction(self):114"""Processing instructions become comments."""115markup = b"""<?PITarget PIContent?>"""116soup = self.soup(markup)117assert str(soup).startswith("<!--?PITarget PIContent?-->")118119def test_cloned_multivalue_node(self):120markup = b"""<a class="my_class"><p></a>"""121soup = self.soup(markup)122a1, a2 = soup.find_all('a')123self.assertEqual(a1, a2)124assert a1 is not a2125126def test_foster_parenting(self):127markup = b"""<table><td></tbody>A"""128soup = self.soup(markup)129self.assertEqual("<body>A<table><tbody><tr><td></td></tr></tbody></table></body>", soup.body.decode())130131def test_extraction(self):132"""133Test that extraction does not destroy the tree.134135https://bugs.launchpad.net/beautifulsoup/+bug/1782928136"""137138markup = """139<html><head></head>140<style>141</style><script></script><body><p>hello</p></body></html>142"""143soup = self.soup(markup)144[s.extract() for s in soup('script')]145[s.extract() for s in soup('style')]146147self.assertEqual(len(soup.find_all("p")), 1)148149def test_empty_comment(self):150"""151Test that empty comment does not break structure.152153https://bugs.launchpad.net/beautifulsoup/+bug/1806598154"""155156markup = """157<html>158<body>159<form>160<!----><input type="text">161</form>162</body>163</html>164"""165soup = self.soup(markup)166inputs = []167for form in soup.find_all('form'):168inputs.extend(form.find_all('input'))169self.assertEqual(len(inputs), 1)170171def test_tracking_line_numbers(self):172# The html.parser TreeBuilder keeps track of line number and173# position of each element.174markup = "\n <p>\n\n<sourceline>\n<b>text</b></sourceline><sourcepos></p>"175soup = self.soup(markup)176self.assertEqual(2, soup.p.sourceline)177self.assertEqual(5, soup.p.sourcepos)178self.assertEqual("sourceline", soup.p.find('sourceline').name)179180# You can deactivate this behavior.181soup = self.soup(markup, store_line_numbers=False)182self.assertEqual("sourceline", soup.p.sourceline.name)183self.assertEqual("sourcepos", soup.p.sourcepos.name)184185def test_special_string_containers(self):186# The html5lib tree builder doesn't support this standard feature,187# because there's no way of knowing, when a string is created,188# where in the tree it will eventually end up.189pass190191192