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 | fdc2bf6d1490d536fb583986b40a34f80ed5a0bc (patch) | |
tree | ca1a86d493158086e7a852f9e9d9d171f8b1d8a2 /bs4/element.py | |
parent | aab2501c3998174f0ce69d93691a5fe1d4922c02 (diff) |
Improved the exception raised when you call .unwrap() or
.replace_with() on an element that's not attached to a tree.
Diffstat (limited to 'bs4/element.py')
-rw-r--r-- | bs4/element.py | 11 |
1 files changed, 10 insertions, 1 deletions
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 |