diff options
-rw-r--r-- | bs4/__init__.py | 1 | ||||
-rw-r--r-- | bs4/element.py | 6 | ||||
-rw-r--r-- | bs4/tests/test_tree.py | 24 | ||||
-rw-r--r-- | setup.py | 5 |
4 files changed, 32 insertions, 4 deletions
diff --git a/bs4/__init__.py b/bs4/__init__.py index b469811..ee68e25 100644 --- a/bs4/__init__.py +++ b/bs4/__init__.py @@ -18,6 +18,7 @@ http://www.crummy.com/software/BeautifulSoup/documentation.html """ __author__ = "Leonard Richardson (leonardr@segfault.org)" +__version__ = "4.0.0b4" __copyright__ = "Copyright (c) 2004-2012 Leonard Richardson" __license__ = "MIT" diff --git a/bs4/element.py b/bs4/element.py index 08a0181..211621e 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -47,6 +47,8 @@ class PageElement(object): def replace_with(self, replace_with): if replace_with is self: return + if replace_with is self.parent: + raise ValueError("Cannot replace a Tag with its parent.") old_parent = self.parent my_index = self.parent.index(self) if (hasattr(replace_with, 'parent') @@ -59,6 +61,7 @@ class PageElement(object): my_index -= 1 self.extract() old_parent.insert(my_index, replace_with) + return self replaceWith = replace_with # BS3 def replace_with_children(self): @@ -67,6 +70,7 @@ class PageElement(object): self.extract() for child in reversed(self.contents[:]): my_parent.insert(my_index, child) + return self replaceWithChildren = replace_with_children # BS3 def extract(self): @@ -105,6 +109,8 @@ class PageElement(object): _lastRecursiveChild = _last_descendant def insert(self, position, new_child): + if new_child == self: + raise ValueError("Cannot insert a tag into itself.") if (isinstance(new_child, basestring) and not isinstance(new_child, NavigableString)): new_child = NavigableString(new_child) diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py index c991a85..82a3bfa 100644 --- a/bs4/tests/test_tree.py +++ b/bs4/tests/test_tree.py @@ -577,6 +577,20 @@ class TestTreeModification(SoupTest): '<p id="1">Don\'t leave me .</p>\n' '<p id="2">Don\'t leave!<b>here</b></p>')) + def test_replace_with_returns_thing_that_was_replaced(self): + text = "<a></a><b><c></c></b>" + soup = self.soup(text) + a = soup.a + new_a = a.replace_with(soup.c) + self.assertEqual(a, new_a) + + def test_replace_with_children_returns_thing_that_was_replaced(self): + text = "<a><b></b><c></c></a>" + soup = self.soup(text) + a = soup.a + new_a = a.replace_with_children() + self.assertEqual(a, new_a) + 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) @@ -584,6 +598,16 @@ class TestTreeModification(SoupTest): soup.c.replace_with(c) self.assertEqual(soup.decode(), self.document_for(text)) + def test_replace_tag_with_its_parent_raises_exception(self): + text = "<a><b></b></a>" + soup = self.soup(text) + self.assertRaises(ValueError, soup.b.replace_with, soup.a) + + def test_insert_tag_into_itself_raises_exception(self): + text = "<a><b></b></a>" + soup = self.soup(text) + self.assertRaises(ValueError, soup.a.insert, 0, soup.a) + def test_replace_final_node(self): soup = self.soup("<b>Argh!</b>") soup.find(text="Argh!").replace_with("Hooray!") @@ -1,7 +1,5 @@ from distutils.core import setup -from bs4.version import __version__ - try: from distutils.command.build_py import build_py_2to3 as build_py except ImportError: @@ -9,14 +7,13 @@ except ImportError: from distutils.command.build_py import build_py setup(name="beautifulsoup4", - version=__version__, + version = "4.0.0b4", author="Leonard Richardson", author_email='leonardr@segfault.org', url="http://www.crummy.com/software/BeautifulSoup/bs4/", download_url = "http://www.crummy.com/software/BeautifulSoup/download/4.x/", long_description="""Beautiful Soup sits atop an HTML or XML parser, providing Pythonic idioms for iterating, searching, and modifying the parse tree.""", license="MIT", - py_modules=['bs4'], packages=['bs4', 'bs4.builder', 'bs4.tests'], cmdclass = {'build_py':build_py}, classifiers=["Development Status :: 4 - Beta", |