changeset 160:d19e8a2c549e trunk

Attribute order in parsed XML is now preserved.
author cmlenz
date Wed, 16 Aug 2006 21:54:49 +0000
parents 759c8f5e9efe
children 7b1f07496bf7
files ChangeLog markup/input.py markup/tests/input.py markup/tests/output.py
diffstat 4 files changed, 16 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -15,7 +15,7 @@
  * The XHTML serializer now correctly handles elements in foreign namespaces
    (such as SVG or MathML).
  * Fixed relative includes in templates on Windows (ticket #27).
- * Output can be encoded using legacy codecs such as ISO-8859-1, any character
+ * Output can be encoded using legacy codecs such as ISO-8859-1. Any character
    not representable in the chosen encoding gets replaced by the corresponding
    XML character reference.
  * String literals in XPath expressions that contain spaces are now parsed
--- a/markup/input.py
+++ b/markup/input.py
@@ -68,6 +68,8 @@
         parser = expat.ParserCreate('utf-8', '}')
         parser.buffer_text = True
         parser.returns_unicode = True
+        parser.ordered_attributes = True
+
         parser.StartElementHandler = self._handle_start
         parser.EndElementHandler = self._handle_end
         parser.CharacterDataHandler = self._handle_data
@@ -144,7 +146,7 @@
                 self.expat.CurrentColumnNumber)
 
     def _handle_start(self, tag, attrib):
-        self._enqueue(START, (QName(tag), Attributes(attrib.items())))
+        self._enqueue(START, (QName(tag), Attributes(zip(*[iter(attrib)] * 2))))
 
     def _handle_end(self, tag):
         self._enqueue(END, QName(tag))
--- a/markup/tests/input.py
+++ b/markup/tests/input.py
@@ -41,6 +41,17 @@
         if sys.version_info[:2] >= (2, 4):
             self.assertEqual((None, 1, -1), pos)
 
+    def test_element_attribute_order(self):
+        text = '<elem title="baz" id="foo" class="bar" />'
+        events = list(XMLParser(StringIO(text)))
+        kind, data, pos = events[0]
+        self.assertEqual(Stream.START, kind)
+        tag, attrib = data
+        self.assertEqual(u'elem', tag)
+        self.assertEqual((u'title', u'baz'), attrib[0])
+        self.assertEqual((u'id', u'foo'), attrib[1])
+        self.assertEqual((u'class', u'bar'), attrib[2])
+
 
 class HTMLParserTestCase(unittest.TestCase):
 
--- a/markup/tests/output.py
+++ b/markup/tests/output.py
@@ -113,7 +113,7 @@
           <body>
             <button>
               <svg:svg width="600px" height="400px">
-                <svg:polygon points="50,50 50,300 300,300" />
+                <svg:polygon id="triangle" points="50,50 50,300 300,300" />
               </svg:svg>
             </button>
           </body>
Copyright (C) 2012-2017 Edgewall Software