Skip to content
Browse files

Merge renderElement-doctype-5560: renderElement handles doctype output.

Author: dreid
Reviewer: ralphm
Fixes: #5560

Add a doctype keyword argument that will be written as the first line of the
response.  Defaulting to the HTML5 doctype '<!DOCTYPE html>'.



git-svn-id: svn://svn.twistedmatrix.com/svn/Twisted/trunk@34016 bbbe8e31-12d6-0310-92fd-ac37d47ddeeb
  • Loading branch information...
1 parent 967c1fe commit 789c881059ff955126eb7edf5390d9f8d38ee48c dreid committed Mar 27, 2012
Showing with 66 additions and 3 deletions.
  1. +10 −1 twisted/web/template.py
  2. +55 −2 twisted/web/test/test_template.py
  3. +1 −0 twisted/web/topfiles/5560.feature
View
11 twisted/web/template.py
@@ -519,17 +519,26 @@ def __getattr__(self, tagName):
-def renderElement(request, element, _failElement=None):
+def renderElement(request, element,
+ doctype='<!DOCTYPE html>', _failElement=None):
"""
Render an element or other C{IRenderable}.
@param request: The C{Request} being rendered to.
@param element: An C{IRenderable} which will be rendered.
+ @param doctype: A C{str} which will be written as the first line of
+ the request, or C{None} to disable writing of a doctype. The C{string}
+ should not include a trailing newline and will default to the HTML5
+ doctype C{'<!DOCTYPE html>'}.
@returns: NOT_DONE_YET
@since: 12.1
"""
+ if doctype is not None:
+ request.write(doctype)
+ request.write('\n')
+
if _failElement is None:
_failElement = twisted.web.util.FailureElement
View
57 twisted/web/test/test_template.py
@@ -694,6 +694,7 @@ def test_simpleRender(self):
def check(_):
self.assertEqual(
"".join(self.request.written),
+ "<!DOCTYPE html>\n"
"<p>Hello, world.</p>")
self.assertTrue(self.request.finished)
@@ -718,7 +719,8 @@ def check(_):
self.assertEqual(len(flushed), 1)
self.assertEqual(
"".join(self.request.written),
- ('<div style="font-size:800%;'
+ ('<!DOCTYPE html>\n'
+ '<div style="font-size:800%;'
'background-color:#FFF;'
'color:#F00'
'">An error occurred while rendering the response.</div>'))
@@ -747,11 +749,62 @@ def check(_):
self.assertEqual(len(flushed), 1)
self.assertEqual(
"".join(self.request.written),
- "<p>I failed.</p>")
+ "<!DOCTYPE html>\n<p>I failed.</p>")
self.assertTrue(self.request.finished)
d.addCallback(check)
renderElement(self.request, element, _failElement=TestFailureElement)
return d
+
+
+ def test_nonDefaultDoctype(self):
+ """
+ L{renderElement} will write the doctype string specified by the
+ doctype keyword argument.
+ """
+
+ element = TestElement()
+
+ d = self.request.notifyFinish()
+
+ def check(_):
+ self.assertEqual(
+ "".join(self.request.written),
+ ('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'
+ ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n'
+ '<p>Hello, world.</p>'))
+
+ d.addCallback(check)
+
+ renderElement(
+ self.request,
+ element,
+ doctype=(
+ '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'
+ ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'))
+
+ return d
+
+
+ def test_noneDoctype(self):
+ """
+ L{renderElement} will not write out a doctype if the doctype keyword
+ argument is C{None}.
+ """
+
+ element = TestElement()
+
+ d = self.request.notifyFinish()
+
+ def check(_):
+ self.assertEqual(
+ "".join(self.request.written),
+ '<p>Hello, world.</p>')
+
+ d.addCallback(check)
+
+ renderElement(self.request, element, doctype=None)
+
+ return d
View
1 twisted/web/topfiles/5560.feature
@@ -0,0 +1 @@
+twisted.web.template.renderElement takes a doctype keyword argument, which will be written as the first line of the response, defaulting to the HTML5 doctype.

0 comments on commit 789c881

Please sign in to comment.
Something went wrong with that request. Please try again.