[py-svn] r9924 - in py/dist/py/xmlobj: . testing

hpk at codespeak.net hpk at codespeak.net
Sun Mar 20 16:41:21 CET 2005


Author: hpk
Date: Sun Mar 20 16:41:21 2005
New Revision: 9924

Modified:
   py/dist/py/xmlobj/html.py
   py/dist/py/xmlobj/testing/test_html.py
   py/dist/py/xmlobj/visit.py
Log:
make things a bit more flexible, 
especially allow css "classes" to be
None which means they don't have any. 



Modified: py/dist/py/xmlobj/html.py
==============================================================================
--- py/dist/py/xmlobj/html.py	(original)
+++ py/dist/py/xmlobj/html.py	Sun Mar 20 16:41:21 2005
@@ -2,16 +2,33 @@
 
 
 """ 
-from py.xml import Namespace
+from py.xml import Namespace, Tag
+from py.__impl__.xmlobj.visit import SimpleUnicodeVisitor 
+
+class HtmlVisitor(SimpleUnicodeVisitor): 
+    def repr_attribute(self, attrs, name): 
+        if name == 'class_':
+            value = getattr(attrs, name) 
+            if value is None: 
+                return
+        return super(HtmlVisitor, self).repr_attribute(attrs, name) 
+
+class HtmlTag(Tag): 
+    def unicode(self, indent=2):
+        l = []
+        HtmlVisitor(l.append, indent).visit(self) 
+        return u"".join(l) 
 
 # exported plain html namespace 
 class html(Namespace):
+    __tagclass__ = HtmlTag
     __stickyname__ = True 
     __tagspec__ = dict([(x,1) for x in ( 
         "h1,h2,h3,h5,h6,p,b,i,a,div,span,code,"
         "html,head,title,style,table,tr,tt,"
         "td,th,link,img,meta,body,pre,br,ul,"
-        "ol,li,em,form,input,select,option"
+        "ol,li,em,form,input,select,option,"
+        "button,script" 
     ).split(',') if x])
 
     class Style(object): 

Modified: py/dist/py/xmlobj/testing/test_html.py
==============================================================================
--- py/dist/py/xmlobj/testing/test_html.py	(original)
+++ py/dist/py/xmlobj/testing/test_html.py	Sun Mar 20 16:41:21 2005
@@ -13,6 +13,11 @@
     u = unicode(my.body())
     assert u == '<body style="font-size: 12pt"/>' 
 
+def test_class_None(): 
+    t = html.body(class_=None)
+    u = unicode(t) 
+    assert u == '<body/>'
+
 def test_alternating_style(): 
     alternating = (
         html.Style(background="white"), 

Modified: py/dist/py/xmlobj/visit.py
==============================================================================
--- py/dist/py/xmlobj/visit.py	(original)
+++ py/dist/py/xmlobj/visit.py	Sun Mar 20 16:41:21 2005
@@ -65,14 +65,19 @@
         attrlist.sort() 
         l = []
         for name in attrlist: 
-            if name[0] != '_': 
-                value = getattr(tag.attr, name) 
-                if name.endswith('_'): 
-                    name = name[:-1]
-                l.append(u' %s="%s"' % (name, escape(unicode(value))))
+            res = self.repr_attribute(tag.attr, name)
+            if res is not None: 
+                l.append(res) 
         l.extend(self.getstyle(tag))
         return u"".join(l)
 
+    def repr_attribute(self, attrs, name): 
+        if name[:2] != '__': 
+            value = getattr(attrs, name) 
+            if name.endswith('_'): 
+                name = name[:-1]
+            return u' %s="%s"' % (name, escape(unicode(value)))
+
     def getstyle(self, tag): 
         """ return attribute list suitable for styling. """ 
         try: 



More information about the pytest-commit mailing list