summaryrefslogtreecommitdiff
path: root/bs4
diff options
context:
space:
mode:
Diffstat (limited to 'bs4')
-rw-r--r--bs4/element.py8
-rw-r--r--bs4/tests/test_tree.py24
2 files changed, 31 insertions, 1 deletions
diff --git a/bs4/element.py b/bs4/element.py
index 282193e..c1ad992 100644
--- a/bs4/element.py
+++ b/bs4/element.py
@@ -677,6 +677,12 @@ class CData(NavigableString):
PREFIX = u'<![CDATA['
SUFFIX = u']]>'
+ def output_ready(self, formatter="minimal"):
+ """CData strings are passed into the formatter.
+ But the return value is ignored."""
+ self.format_string(self, formatter)
+ return self.PREFIX + self + self.SUFFIX
+
class ProcessingInstruction(NavigableString):
@@ -791,7 +797,7 @@ class Tag(PageElement):
@string.setter
def string(self, string):
self.clear()
- self.append(unicode(string))
+ self.append(string.__class__(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 5acaeea..2dea886 100644
--- a/bs4/tests/test_tree.py
+++ b/bs4/tests/test_tree.py
@@ -995,6 +995,12 @@ class TestTreeModification(SoupTest):
soup.b.string = soup.c.string
self.assertEqual(soup.a.encode(), b"<a><b>bar</b><c>bar</c></a>")
+ def test_set_string_preserves_class_of_string(self):
+ soup = self.soup("<a></a>")
+ cdata = CData("foo")
+ soup.a.string = cdata
+ self.assertTrue(isinstance(soup.a.string, CData))
+
class TestElementObjects(SoupTest):
"""Test various features of element objects."""
@@ -1346,6 +1352,24 @@ class TestNavigableStringSubclasses(SoupTest):
self.assertEqual(soup.find(text="foo"), "foo")
self.assertEqual(soup.contents[0], "foo")
+ def test_cdata_is_never_formatted(self):
+ """Text inside a CData object is passed into the formatter.
+
+ But the return value is ignored.
+ """
+
+ self.count = 0
+ def increment(*args):
+ self.count += 1
+ return "BITTER FAILURE"
+
+ soup = self.soup("")
+ cdata = CData("<><><>")
+ soup.insert(1, cdata)
+ self.assertEqual(
+ b"<![CDATA[<><><>]]>", soup.encode(formatter=increment))
+ self.assertEqual(1, self.count)
+
def test_doctype_ends_in_newline(self):
# Unlike other NavigableString subclasses, a DOCTYPE always ends
# in a newline.