From 5e07ed3b1d6cd3e8e615a1f9cc707bcbe043f5a0 Mon Sep 17 00:00:00 2001 From: Leonard Richardson Date: Mon, 6 Feb 2012 11:19:56 -0500 Subject: You can't replace a tag with its parent, or insert a tag into itself. replace operations return the thing that was replaced. --- bs4/tests/test_tree.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'bs4/tests/test_tree.py') 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): '

Don\'t leave me .

\n' '

Don\'t leave!here

')) + def test_replace_with_returns_thing_that_was_replaced(self): + text = "" + 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 = "" + 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 = "Foo" 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 = "" + soup = self.soup(text) + self.assertRaises(ValueError, soup.b.replace_with, soup.a) + + def test_insert_tag_into_itself_raises_exception(self): + text = "" + soup = self.soup(text) + self.assertRaises(ValueError, soup.a.insert, 0, soup.a) + def test_replace_final_node(self): soup = self.soup("Argh!") soup.find(text="Argh!").replace_with("Hooray!") -- cgit v1.2.3