]> granicus.if.org Git - python/commitdiff
Issue #17582: xml.etree.ElementTree nows preserves whitespaces in attributes
authorRaymond Hettinger <python@rcn.com>
Mon, 12 Sep 2016 06:18:03 +0000 (23:18 -0700)
committerRaymond Hettinger <python@rcn.com>
Mon, 12 Sep 2016 06:18:03 +0000 (23:18 -0700)
(Patch by Duane Griffin.  Reviewed and approved by Stefan Behnel.)

Lib/test/test_xml_etree.py
Lib/xml/etree/ElementTree.py
Misc/NEWS

index bc1dd1461bea37f4b8b04e62b7d6ec86d572df37..fbfc0b0250d5570d1e809004513300d7bebd6d96 100644 (file)
@@ -405,6 +405,14 @@ class ElementTreeTest(unittest.TestCase):
         self.assertEqual(ET.tostring(elem),
                 b'<test testa="testval" testb="test1" testc="test2">aa</test>')
 
+        elem = ET.Element('test')
+        elem.set('a', '\r')
+        elem.set('b', '\r\n')
+        elem.set('c', '\t\n\r ')
+        elem.set('d', '\n\n')
+        self.assertEqual(ET.tostring(elem),
+                b'<test a="&#10;" b="&#10;" c="&#09;&#10;&#10; " d="&#10;&#10;" />')
+
     def test_makeelement(self):
         # Test makeelement handling.
 
index 6d1b0ab864ccc4c1912cc8e3b242020db58e045f..92821c5706268a27972b1e1abd3ea4baa44b2c69 100644 (file)
@@ -1083,8 +1083,19 @@ def _escape_attrib(text):
             text = text.replace(">", "&gt;")
         if "\"" in text:
             text = text.replace("\"", "&quot;")
+        # The following business with carriage returns is to satisfy
+        # Section 2.11 of the XML specification, stating that 
+        # CR or CR LN should be replaced with just LN
+        # http://www.w3.org/TR/REC-xml/#sec-line-ends
+        if "\r\n" in text:
+            text = text.replace("\r\n", "\n")
+        if "\r" in text:
+            text = text.replace("\r", "\n")
+        #The following four lines are issue 17582
         if "\n" in text:
             text = text.replace("\n", "&#10;")
+        if "\t" in text:
+            text = text.replace("\t", "&#09;")
         return text
     except (TypeError, AttributeError):
         _raise_serialization_error(text)
index 43f8f25ed5173e6f6c1400bf6fb422aed4fec896..1dc5ad898ca66ab0237578b3dac4115e597722b9 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -83,6 +83,9 @@ Library
 
 - Issue #24594: Validates persist parameter when opening MSI database
 
+- Issue #17582: xml.etree.ElementTree nows preserves whitespaces in attributes
+  (Patch by Duane Griffin.  Reviewed and approved by Stefan Behnel.)
+
 - Issue #28047: Fixed calculation of line length used for the base64 CTE
   in the new email policies.