summaryrefslogtreecommitdiff
path: root/bs4
diff options
context:
space:
mode:
Diffstat (limited to 'bs4')
-rw-r--r--bs4/element.py5
-rw-r--r--bs4/tests/test_tree.py19
2 files changed, 23 insertions, 1 deletions
diff --git a/bs4/element.py b/bs4/element.py
index d1b7c12..0486da2 100644
--- a/bs4/element.py
+++ b/bs4/element.py
@@ -689,7 +689,10 @@ class NavigableString(unicode, PageElement):
return u
def __copy__(self):
- return self
+ """A copy of a NavigableString has the same contents and class
+ as the original, but it is not connected to the parse tree.
+ """
+ return type(self)(self)
def __getnewargs__(self):
return (unicode(self),)
diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py
index 7edf848..22d4b4f 100644
--- a/bs4/tests/test_tree.py
+++ b/bs4/tests/test_tree.py
@@ -1310,6 +1310,25 @@ class TestPersistence(SoupTest):
loaded = pickle.loads(dumped)
self.assertEqual(loaded.decode(), soup.decode())
+ def test_copy_navigablestring_is_not_attached_to_tree(self):
+ html = u"<b>Foo<a></a></b><b>Bar</b>"
+ soup = self.soup(html)
+ s1 = soup.find(string="Foo")
+ s2 = copy.copy(s1)
+ self.assertEqual(s1, s2)
+ self.assertEqual(None, s2.parent)
+ self.assertEqual(None, s2.next_element)
+ self.assertNotEqual(None, s1.next_sibling)
+ self.assertEqual(None, s2.next_sibling)
+ self.assertEqual(None, s2.previous_element)
+
+ def test_copy_navigablestring_subclass_has_same_type(self):
+ html = u"<b><!--Foo--></b>"
+ soup = self.soup(html)
+ s1 = soup.string
+ s2 = copy.copy(s1)
+ self.assertEqual(s1, s2)
+ self.assertTrue(isinstance(s2, Comment))
class TestSubstitutions(SoupTest):