1 import xml.dom.minidom, io
2 from . import cons as _cons
4 dom = xml.dom.minidom.getDOMImplementation()
6 ns = "http://www.w3.org/1999/xhtml"
7 doctype = "-//W3C//DTD XHTML 1.1//EN"
8 dtd = "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
10 class htmlelement(_cons.element):
12 doc = dom.createDocument(None, None, None)
13 doc.appendChild(dom.createDocumentType("html", doctype, dtd))
14 doc.appendChild(self.__todom__(doc))
17 class xhtmlcontext(_cons.context):
18 attrmap = {"klass": "class"}
20 def addattr(self, node, k, v):
22 super().addattr(node, self.attrmap.get(k, k), v)
25 if ctx is None: ctx = xhtmlcontext()
26 return _cons.constructor(ns, htmlelement, ctx)
28 def head(title=None, css=None):
33 if isinstance(css, str) or isinstance(css, bytes):
34 head(h.link(rel="stylesheet", type="text/css", href=css))
37 head(h.link(rel="stylesheet", type="text/css", href=ss))
40 class htmlformatter(util.formatter):
41 allowshort = {"br", "hr", "img", "input", "meta", "link"}
42 def shorttag(self, el):
43 if el.name in self.allowshort:
49 class htmlindenter(util.textindenter, htmlformatter):
52 def forreq(req, tree):
53 # XXX: Use proper Content-Type for clients accepting it.
54 req.ohead["Content-Type"] = "text/html; charset=utf-8"
56 htmlindenter.output(buf, tree, doctype=(doctype, dtd), charset="utf-8")
58 req.ohead["Content-Length"] = len(ret)
61 def xhtmlresp(callable):
63 return forreq(req, callable(req))
64 wrapper.__wrapped__ = callable