From 9c99d3050584a088b1d066aca0c21d04f87775b0 Mon Sep 17 00:00:00 2001 From: Leonard Richardson Date: Sun, 6 Jan 2019 18:49:34 -0500 Subject: Fixed an incorrectly raised exception when inserting a tag before or after an identical tag. [bug=1810692] --- CHANGELOG | 10 ++++++++++ bs4/element.py | 4 ++-- bs4/tests/test_tree.py | 8 ++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index b778f6d..389b949 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,13 @@ += 4.7.1 (Unreleased) + +* Fixed a significant performance problem introduced in 4.7.0. [bug=1810617] + +* Fixed an incorrectly raised exception when inserting a tag before or + after an identical tag. [bug=1810692] + +* Beautiful Soup will no longer try to keep track of namespaces that + are not defined with a prefix; this can confuse soupselect. [bug=1810680] + = 4.7.0 (20181231) * Beautiful Soup's CSS Selector implementation has been replaced by a diff --git a/bs4/element.py b/bs4/element.py index c367dee..5718d31 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -455,7 +455,7 @@ class PageElement(object): if parent is None: raise ValueError( "Element has no parent, so 'before' has no meaning.") - if self in args: + if any(x is self for x in args): raise ValueError("Can't insert an element before itself.") for predecessor in args: # Extract first so that the index won't be screwed up if they @@ -476,7 +476,7 @@ class PageElement(object): if parent is None: raise ValueError( "Element has no parent, so 'after' has no meaning.") - if self in args: + if any(x is self for x in args): raise ValueError("Can't insert an element after itself.") offset = 0 diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py index 2290558..6d79454 100644 --- a/bs4/tests/test_tree.py +++ b/bs4/tests/test_tree.py @@ -971,6 +971,10 @@ class TestTreeModification(SoupTest): # Can't insert before if an element has no parent. b.extract() self.assertRaises(ValueError, b.insert_before, "nope") + + # Can insert an identical element + soup = self.soup("") + soup.a.insert_before(soup.new_tag("a")) def test_insert_multiple_before(self): soup = self.soup("foobar") @@ -1000,6 +1004,10 @@ class TestTreeModification(SoupTest): # Can't insert after if an element has no parent. b.extract() self.assertRaises(ValueError, b.insert_after, "nope") + + # Can insert an identical element + soup = self.soup("") + soup.a.insert_before(soup.new_tag("a")) def test_insert_multiple_after(self): soup = self.soup("foobar") -- cgit v1.2.1