diff options
author | Leonard Richardson <leonardr@segfault.org> | 2021-06-01 19:58:18 -0400 |
---|---|---|
committer | Leonard Richardson <leonardr@segfault.org> | 2021-06-01 19:58:18 -0400 |
commit | 70f546b1e689a70e2f103795efce6d261a3dadf7 (patch) | |
tree | e803f70d1e1e5625c8f31e5495201f0be462cb3d /bs4/tests/test_tree.py | |
parent | a00624d7fc2e29b41b286f46844cb75f4d96ff63 (diff) |
The 'replace_with()' method now takes a variable number of arguments,
and can be used to replace a single element with a sequence of elements.
Patch by Bill Chandos.
Diffstat (limited to 'bs4/tests/test_tree.py')
-rw-r--r-- | bs4/tests/test_tree.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py index 875befe..26004ce 100644 --- a/bs4/tests/test_tree.py +++ b/bs4/tests/test_tree.py @@ -1130,6 +1130,37 @@ class TestTreeModification(SoupTest): self.assertEqual(no.next_element, "no") self.assertEqual(no.next_sibling, " business") + def test_replace_with_errors(self): + # Can't replace a tag that's not part of a tree. + a_tag = Tag(name="a") + self.assertRaises(ValueError, a_tag.replace_with, "won't work") + + # Can't replace a tag with its parent. + a_tag = self.soup("<a><b></b></a>").a + self.assertRaises(ValueError, a_tag.b.replace_with, a_tag) + + # Or with a list that includes its parent. + self.assertRaises(ValueError, a_tag.b.replace_with, + "string1", a_tag, "string2") + + def test_replace_with_multiple(self): + data = "<a><b></b><c></c></a>" + soup = self.soup(data) + d_tag = soup.new_tag("d") + d_tag.string = "Text In D Tag" + e_tag = soup.new_tag("e") + f_tag = soup.new_tag("f") + a_string = "Random Text" + soup.c.replace_with(d_tag, e_tag, a_string, f_tag) + self.assertEqual( + "<a><b></b><d>Text In D Tag</d><e></e>Random Text<f></f></a>", + soup.decode() + ) + assert soup.b.next_element == d_tag + assert d_tag.string.next_element==e_tag + assert e_tag.next_element.string == a_string + assert e_tag.next_element.next_element == f_tag + def test_replace_first_child(self): data = "<a><b></b><c></c></a>" soup = self.soup(data) |