diff options
author | Leonard Richardson <leonardr@segfault.org> | 2015-06-25 20:21:09 -0400 |
---|---|---|
committer | Leonard Richardson <leonardr@segfault.org> | 2015-06-25 20:21:09 -0400 |
commit | d3ba5f680040185f79419a5805ed8057f48bae93 (patch) | |
tree | 55b8dc010ab1d6e6888f45236854eb5a98f4a7a4 | |
parent | 52e27b39609df2b801413197d179f78df26cff57 (diff) | |
download | beautifulsoup4-d3ba5f680040185f79419a5805ed8057f48bae93.tar.gz |
Improved the exception raised when you call .unwrap() or
.replace_with() on an element that's not attached to a tree.
-rw-r--r-- | NEWS.txt | 3 | ||||
-rw-r--r-- | bs4/element.py | 11 | ||||
-rw-r--r-- | bs4/tests/test_tree.py | 9 |
3 files changed, 22 insertions, 1 deletions
@@ -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("<a><b>Foo</b></a><c>Bar</c>") + 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 = "<a><b></b><c>Foo<d></d></c></a><a><e></e></a>" soup = self.soup(text) |