Commits

Rick Copeland committed 70afe7e

HTML4/5 serialization supported (I think)

  • Participants
  • Parent commits 57815da

Comments (0)

Files changed (2)

File kajiki/tests/test_xml.py

         tpl = XMLTemplate('''<input type="checkbox" checked="$checked"/>''', mode='html')
         rsp = tpl(dict(checked=True)).__kj__.render()
         assert rsp == '<input type="checkbox" CHECKED>', rsp
+        tpl = XMLTemplate('''<!DOCTYPE html>\n<input type="checkbox" checked="$checked"/>''')
+        rsp = tpl(dict(checked=True)).__kj__.render()
+        assert rsp == '<!DOCTYPE html><input type="checkbox" checked="True"/>', rsp
+        tpl = XMLTemplate('''<input type="checkbox" checked="$checked"/>''',
+                          mode='html5')
+        rsp = tpl(dict(checked=True)).__kj__.render()
+        assert rsp == '<!DOCTYPE html><input type="checkbox" CHECKED>', rsp
 
 if __name__ == '__main__':
     main()

File kajiki/xml_template.py

 from .markup_template import QDIRECTIVES, QDIRECTIVES_DICT
 from .html_utils import HTML_OPTIONAL_END_TAGS
 
+impl = dom.getDOMImplementation(' ')
+
 _pattern = r'''
 \$(?:
     (?P<expr_escaped>\$) |      # Escape $$
 
     def compile(self):
         body = list(self._compile_node(self.doc.firstChild))
+        if self.mode == 'xml' and self.doc.doctype:
+            body = [ ir.TextNode(self.doc.doctype.toxml()) ] + body
+        elif self.mode == 'html5':
+            body = [ ir.TextNode('<!DOCTYPE html>')] +body
         self.functions['__call__()'] = body
         defs = [ ir.DefNode(k, *v) for k,v in self.functions.iteritems() ]
         return ir.TemplateNode(self.mod_py, defs)
 
     def startCDATA(self): pass
     def endCDATA(self): pass
-    def startDTD(self, name, pubid, sysid): pass
+    def startDTD(self, name, pubid, sysid):
+        self._doc.doctype = impl.createDocumentType(name, pubid, sysid)
     def endDTD(self): pass
 
 def expand(tree, parent=None):