def test_text_acquisition_methods(self): # These methods are intended for use against Tag, but they # work on NavigableString as well, s = NavigableString("fee ") cdata = CData("fie ") comment = Comment("foe ") assert "fee " == s.get_text() assert "fee" == s.get_text(strip=True) assert ["fee "] == list(s.strings) assert ["fee"] == list(s.stripped_strings) assert ["fee "] == list(s._all_strings()) assert "fie " == cdata.get_text() assert "fie" == cdata.get_text(strip=True) assert ["fie "] == list(cdata.strings) assert ["fie"] == list(cdata.stripped_strings) assert ["fie "] == list(cdata._all_strings()) # Since a Comment isn't normally considered 'text', # these methods generally do nothing. assert "" == comment.get_text() assert [] == list(comment.strings) assert [] == list(comment.stripped_strings) assert [] == list(comment._all_strings()) # Unless you specifically say that comments are okay. assert "foe" == comment.get_text(strip=True, types=Comment) assert "foe " == comment.get_text(types=(Comment, NavigableString))
def test_cdata(self): # None of the current builders turn CDATA sections into CData # objects, but you can create them manually. soup = self.soup("") cdata = CData("foo") soup.insert(1, cdata) assert str(soup) == "<![CDATA[foo]]>" assert soup.find(string="foo") == "foo" assert soup.contents[0] == "foo"
def test_cdata(self): # None of the current builders turn CDATA sections into CData # objects, but you can create them manually. soup = self.soup("") cdata = CData("foo") soup.insert(1, cdata) self.assertEqual(str(soup), "<![CDATA[foo]]>") 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) assert b"<![CDATA[<><><>]]>" == soup.encode(formatter=increment) assert 1 == self.count
def test_set_string_preserves_class_of_string(self): soup = self.soup("<a></a>") cdata = CData("foo") soup.a.string = cdata assert isinstance(soup.a.string, CData)