summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeonard Richardson <leonardr@segfault.org>2023-04-17 09:34:58 -0400
committerLeonard Richardson <leonardr@segfault.org>2023-04-17 09:34:58 -0400
commit31eb728f84cd795f432838ba58d2bbf9502520db (patch)
tree9937db86e970e80b4ebf6a13a3abd3db5f7da007
parentc4a7d44e76b1784185946682d4111759f48ac0bd (diff)
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.
-rw-r--r--CHANGELOG7
-rw-r--r--bs4/element.py5
-rw-r--r--bs4/tests/test_tag.py13
3 files changed, 25 insertions, 0 deletions
diff --git a/CHANGELOG b/CHANGELOG
index f6a7258..94f1209 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -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)