From cf15d10de1d8a6bff76d23e588eea4d7bc24e3e2 Mon Sep 17 00:00:00 2001 From: mozbugbox Date: Fri, 5 Jun 2015 19:27:48 +0800 Subject: BeautifulSoup 4: handle Doctype and Declaration bs4 can use lxml or html5lib to parse html content. Force bs4 builtin html parser when parse html with soupparser. --- src/lxml/html/tests/test_elementsoup.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'src/lxml/html/tests') diff --git a/src/lxml/html/tests/test_elementsoup.py b/src/lxml/html/tests/test_elementsoup.py index 2b19965d..d16a702e 100644 --- a/src/lxml/html/tests/test_elementsoup.py +++ b/src/lxml/html/tests/test_elementsoup.py @@ -1,11 +1,14 @@ import unittest, sys from lxml.tests.common_imports import make_doctest, HelperTestCase +BS_INSTALLED = True try: import BeautifulSoup - BS_INSTALLED = True except ImportError: - BS_INSTALLED = False + try: + import bs4 + except ImportError: + BS_INSTALLED = False from lxml.html import tostring @@ -24,21 +27,21 @@ if BS_INSTALLED: def test_body(self): html = '''

test

''' - res = '''

test

''' + res = b'''

test

''' tree = self.soupparser.fromstring(html) self.assertEqual(tostring(tree), res) def test_head_body(self): # HTML tag missing, parser should fix that html = 'test

test

' - res = 'test

test

' + res = b'test

test

' tree = self.soupparser.fromstring(html) self.assertEqual(tostring(tree), res) def test_wrap_html(self): # outside , parser should fix that html = 'title</test></head><html><body/></html>' - res = '<html><head><title>title' + res = b'title' tree = self.soupparser.fromstring(html) self.assertEqual(tostring(tree), res) @@ -47,7 +50,7 @@ if BS_INSTALLED: test

test

''' - res = ''' + res = b''' test

test

''' tree = self.soupparser.fromstring(html).getroottree() self.assertEqual(tostring(tree, method='html'), res) @@ -60,7 +63,7 @@ if BS_INSTALLED: My first HTML document

Hello world!

''' res = \ -''' +b''' My first HTML document

Hello world!

''' tree = self.soupparser.fromstring(html).getroottree() @@ -75,7 +78,7 @@ if BS_INSTALLED: My first HTML document

Hello world!

''' res = \ -''' +b''' My first HTML document

Hello world!

''' tree = self.soupparser.fromstring(html).getroottree() @@ -84,7 +87,7 @@ if BS_INSTALLED: def test_doctype_html5(self): # html 5 doctype declaration - html = '\n' + html = b'\n' tree = self.soupparser.fromstring(html).getroottree() self.assertTrue(tree.docinfo.public_id is None) -- cgit v1.2.1