Commits

Alain Poirier committed c6e283e

Custom data attributes `data-*` of HTML5 can be created as `data_*`

  • Participants
  • Parent commits 56b25c2

Comments (0)

Files changed (2)

File nagare/namespaces/xhtml_base.py

         return self if err is None else self.renderer.decorate_error(self, err)
 
 
-@peak.rules.when(xml.add_child, (xml._Tag, _HTMLTag))
+@peak.rules.when(xml.add_child, (_HTMLTag, _HTMLTag))
 def add_child(next_method, self, element):
     """Add a tag to a tag
 
     return next_method(self, element.decorate_error())
 
 
+@peak.rules.when(xml.add_attribute, (_HTMLTag, basestring, basestring))
+def add_attribute(next_method, self, name, value):
+    if name.startswith('data_'):
+        name = 'data-' + name[5:]
+
+    next_method(self, name, value)
+
+
 class HeadRenderer(xml.XmlRenderer):
     """The XHTML head Renderer
 

File nagare/namespaces/xml.py

     Attribute name can end with a '_' which is removed
     """
     for (name, value) in d.items():
-        if name.endswith('_'):
-            name = name[:-1]
-
         add_attribute(self, name, value)
 
 # ---------------------------------------------------------------------------
 
 @peak.rules.when(add_attribute, (_Tag, basestring, basestring))
 def add_attribute(self, name, value):
+    if name.endswith('_'):
+        name = name[:-1]
+
     self.set(name, value)
 
 @peak.rules.when(add_attribute, (ET._ProcessingInstruction, basestring, basestring))
 def add_attribute(self, name, value):
+    if name.endswith('_'):
+        name = name[:-1]
+
     self.text += (' %s="%s"' % (name, value))
 
 # ---------------------------------------------------------------------------
         return self._makeelement(tag, self._xml_parser)
 
     def __lshift__(self, current):
-        """Add a tag tag the last tag pushed by a ``with`` statement
+        """Add a tag to the last tag pushed by a ``with`` statement
 
         In:
           - ``current`` -- tag to add
         """
         pi = ET.ProcessingInstruction(target, text)
 
-        for name, value in kw.iteritems():
-            if name.endswith('_'):
-                name = name[:-1]
-
+        for name, value in kw.items():
             add_attribute(pi, name, value)
 
         return pi