diff options
author | Leonard Richardson <leonardr@segfault.org> | 2019-08-21 18:36:32 -0400 |
---|---|---|
committer | Leonard Richardson <leonardr@segfault.org> | 2019-08-21 18:36:32 -0400 |
commit | c9aef4f30b233ee3b1b5a6822751cf33b0fb05ed (patch) | |
tree | 31c8167bfc02f45d70fba0614d9a1139f3751cce | |
parent | 25a4efbf0aedba46687a2c151f1230eeeaacea35 (diff) |
Fixed a crash when pretty-printing tags that were not created
during initial parsing. [bug=1838903]
-rw-r--r-- | CHANGELOG | 3 | ||||
-rw-r--r-- | bs4/element.py | 5 | ||||
-rw-r--r-- | bs4/tests/test_tree.py | 24 |
3 files changed, 31 insertions, 1 deletions
@@ -6,6 +6,9 @@ and position within a line (Tag.sourcepos). Based on code by Chris Mayo. [bug=1742921] +* Fixed a bug that made it impossible to pretty-print tags that were not + created during initial parsing. [bug=1838903] + = 4.8.0 (20190720, "One Small Soup") This release focuses on making it easier to customize Beautiful Soup's diff --git a/bs4/element.py b/bs4/element.py index a57027e..07bd893 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -1173,7 +1173,10 @@ class Tag(PageElement): """Should this tag be pretty-printed?""" return ( indent_level is not None - and self.name not in self.preserve_whitespace_tags + and ( + not self.preserve_whitespace_tags + or self.name not in self.preserve_whitespace_tags + ) ) def prettify(self, encoding=None, formatter="minimal"): diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py index e655dcc..c995c46 100644 --- a/bs4/tests/test_tree.py +++ b/bs4/tests/test_tree.py @@ -741,6 +741,30 @@ class TestPreviousSibling(SiblingTest): self.assertEqual(start.find_previous_sibling(text="nonesuch"), None) +class TestTag(SoupTest): + + # Test various methods of Tag. + + def test__should_pretty_print(self): + # Test the rules about when a tag should be pretty-printed. + tag = self.soup("").new_tag("a_tag") + + # No list of whitespace-preserving tags -> pretty-print + tag._preserve_whitespace_tags = None + self.assertEquals(True, tag._should_pretty_print(0)) + + # List exists but tag is not on the list -> pretty-print + tag.preserve_whitespace_tags = ["some_other_tag"] + self.assertEquals(True, tag._should_pretty_print(1)) + + # Indent level is None -> don't pretty-print + self.assertEquals(False, tag._should_pretty_print(None)) + + # Tag is on the whitespace-preserving list -> don't pretty-print + tag.preserve_whitespace_tags = ["some_other_tag", "a_tag"] + self.assertEquals(False, tag._should_pretty_print(1)) + + class TestTagCreation(SoupTest): """Test the ability to create new tags.""" def test_new_tag(self): |