summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--NEWS.txt3
-rw-r--r--bs4/element.py2
-rw-r--r--bs4/tests/test_tree.py9
3 files changed, 11 insertions, 3 deletions
diff --git a/NEWS.txt b/NEWS.txt
index 259a881..7db893f 100644
--- a/NEWS.txt
+++ b/NEWS.txt
@@ -2,6 +2,9 @@
* Fixed a bug that sometimes created disconnected trees.
+* Fixed a bug with the string setter that moved a string around the
+ tree instead of copying it. [bug=983050]
+
* Added the missing renderContents method from Beautiful Soup 3. Also
added an encode_contents() method to go along with decode_contents().
diff --git a/bs4/element.py b/bs4/element.py
index 7935cb1..496f2ad 100644
--- a/bs4/element.py
+++ b/bs4/element.py
@@ -758,7 +758,7 @@ class Tag(PageElement):
@string.setter
def string(self, string):
self.clear()
- self.append(string)
+ self.append(unicode(string))
def _all_strings(self, strip=False):
"""Yield all child strings, possibly stripping them."""
diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py
index 76e6bf7..f4fe451 100644
--- a/bs4/tests/test_tree.py
+++ b/bs4/tests/test_tree.py
@@ -990,6 +990,10 @@ class TestTreeModification(SoupTest):
soup.b.string = "bar"
self.assertEqual(soup.b.contents, ["bar"])
+ def test_string_set_does_not_affect_original_string(self):
+ soup = self.soup("<a><b>foo</b><c>bar</c>")
+ soup.b.string = soup.c.string
+ self.assertEqual(soup.a.encode(), b"<a><b>bar</b><c>bar</c></a>")
class TestElementObjects(SoupTest):
"""Test various features of element objects."""
@@ -1300,7 +1304,7 @@ class TestEncoding(SoupTest):
def test_decode_contents(self):
html = u"<b>\N{SNOWMAN}</b>"
soup = self.soup(html)
- self.assertEquals(u"\N{SNOWMAN}", soup.b.decode_contents())
+ self.assertEqual(u"\N{SNOWMAN}", soup.b.decode_contents())
def test_encode_contents(self):
html = u"<b>\N{SNOWMAN}</b>"
@@ -1312,7 +1316,8 @@ class TestEncoding(SoupTest):
def test_deprecated_renderContents(self):
html = u"<b>\N{SNOWMAN}</b>"
soup = self.soup(html)
- self.assertEquals(u"\N{SNOWMAN}".encode("utf8"), soup.b.renderContents())
+ self.assertEqual(
+ u"\N{SNOWMAN}".encode("utf8"), soup.b.renderContents())
class TestNavigableStringSubclasses(SoupTest):