From fdc2bf6d1490d536fb583986b40a34f80ed5a0bc Mon Sep 17 00:00:00 2001 From: Leonard Richardson Date: Thu, 25 Jun 2015 20:21:09 -0400 Subject: Improved the exception raised when you call .unwrap() or .replace_with() on an element that's not attached to a tree. --- NEWS.txt | 3 +++ bs4/element.py | 11 ++++++++++- bs4/tests/test_tree.py | 9 +++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/NEWS.txt b/NEWS.txt index 768b96f..6ba5275 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -58,6 +58,9 @@ * Fixed a crash in Unicode, Dammit's encoding detector when the name of the encoding itself contained invalid bytes. [bug=1360913] +* Improved the exception raised when you call .unwrap() or + .replace_with() on an element that's not attached to a tree. + = 4.3.2 (20131002) = * Fixed a bug in which short Unicode input was improperly encoded to diff --git a/bs4/element.py b/bs4/element.py index 3c32c17..2ea68b3 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -215,6 +215,10 @@ class PageElement(object): previousSibling = _alias("previous_sibling") # BS3 def replace_with(self, replace_with): + if not self.parent: + raise ValueError( + "Cannot replace one element with another when the" + "element to be replaced is not part of a tree.") if replace_with is self: return if replace_with is self.parent: @@ -228,6 +232,10 @@ class PageElement(object): def unwrap(self): my_parent = self.parent + if not self.parent: + raise ValueError( + "Cannot replace an element with its contents when that" + "element is not part of a tree.") my_index = self.parent.index(self) self.extract() for child in reversed(self.contents[:]): @@ -667,7 +675,8 @@ class NavigableString(unicode, PageElement): """ if isinstance(value, unicode): return unicode.__new__(cls, value) - return unicode.__new__(cls, value, DEFAULT_OUTPUT_ENCODING) + u = unicode.__new__(cls, value, DEFAULT_OUTPUT_ENCODING) + u.setup() def __copy__(self): return self diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py index bab73c6..0bd4713 100644 --- a/bs4/tests/test_tree.py +++ b/bs4/tests/test_tree.py @@ -9,6 +9,7 @@ same markup, but all Beautiful Soup trees can be traversed with the methods tested here. """ +from pdb import set_trace import copy import pickle import re @@ -780,6 +781,14 @@ class TestTreeModification(SoupTest): new_a = a.unwrap() self.assertEqual(a, new_a) + def test_replace_with_and_unwrap_give_useful_exception_when_tag_has_no_parent(self): + soup = self.soup("FooBar") + a = soup.a + a.extract() + self.assertEqual(None, a.parent) + self.assertRaises(ValueError, a.unwrap) + self.assertRaises(ValueError, a.replace_with, soup.c) + def test_replace_tag_with_itself(self): text = "Foo" soup = self.soup(text) -- cgit v1.2.3