summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--NEWS.txt3
-rw-r--r--bs4/element.py2
-rw-r--r--bs4/testing.py12
3 files changed, 15 insertions, 2 deletions
diff --git a/NEWS.txt b/NEWS.txt
index 602e7c1..4b4ac23 100644
--- a/NEWS.txt
+++ b/NEWS.txt
@@ -13,6 +13,9 @@
* HTML parsers treat all HTML4 and HTML5 empty element tags (aka void
element tags) correctly. [bug=1656909]
+* Namespace prefix is preserved when an XML tag is copied. Thanks
+ to Vikas for a patch and test. [bug=1685172]
+
= 4.5.3 (20170102) =
* Fixed foster parenting when html5lib is the tree builder. Thanks to
diff --git a/bs4/element.py b/bs4/element.py
index 526db28..9ef75f8 100644
--- a/bs4/element.py
+++ b/bs4/element.py
@@ -870,7 +870,7 @@ class Tag(PageElement):
Its contents are a copy of the old Tag's contents.
"""
clone = type(self)(None, self.builder, self.name, self.namespace,
- self.nsprefix, self.attrs, is_xml=self._is_xml)
+ self.prefix, self.attrs, is_xml=self._is_xml)
for attr in ('can_be_empty_element', 'hidden'):
setattr(clone, attr, getattr(self, attr))
for child in self.contents:
diff --git a/bs4/testing.py b/bs4/testing.py
index 40ccac6..6ba2506 100644
--- a/bs4/testing.py
+++ b/bs4/testing.py
@@ -710,8 +710,18 @@ class XMLTreeBuilderSmokeTest(object):
self.assertEqual(1, len(soup.find_all('ns2:tag', key='value')))
self.assertEqual(3, len(soup.find_all(['ns1:tag', 'ns2:tag'])))
+ def test_copy_tag_preserves_namespace(self):
+ xml = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<w:document xmlns:w="http://example.com/ns0"/>"""
+
+ soup = self.soup(xml)
+ tag = soup.document
+ duplicate = copy.copy(tag)
+
+ # The two tags have the same namespace prefix.
+ self.assertEqual(tag.prefix, duplicate.prefix)
+
-
class HTML5TreeBuilderSmokeTest(HTMLTreeBuilderSmokeTest):
"""Smoke test for a tree builder that supports HTML5."""