Permalink
Browse files

Merge pull request #107 from zzzeek/master

repair non-namespace-aware attribute mis-interpretation in ElementTreeContentHandler
  • Loading branch information...
2 parents d7ea8fd + b844f9e commit 103cd6cbf9ff1cd0daa5a21b8bd310fcee6e7b90 @scoder scoder committed Mar 31, 2013
Showing with 34 additions and 4 deletions.
  1. +7 −2 src/lxml/sax.py
  2. +27 −2 src/lxml/tests/test_sax.py
View
@@ -26,6 +26,7 @@ def _getNsTag(tag):
else:
return (None, tag)
+
class ElementTreeContentHandler(ContentHandler):
"""Build an lxml ElementTree from SAX events.
"""
@@ -45,7 +46,7 @@ def _get_etree(self):
return ElementTree(self._root)
etree = property(_get_etree, doc=_get_etree.__doc__)
-
+
def setDocumentLocator(self, locator):
pass
@@ -127,6 +128,10 @@ def endElementNS(self, ns_name, qname):
raise SaxError("Unexpected element closed: " + el_tag)
def startElement(self, name, attributes=None):
+ if attributes:
+ attributes = dict(
+ [((None, k), v) for k, v in attributes.items()]
+ )
self.startElementNS((None, name), name, attributes)
def endElement(self, name):
@@ -143,7 +148,7 @@ def characters(self, data):
last_element.text = (last_element.text or '') + data
ignorableWhitespace = characters
-
+
class ElementTreeProducer(object):
"""Produces SAX events for an element and children.
@@ -216,6 +216,31 @@ def test_etree_sax_no_ns(self):
self.assertEqual('b', root[0].tag)
self.assertEqual('c', root[1].tag)
+ def test_etree_sax_no_ns_attributes(self):
+ handler = sax.ElementTreeContentHandler()
+ handler.startDocument()
+ handler.startElement('a', {"attr_a1": "a1"})
+ handler.startElement('b', {"attr_b1": "b1"})
+ handler.endElement('b')
+ handler.endElement('a')
+ handler.endDocument()
+
+ new_tree = handler.etree
+ root = new_tree.getroot()
+ self.assertEqual('a', root.tag)
+ self.assertEqual('b', root[0].tag)
+ self.assertEqual('a1', root.attrib["attr_a1"])
+ self.assertEqual('b1', root[0].attrib["attr_b1"])
+
+ def test_etree_sax_ns_attributes(self):
+ handler = sax.ElementTreeContentHandler()
+ handler.startDocument()
+
+ self.assertRaises(ValueError,
+ handler.startElement,
+ 'a', {"blaA:attr_a1": "a1"}
+ )
+
def test_etree_sax_error(self):
handler = sax.ElementTreeContentHandler()
handler.startDocument()
@@ -233,14 +258,14 @@ def _saxify_unsaxify(self, saxifiable):
handler = sax.ElementTreeContentHandler()
sax.ElementTreeProducer(saxifiable, handler).saxify()
return handler.etree
-
+
def _saxify_serialize(self, tree):
new_tree = self._saxify_unsaxify(tree)
f = BytesIO()
new_tree.write(f)
return f.getvalue().replace(_bytes('\n'), _bytes(''))
-
+
def test_suite():
suite = unittest.TestSuite()
suite.addTests([unittest.makeSuite(ETreeSaxTestCase)])

0 comments on commit 103cd6c

Please sign in to comment.