diff options
author | Leonard Richardson <leonardr@segfault.org> | 2020-09-26 11:18:12 -0400 |
---|---|---|
committer | Leonard Richardson <leonardr@segfault.org> | 2020-09-26 11:18:12 -0400 |
commit | 4eef28adde8d1b09dda10b1f6c4f8f896a2b9393 (patch) | |
tree | d7f3b1e645f4fa3afd128a5d05fce939bc36de90 | |
parent | e37c55b111b6d3ece22237188ad77a1ea9cd3724 (diff) |
Fixed a bug that inconsistently moved elements over when passing
a Tag, rather than a list, into Tag.extend(). [bug=1885710]
-rw-r--r-- | CHANGELOG | 5 | ||||
-rw-r--r-- | bs4/__init__.py | 4 | ||||
-rw-r--r-- | bs4/element.py | 5 | ||||
-rw-r--r-- | bs4/tests/test_tree.py | 9 | ||||
-rw-r--r-- | doc/source/index.rst | 4 |
5 files changed, 23 insertions, 4 deletions
@@ -1,9 +1,12 @@ -= Unreleased += 4.9.2 (Unreleased) * Fixed a bug that caused too many tags to be popped from the tag stack during tree building, when encountering a closing tag that had no matching opening tag. [bug=1880420] +* Fixed a bug that inconsistently moved elements over when passing + a Tag, rather than a list, into Tag.extend(). [bug=1885710] + * Specify the soupsieve dependency in a way that complies with PEP 508. Patch by Mike Nerone. [bug=1893696] diff --git a/bs4/__init__.py b/bs4/__init__.py index 73109d6..2b82864 100644 --- a/bs4/__init__.py +++ b/bs4/__init__.py @@ -253,7 +253,9 @@ class BeautifulSoup(Tag): if not original_builder and not ( original_features == builder.NAME or original_features in builder.ALTERNATE_NAMES - ): + ) and markup: + # The user did not tell us which TreeBuilder to use, + # and we had to guess. Issue a warning. if builder.is_xml: markup_type = "XML" else: diff --git a/bs4/element.py b/bs4/element.py index 130dfa9..370b153 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -457,6 +457,11 @@ class PageElement(object): :param tags: A list of PageElements. """ + 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) for tag in tags: self.append(tag) diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py index 7981130..2246346 100644 --- a/bs4/tests/test_tree.py +++ b/bs4/tests/test_tree.py @@ -1009,6 +1009,15 @@ class TestTreeModification(SoupTest): soup.a.extend(l) self.assertEqual("<a><g></g><f></f><e></e><d></d><c></c><b></b></a>", soup.decode()) + def test_extend_with_another_tags_contents(self): + 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) + self.assertEqual(u'<div id="d1"></div>', d1.decode()) + self.assertEqual(u'<div id="d2"><a>1</a><a>2</a><a>3</a><a>4</a></div>', d2.decode()) + def test_move_tag_to_beginning_of_parent(self): data = "<a><b></b><c></c><d></d></a>" soup = self.soup(data) diff --git a/doc/source/index.rst b/doc/source/index.rst index 76a32e9..ec3add9 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -1915,8 +1915,8 @@ like calling ``.append()`` on a Python list:: ------------ Starting in Beautiful Soup 4.7.0, ``Tag`` also supports a method -called ``.extend()``, which works just like calling ``.extend()`` on a -Python list:: +called ``.extend()``, which adds every element of a list to a ``Tag``, +in order:: soup = BeautifulSoup("<a>Soup</a>", 'html.parser') soup.a.extend(["'s", " ", "on"]) |