"
soup = self.soup(markup)
self.assertEquals(soup.div.contents[0], CData("foo"))
def test_attribute_value_never_got_closed(self):
markup = '
')
def test_nonexistent_entity(self):
soup = self.soup("foobar;baz
")
# This is very strange.
self.assertEqual(soup.p.string, "foofoodbaz
")
self.assertEqual(soup.p.string, "foodbaz")
# Also compare html5lib, which preserves the before the
# entity name.
def test_entity_out_of_range(self):
# An entity that's out of range will be replaced with
# REPLACEMENT CHARACTER.
soup = self.soup("")
self.assertEqual(soup.p.string, u"\N{REPLACEMENT CHARACTER}")
soup = self.soup("")
self.assertEqual(soup.p.string, u"\N{REPLACEMENT CHARACTER}")
soup = self.soup("")
self.assertEqual(soup.p.string, u"\N{REPLACEMENT CHARACTER}")
def test_entity_was_not_finished(self):
soup = self.soup("<Hello>")
# Compare html5lib, which completes the entity.
self.assertEqual(soup.p.string, "aa
')
def test_tag_name_contains_unicode(self):
# Unicode characters in tag names are stripped.
tag_name = u"Joe"
self.assertSoupEquals("Joe")
def test_multiple_values_for_the_same_attribute(self):
markup = ''
self.assertSoupEquals(markup, '')
class TestHTMLParserTreeBuilderEncodingConversion(SoupTest):
# Test Beautiful Soup's ability to decode and encode from various
# encodings.
@property
def default_builder(self):
return HTMLParserTreeBuilder()
def setUp(self):
super(TestHTMLParserTreeBuilderEncodingConversion, self).setUp()
self.unicode_data = u"Sacr\N{LATIN SMALL LETTER E WITH ACUTE} bleu!"
self.utf8_data = self.unicode_data.encode("utf-8")
# Just so you know what it looks like.
self.assertEqual(
self.utf8_data,
b"Sacr\xc3\xa9 bleu!")
def test_ascii_in_unicode_out(self):
# ASCII input is converted to Unicode. The original_encoding
# attribute is set.
ascii = b"a"
soup_from_ascii = self.soup(ascii)
unicode_output = soup_from_ascii.decode()
self.assertTrue(isinstance(unicode_output, unicode))
self.assertEqual(unicode_output, self.document_for(ascii.decode()))
self.assertEqual(soup_from_ascii.original_encoding, "ascii")
def test_unicode_in_unicode_out(self):
# Unicode input is left alone. The original_encoding attribute
# is not set.
soup_from_unicode = self.soup(self.unicode_data)
self.assertEqual(soup_from_unicode.decode(), self.unicode_data)
self.assertEqual(soup_from_unicode.foo.string, u'Sacr\xe9 bleu!')
self.assertEqual(soup_from_unicode.original_encoding, None)
def test_utf8_in_unicode_out(self):
# UTF-8 input is converted to Unicode. The original_encoding
# attribute is set.
soup_from_utf8 = self.soup(self.utf8_data)
self.assertEqual(soup_from_utf8.decode(), self.unicode_data)
self.assertEqual(soup_from_utf8.foo.string, u'Sacr\xe9 bleu!')
def test_utf8_out(self):
# The internal data structures can be encoded as UTF-8.
soup_from_unicode = self.soup(self.unicode_data)
self.assertEqual(soup_from_unicode.encode('utf-8'), self.utf8_data)
HEBREW_DOCUMENT = b'Hebrew (ISO 8859-8) in Visual DirectionalityHebrew (ISO 8859-8) in Visual Directionality
\xed\xe5\xec\xf9'
def test_real_hebrew_document(self):
# A real-world test to make sure we can convert ISO-8859-9 (a
# Hebrew encoding) to UTF-8.
soup = self.soup(self.HEBREW_DOCUMENT,
from_encoding="iso-8859-8")
self.assertEqual(soup.original_encoding, 'iso-8859-8')
self.assertEqual(
soup.encode('utf-8'),
self.HEBREW_DOCUMENT.decode("iso-8859-8").encode("utf-8"))