1. Ronny Pfannschmidt
  2. py-patches

Source

py-patches / xmlgen-raw-attr

# HG changeset patch
# Parent c4b10e99d306fb1143d36d3134348396a5ed5a42
xmlgen: support using raw for attribute values, fixes #14

diff --git a/CHANGELOG b/CHANGELOG
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -2,6 +2,7 @@ Changes between 1.4.7 and 1.4.8
 ==================================================
 
 - fix issue 13 - correct handling of the tag name object in xmlgen
+- fix issue 14 - support raw attribute values in xmlgen
 
 Changes between 1.4.6 and 1.4.7
 ==================================================
diff --git a/py/_xmlgen.py b/py/_xmlgen.py
--- a/py/_xmlgen.py
+++ b/py/_xmlgen.py
@@ -186,7 +186,11 @@ class SimpleUnicodeVisitor(object):
             value = getattr(attrs, name)
             if name.endswith('_'):
                 name = name[:-1]
-            return ' %s="%s"' % (name, escape(unicode(value)))
+            if isinstance(value, raw):
+                insert = value.uniobj
+            else:
+                insert = escape(unicode(value))
+            return ' %s="%s"' % (name, insert)
 
     def getstyle(self, tag):
         """ return attribute list suitable for styling. """
diff --git a/testing/root/test_xmlgen.py b/testing/root/test_xmlgen.py
--- a/testing/root/test_xmlgen.py
+++ b/testing/root/test_xmlgen.py
@@ -1,6 +1,6 @@
 
 import py
-from py._xmlgen import unicode, html
+from py._xmlgen import unicode, html, raw
 
 class ns(py.xml.Namespace):
     pass
@@ -47,6 +47,10 @@ def test_tag_with_subclassed_attr_simple
     assert x.attr.hello == 'world'
     assert unicode(x) == '<my hello="world"/>'
 
+def test_tag_with_raw_attr():
+    x = html.object(data=raw('&'))
+    assert unicode(x) == '<object data="&"></object>'
+
 def test_tag_nested():
     x = ns.hello(ns.world())
     unicode(x) # triggers parentifying