You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

template.py 17KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. # -*- test-case-name: twisted.web.test.test_template -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. HTML rendering for twisted.web.
  6. @var VALID_HTML_TAG_NAMES: A list of recognized HTML tag names, used by the
  7. L{tag} object.
  8. @var TEMPLATE_NAMESPACE: The XML namespace used to identify attributes and
  9. elements used by the templating system, which should be removed from the
  10. final output document.
  11. @var tags: A convenience object which can produce L{Tag} objects on demand via
  12. attribute access. For example: C{tags.div} is equivalent to C{Tag("div")}.
  13. Tags not specified in L{VALID_HTML_TAG_NAMES} will result in an
  14. L{AttributeError}.
  15. """
  16. from __future__ import division, absolute_import
  17. __all__ = [
  18. 'TEMPLATE_NAMESPACE', 'VALID_HTML_TAG_NAMES', 'Element', 'TagLoader',
  19. 'XMLString', 'XMLFile', 'renderer', 'flatten', 'flattenString', 'tags',
  20. 'Comment', 'CDATA', 'Tag', 'slot', 'CharRef', 'renderElement'
  21. ]
  22. import warnings
  23. from collections import OrderedDict
  24. from zope.interface import implementer
  25. from xml.sax import make_parser, handler
  26. from twisted.python.compat import NativeStringIO, items
  27. from twisted.python.filepath import FilePath
  28. from twisted.web._stan import Tag, slot, Comment, CDATA, CharRef
  29. from twisted.web.iweb import ITemplateLoader
  30. from twisted.logger import Logger
  31. TEMPLATE_NAMESPACE = 'http://twistedmatrix.com/ns/twisted.web.template/0.1'
  32. # Go read the definition of NOT_DONE_YET. For lulz. This is totally
  33. # equivalent. And this turns out to be necessary, because trying to import
  34. # NOT_DONE_YET in this module causes a circular import which we cannot escape
  35. # from. From which we cannot escape. Etc. glyph is okay with this solution for
  36. # now, and so am I, as long as this comment stays to explain to future
  37. # maintainers what it means. ~ C.
  38. #
  39. # See http://twistedmatrix.com/trac/ticket/5557 for progress on fixing this.
  40. NOT_DONE_YET = 1
  41. _moduleLog = Logger()
  42. class _NSContext(object):
  43. """
  44. A mapping from XML namespaces onto their prefixes in the document.
  45. """
  46. def __init__(self, parent=None):
  47. """
  48. Pull out the parent's namespaces, if there's no parent then default to
  49. XML.
  50. """
  51. self.parent = parent
  52. if parent is not None:
  53. self.nss = OrderedDict(parent.nss)
  54. else:
  55. self.nss = {'http://www.w3.org/XML/1998/namespace':'xml'}
  56. def get(self, k, d=None):
  57. """
  58. Get a prefix for a namespace.
  59. @param d: The default prefix value.
  60. """
  61. return self.nss.get(k, d)
  62. def __setitem__(self, k, v):
  63. """
  64. Proxy through to setting the prefix for the namespace.
  65. """
  66. self.nss.__setitem__(k, v)
  67. def __getitem__(self, k):
  68. """
  69. Proxy through to getting the prefix for the namespace.
  70. """
  71. return self.nss.__getitem__(k)
  72. class _ToStan(handler.ContentHandler, handler.EntityResolver):
  73. """
  74. A SAX parser which converts an XML document to the Twisted STAN
  75. Document Object Model.
  76. """
  77. def __init__(self, sourceFilename):
  78. """
  79. @param sourceFilename: the filename to load the XML out of.
  80. """
  81. self.sourceFilename = sourceFilename
  82. self.prefixMap = _NSContext()
  83. self.inCDATA = False
  84. def setDocumentLocator(self, locator):
  85. """
  86. Set the document locator, which knows about line and character numbers.
  87. """
  88. self.locator = locator
  89. def startDocument(self):
  90. """
  91. Initialise the document.
  92. """
  93. self.document = []
  94. self.current = self.document
  95. self.stack = []
  96. self.xmlnsAttrs = []
  97. def endDocument(self):
  98. """
  99. Document ended.
  100. """
  101. def processingInstruction(self, target, data):
  102. """
  103. Processing instructions are ignored.
  104. """
  105. def startPrefixMapping(self, prefix, uri):
  106. """
  107. Set up the prefix mapping, which maps fully qualified namespace URIs
  108. onto namespace prefixes.
  109. This gets called before startElementNS whenever an C{xmlns} attribute
  110. is seen.
  111. """
  112. self.prefixMap = _NSContext(self.prefixMap)
  113. self.prefixMap[uri] = prefix
  114. # Ignore the template namespace; we'll replace those during parsing.
  115. if uri == TEMPLATE_NAMESPACE:
  116. return
  117. # Add to a list that will be applied once we have the element.
  118. if prefix is None:
  119. self.xmlnsAttrs.append(('xmlns',uri))
  120. else:
  121. self.xmlnsAttrs.append(('xmlns:%s'%prefix,uri))
  122. def endPrefixMapping(self, prefix):
  123. """
  124. "Pops the stack" on the prefix mapping.
  125. Gets called after endElementNS.
  126. """
  127. self.prefixMap = self.prefixMap.parent
  128. def startElementNS(self, namespaceAndName, qname, attrs):
  129. """
  130. Gets called when we encounter a new xmlns attribute.
  131. @param namespaceAndName: a (namespace, name) tuple, where name
  132. determines which type of action to take, if the namespace matches
  133. L{TEMPLATE_NAMESPACE}.
  134. @param qname: ignored.
  135. @param attrs: attributes on the element being started.
  136. """
  137. filename = self.sourceFilename
  138. lineNumber = self.locator.getLineNumber()
  139. columnNumber = self.locator.getColumnNumber()
  140. ns, name = namespaceAndName
  141. if ns == TEMPLATE_NAMESPACE:
  142. if name == 'transparent':
  143. name = ''
  144. elif name == 'slot':
  145. try:
  146. # Try to get the default value for the slot
  147. default = attrs[(None, 'default')]
  148. except KeyError:
  149. # If there wasn't one, then use None to indicate no
  150. # default.
  151. default = None
  152. el = slot(
  153. attrs[(None, 'name')], default=default,
  154. filename=filename, lineNumber=lineNumber,
  155. columnNumber=columnNumber)
  156. self.stack.append(el)
  157. self.current.append(el)
  158. self.current = el.children
  159. return
  160. render = None
  161. attrs = OrderedDict(attrs)
  162. for k, v in items(attrs):
  163. attrNS, justTheName = k
  164. if attrNS != TEMPLATE_NAMESPACE:
  165. continue
  166. if justTheName == 'render':
  167. render = v
  168. del attrs[k]
  169. # nonTemplateAttrs is a dictionary mapping attributes that are *not* in
  170. # TEMPLATE_NAMESPACE to their values. Those in TEMPLATE_NAMESPACE were
  171. # just removed from 'attrs' in the loop immediately above. The key in
  172. # nonTemplateAttrs is either simply the attribute name (if it was not
  173. # specified as having a namespace in the template) or prefix:name,
  174. # preserving the xml namespace prefix given in the document.
  175. nonTemplateAttrs = OrderedDict()
  176. for (attrNs, attrName), v in items(attrs):
  177. nsPrefix = self.prefixMap.get(attrNs)
  178. if nsPrefix is None:
  179. attrKey = attrName
  180. else:
  181. attrKey = '%s:%s' % (nsPrefix, attrName)
  182. nonTemplateAttrs[attrKey] = v
  183. if ns == TEMPLATE_NAMESPACE and name == 'attr':
  184. if not self.stack:
  185. # TODO: define a better exception for this?
  186. raise AssertionError(
  187. '<{%s}attr> as top-level element' % (TEMPLATE_NAMESPACE,))
  188. if 'name' not in nonTemplateAttrs:
  189. # TODO: same here
  190. raise AssertionError(
  191. '<{%s}attr> requires a name attribute' % (TEMPLATE_NAMESPACE,))
  192. el = Tag('', render=render, filename=filename,
  193. lineNumber=lineNumber, columnNumber=columnNumber)
  194. self.stack[-1].attributes[nonTemplateAttrs['name']] = el
  195. self.stack.append(el)
  196. self.current = el.children
  197. return
  198. # Apply any xmlns attributes
  199. if self.xmlnsAttrs:
  200. nonTemplateAttrs.update(OrderedDict(self.xmlnsAttrs))
  201. self.xmlnsAttrs = []
  202. # Add the prefix that was used in the parsed template for non-template
  203. # namespaces (which will not be consumed anyway).
  204. if ns != TEMPLATE_NAMESPACE and ns is not None:
  205. prefix = self.prefixMap[ns]
  206. if prefix is not None:
  207. name = '%s:%s' % (self.prefixMap[ns],name)
  208. el = Tag(
  209. name, attributes=OrderedDict(nonTemplateAttrs), render=render,
  210. filename=filename, lineNumber=lineNumber,
  211. columnNumber=columnNumber)
  212. self.stack.append(el)
  213. self.current.append(el)
  214. self.current = el.children
  215. def characters(self, ch):
  216. """
  217. Called when we receive some characters. CDATA characters get passed
  218. through as is.
  219. @type ch: C{string}
  220. """
  221. if self.inCDATA:
  222. self.stack[-1].append(ch)
  223. return
  224. self.current.append(ch)
  225. def endElementNS(self, name, qname):
  226. """
  227. A namespace tag is closed. Pop the stack, if there's anything left in
  228. it, otherwise return to the document's namespace.
  229. """
  230. self.stack.pop()
  231. if self.stack:
  232. self.current = self.stack[-1].children
  233. else:
  234. self.current = self.document
  235. def startDTD(self, name, publicId, systemId):
  236. """
  237. DTDs are ignored.
  238. """
  239. def endDTD(self, *args):
  240. """
  241. DTDs are ignored.
  242. """
  243. def startCDATA(self):
  244. """
  245. We're starting to be in a CDATA element, make a note of this.
  246. """
  247. self.inCDATA = True
  248. self.stack.append([])
  249. def endCDATA(self):
  250. """
  251. We're no longer in a CDATA element. Collect up the characters we've
  252. parsed and put them in a new CDATA object.
  253. """
  254. self.inCDATA = False
  255. comment = ''.join(self.stack.pop())
  256. self.current.append(CDATA(comment))
  257. def comment(self, content):
  258. """
  259. Add an XML comment which we've encountered.
  260. """
  261. self.current.append(Comment(content))
  262. def _flatsaxParse(fl):
  263. """
  264. Perform a SAX parse of an XML document with the _ToStan class.
  265. @param fl: The XML document to be parsed.
  266. @type fl: A file object or filename.
  267. @return: a C{list} of Stan objects.
  268. """
  269. parser = make_parser()
  270. parser.setFeature(handler.feature_validation, 0)
  271. parser.setFeature(handler.feature_namespaces, 1)
  272. parser.setFeature(handler.feature_external_ges, 0)
  273. parser.setFeature(handler.feature_external_pes, 0)
  274. s = _ToStan(getattr(fl, "name", None))
  275. parser.setContentHandler(s)
  276. parser.setEntityResolver(s)
  277. parser.setProperty(handler.property_lexical_handler, s)
  278. parser.parse(fl)
  279. return s.document
  280. @implementer(ITemplateLoader)
  281. class TagLoader(object):
  282. """
  283. An L{ITemplateLoader} that loads existing L{IRenderable} providers.
  284. @ivar tag: The object which will be loaded.
  285. @type tag: An L{IRenderable} provider.
  286. """
  287. def __init__(self, tag):
  288. """
  289. @param tag: The object which will be loaded.
  290. @type tag: An L{IRenderable} provider.
  291. """
  292. self.tag = tag
  293. def load(self):
  294. return [self.tag]
  295. @implementer(ITemplateLoader)
  296. class XMLString(object):
  297. """
  298. An L{ITemplateLoader} that loads and parses XML from a string.
  299. @ivar _loadedTemplate: The loaded document.
  300. @type _loadedTemplate: a C{list} of Stan objects.
  301. """
  302. def __init__(self, s):
  303. """
  304. Run the parser on a L{NativeStringIO} copy of the string.
  305. @param s: The string from which to load the XML.
  306. @type s: C{str}, or a UTF-8 encoded L{bytes}.
  307. """
  308. if not isinstance(s, str):
  309. s = s.decode('utf8')
  310. self._loadedTemplate = _flatsaxParse(NativeStringIO(s))
  311. def load(self):
  312. """
  313. Return the document.
  314. @return: the loaded document.
  315. @rtype: a C{list} of Stan objects.
  316. """
  317. return self._loadedTemplate
  318. @implementer(ITemplateLoader)
  319. class XMLFile(object):
  320. """
  321. An L{ITemplateLoader} that loads and parses XML from a file.
  322. @ivar _loadedTemplate: The loaded document, or L{None}, if not loaded.
  323. @type _loadedTemplate: a C{list} of Stan objects, or L{None}.
  324. @ivar _path: The L{FilePath}, file object, or filename that is being
  325. loaded from.
  326. """
  327. def __init__(self, path):
  328. """
  329. Run the parser on a file.
  330. @param path: The file from which to load the XML.
  331. @type path: L{FilePath}
  332. """
  333. if not isinstance(path, FilePath):
  334. warnings.warn(
  335. "Passing filenames or file objects to XMLFile is deprecated "
  336. "since Twisted 12.1. Pass a FilePath instead.",
  337. category=DeprecationWarning, stacklevel=2)
  338. self._loadedTemplate = None
  339. self._path = path
  340. def _loadDoc(self):
  341. """
  342. Read and parse the XML.
  343. @return: the loaded document.
  344. @rtype: a C{list} of Stan objects.
  345. """
  346. if not isinstance(self._path, FilePath):
  347. return _flatsaxParse(self._path)
  348. else:
  349. with self._path.open('r') as f:
  350. return _flatsaxParse(f)
  351. def __repr__(self):
  352. return '<XMLFile of %r>' % (self._path,)
  353. def load(self):
  354. """
  355. Return the document, first loading it if necessary.
  356. @return: the loaded document.
  357. @rtype: a C{list} of Stan objects.
  358. """
  359. if self._loadedTemplate is None:
  360. self._loadedTemplate = self._loadDoc()
  361. return self._loadedTemplate
  362. # Last updated October 2011, using W3Schools as a reference. Link:
  363. # http://www.w3schools.com/html5/html5_reference.asp
  364. # Note that <xmp> is explicitly omitted; its semantics do not work with
  365. # t.w.template and it is officially deprecated.
  366. VALID_HTML_TAG_NAMES = set([
  367. 'a', 'abbr', 'acronym', 'address', 'applet', 'area', 'article', 'aside',
  368. 'audio', 'b', 'base', 'basefont', 'bdi', 'bdo', 'big', 'blockquote',
  369. 'body', 'br', 'button', 'canvas', 'caption', 'center', 'cite', 'code',
  370. 'col', 'colgroup', 'command', 'datalist', 'dd', 'del', 'details', 'dfn',
  371. 'dir', 'div', 'dl', 'dt', 'em', 'embed', 'fieldset', 'figcaption',
  372. 'figure', 'font', 'footer', 'form', 'frame', 'frameset', 'h1', 'h2', 'h3',
  373. 'h4', 'h5', 'h6', 'head', 'header', 'hgroup', 'hr', 'html', 'i', 'iframe',
  374. 'img', 'input', 'ins', 'isindex', 'keygen', 'kbd', 'label', 'legend',
  375. 'li', 'link', 'map', 'mark', 'menu', 'meta', 'meter', 'nav', 'noframes',
  376. 'noscript', 'object', 'ol', 'optgroup', 'option', 'output', 'p', 'param',
  377. 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'script',
  378. 'section', 'select', 'small', 'source', 'span', 'strike', 'strong',
  379. 'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'textarea',
  380. 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'tt', 'u', 'ul', 'var',
  381. 'video', 'wbr',
  382. ])
  383. class _TagFactory(object):
  384. """
  385. A factory for L{Tag} objects; the implementation of the L{tags} object.
  386. This allows for the syntactic convenience of C{from twisted.web.html import
  387. tags; tags.a(href="linked-page.html")}, where 'a' can be basically any HTML
  388. tag.
  389. The class is not exposed publicly because you only ever need one of these,
  390. and we already made it for you.
  391. @see: L{tags}
  392. """
  393. def __getattr__(self, tagName):
  394. if tagName == 'transparent':
  395. return Tag('')
  396. # allow for E.del as E.del_
  397. tagName = tagName.rstrip('_')
  398. if tagName not in VALID_HTML_TAG_NAMES:
  399. raise AttributeError('unknown tag %r' % (tagName,))
  400. return Tag(tagName)
  401. tags = _TagFactory()
  402. def renderElement(request, element,
  403. doctype=b'<!DOCTYPE html>', _failElement=None):
  404. """
  405. Render an element or other C{IRenderable}.
  406. @param request: The C{Request} being rendered to.
  407. @param element: An C{IRenderable} which will be rendered.
  408. @param doctype: A C{bytes} which will be written as the first line of
  409. the request, or L{None} to disable writing of a doctype. The C{string}
  410. should not include a trailing newline and will default to the HTML5
  411. doctype C{'<!DOCTYPE html>'}.
  412. @returns: NOT_DONE_YET
  413. @since: 12.1
  414. """
  415. if doctype is not None:
  416. request.write(doctype)
  417. request.write(b'\n')
  418. if _failElement is None:
  419. _failElement = twisted.web.util.FailureElement
  420. d = flatten(request, element, request.write)
  421. def eb(failure):
  422. _moduleLog.failure(
  423. "An error occurred while rendering the response.",
  424. failure=failure
  425. )
  426. if request.site.displayTracebacks:
  427. return flatten(request, _failElement(failure),
  428. request.write).encode('utf8')
  429. else:
  430. request.write(
  431. (b'<div style="font-size:800%;'
  432. b'background-color:#FFF;'
  433. b'color:#F00'
  434. b'">An error occurred while rendering the response.</div>'))
  435. d.addErrback(eb)
  436. d.addBoth(lambda _: request.finish())
  437. return NOT_DONE_YET
  438. from twisted.web._element import Element, renderer
  439. from twisted.web._flatten import flatten, flattenString
  440. import twisted.web.util