Skip to content

Commit

Permalink
- pass through non-NS aware attribute names as is; a ":" should just …
Browse files Browse the repository at this point in the history
…go to

ValueError
- use boolean test for attributes dict
  • Loading branch information
zzzeek committed Mar 30, 2013
1 parent 3222e75 commit b844f9e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 20 deletions.
9 changes: 2 additions & 7 deletions src/lxml/sax.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ def _getNsTag(tag):
else: else:
return (None, tag) return (None, tag)


def _getAttrNSTag(attr_key):
if ":" in attr_key:
return tuple(attr_key.split(":", 1))
else:
return (None, attr_key)


class ElementTreeContentHandler(ContentHandler): class ElementTreeContentHandler(ContentHandler):
"""Build an lxml ElementTree from SAX events. """Build an lxml ElementTree from SAX events.
Expand Down Expand Up @@ -133,9 +128,9 @@ def endElementNS(self, ns_name, qname):
raise SaxError("Unexpected element closed: " + el_tag) raise SaxError("Unexpected element closed: " + el_tag)


def startElement(self, name, attributes=None): def startElement(self, name, attributes=None):
if attributes is not None: if attributes:
attributes = dict( attributes = dict(
[(_getAttrNSTag(k), v) for k, v in attributes.items()] [((None, k), v) for k, v in attributes.items()]
) )
self.startElementNS((None, name), name, attributes) self.startElementNS((None, name), name, attributes)


Expand Down
17 changes: 4 additions & 13 deletions src/lxml/tests/test_sax.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -236,19 +236,10 @@ def test_etree_sax_ns_attributes(self):
handler = sax.ElementTreeContentHandler() handler = sax.ElementTreeContentHandler()
handler.startDocument() handler.startDocument()


handler.startElement('a', {"blaA:attr_a1": "a1"}) self.assertRaises(ValueError,
handler.startElement('b', {"blaA:attr_b1": "b1"}) handler.startElement,
handler.endElement('b') 'a', {"blaA:attr_a1": "a1"}
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["{blaA}attr_a1"])
self.assertEqual('b1', root[0].attrib["{blaA}attr_b1"])


def test_etree_sax_error(self): def test_etree_sax_error(self):
handler = sax.ElementTreeContentHandler() handler = sax.ElementTreeContentHandler()
Expand Down

0 comments on commit b844f9e

Please sign in to comment.