diff options
-rw-r--r-- | CHANGELOG | 7 | ||||
-rw-r--r-- | bs4/element.py | 5 | ||||
-rw-r--r-- | bs4/tests/test_tag.py | 13 |
3 files changed, 25 insertions, 0 deletions
@@ -1,3 +1,10 @@ += 4.12.3 (?) + +* Fixed a regression such that if you set .hidden on a tag, the tag + becomes invisible but its contents are still visible. User manipulation + of .hidden is not a documented or supported feature, so don't do this, + but it's not too difficult to keep the old behavior working. + = 4.12.2 (20230407) * Fixed an unhandled exception in BeautifulSoup.decode_contents diff --git a/bs4/element.py b/bs4/element.py index 33cbc19..0aefe73 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -1845,6 +1845,11 @@ class Tag(PageElement): return space_before + s + space_after def _format_tag(self, eventual_encoding, formatter, opening): + if self.hidden: + # A hidden tag is invisible, although its contents + # are visible. + return '' + # A tag starts with the < character (see below). # Then the / character, if this is a closing tag. diff --git a/bs4/tests/test_tag.py b/bs4/tests/test_tag.py index 88cc202..c48f334 100644 --- a/bs4/tests/test_tag.py +++ b/bs4/tests/test_tag.py @@ -219,3 +219,16 @@ class TestMultiValuedAttributes(SoupTest): ) assert soup.a['class'] == 'foo' assert soup.a['id'] == ['bar'] + + def test_hidden_tag_is_invisible(self): + # Setting .hidden on a tag makes it invisible in output, but + # leaves its contents visible. + # + # This is not a documented or supported feature of Beautiful + # Soup (e.g. NavigableString doesn't support .hidden even + # though it could), but some people use it and it's not + # hurting anything to verify that it keeps working. + # + soup = self.soup('<div id="1"><span id="2">a string</span></div>') + soup.span.hidden = True + assert '<div id="1">a string</div>' == str(soup.div) |