1
"""Tests to ensure that the html5lib tree builder generates good trees."""
6
from bs4.builder import HTML5TreeBuilder
7
HTML5LIB_PRESENT = True
9
HTML5LIB_PRESENT = False
10
from bs4.element import SoupStrainer
11
from bs4.testing import (
12
HTML5TreeBuilderSmokeTest,
19
"html5lib seems not to be present, not testing its tree builder.")
20
class HTML5LibBuilderSmokeTest(SoupTest, HTML5TreeBuilderSmokeTest):
21
"""See ``HTML5TreeBuilderSmokeTest``."""
24
def default_builder(self):
25
return HTML5TreeBuilder()
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)
34
soup.decode(), self.document_for(markup))
37
"the html5lib tree builder doesn't support parse_only" in
40
def test_correctly_nested_tables(self):
41
"""html5lib inserts <tbody> tags where other parsers don't."""
42
markup = ('<table id="1">'
44
"<td>Here's another table:"
46
'<tr><td>foo</td></tr>'
49
self.assertSoupEquals(
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>')
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>")
60
def test_xml_declaration_followed_by_doctype(self):
61
markup = '''<?xml version="1.0" encoding="utf-8"?>
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())
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(u"<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')))
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(u"<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')))