summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeonard Richardson <leonardr@segfault.org>2023-01-25 14:50:44 -0500
committerLeonard Richardson <leonardr@segfault.org>2023-01-25 14:50:44 -0500
commit9f29d5b62539d8020b3e99e084c52a45996b7403 (patch)
treef0b475ecbbf521c8716e7cd9676b4aa34169d8d2
parente4f967730af7d5ea59f6a05d03c779df16a24bd3 (diff)
Passing a Tag's .contents into PageElement.extend() now works the
same way as passing the Tag itself.
-rw-r--r--CHANGELOG8
-rw-r--r--bs4/element.py13
-rw-r--r--bs4/tests/test_tree.py8
3 files changed, 20 insertions, 9 deletions
diff --git a/CHANGELOG b/CHANGELOG
index ac7882b..3fe20e6 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,7 +1,8 @@
Beautiful Soup's official support for Python 2 ended on January 1st,
2021. The final release to support Python 2 was Beautiful Soup
-4.9.3. In the Launchpad Bazaar repository, the final revision to support
-Python 2 was revision 605.
+4.9.3. In the Launchpad Git repository, the final revision to support
+Python 2 was revision 70f546b1e689a70e2f103795efce6d261a3dadf7. In the
+Bazaar repository, the final revision to support Python 2 was 605.
= Unreleased
@@ -11,6 +12,9 @@ Python 2 was revision 605.
* Fixed another crash when overriding multi_valued_attributes and using the
html5lib parser. [bug=1948488]
+* Passing a Tag's .contents into PageElement.extend() now works the
+ same way as passing the Tag itself.
+
= 4.11.1 (20220408)
This release was done to ensure that the unit tests are packaged along
diff --git a/bs4/element.py b/bs4/element.py
index 74b1dc0..98e978f 100644
--- a/bs4/element.py
+++ b/bs4/element.py
@@ -496,13 +496,16 @@ class PageElement(object):
def extend(self, tags):
"""Appends the given PageElements to this one's contents.
- :param tags: A list of PageElements.
+ :param tags: A list of PageElements. If a single Tag is
+ provided instead, this PageElement's contents will be extended
+ with that Tag's contents.
"""
if isinstance(tags, Tag):
- # Calling self.append() on another tag's contents will change
- # the list we're iterating over. Make a list that won't
- # change.
- tags = list(tags.contents)
+ tags = tags.contents
+ if isinstance(tags, list):
+ # Moving items around the tree may change their position in
+ # the original list. Make a list that won't change.
+ tags = list(tags)
for tag in tags:
self.append(tag)
diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py
index bfd6826..f40e764 100644
--- a/bs4/tests/test_tree.py
+++ b/bs4/tests/test_tree.py
@@ -910,12 +910,16 @@ class TestTreeModification(SoupTest):
soup.a.extend(l)
assert "<a><g></g><f></f><e></e><d></d><c></c><b></b></a>" == soup.decode()
- def test_extend_with_another_tags_contents(self):
+ @pytest.mark.parametrize(
+ "get_tags", [lambda tag: tag, lambda tag: tag.contents]
+ )
+ def test_extend_with_another_tags_contents(self, tags):
data = '<body><div id="d1"><a>1</a><a>2</a><a>3</a><a>4</a></div><div id="d2"></div></body>'
soup = self.soup(data)
d1 = soup.find('div', id='d1')
d2 = soup.find('div', id='d2')
- d2.extend(d1)
+ tags = get_tags(d1)
+ d2.extend(tags)
assert '<div id="d1"></div>' == d1.decode()
assert '<div id="d2"><a>1</a><a>2</a><a>3</a><a>4</a></div>' == d2.decode()