domhelpers.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. # -*- test-case-name: twisted.web.test.test_domhelpers -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. A library for performing interesting tasks with DOM objects.
  6. """
  7. from io import StringIO
  8. from twisted.web import microdom
  9. from twisted.web.microdom import getElementsByTagName, escape, unescape
  10. # These modules are imported here as a shortcut.
  11. escape
  12. getElementsByTagName
  13. class NodeLookupError(Exception):
  14. pass
  15. def substitute(request, node, subs):
  16. """
  17. Look through the given node's children for strings, and
  18. attempt to do string substitution with the given parameter.
  19. """
  20. for child in node.childNodes:
  21. if hasattr(child, 'nodeValue') and child.nodeValue:
  22. child.replaceData(0, len(child.nodeValue), child.nodeValue % subs)
  23. substitute(request, child, subs)
  24. def _get(node, nodeId, nodeAttrs=('id','class','model','pattern')):
  25. """
  26. (internal) Get a node with the specified C{nodeId} as any of the C{class},
  27. C{id} or C{pattern} attributes.
  28. """
  29. if hasattr(node, 'hasAttributes') and node.hasAttributes():
  30. for nodeAttr in nodeAttrs:
  31. if (str (node.getAttribute(nodeAttr)) == nodeId):
  32. return node
  33. if node.hasChildNodes():
  34. if hasattr(node.childNodes, 'length'):
  35. length = node.childNodes.length
  36. else:
  37. length = len(node.childNodes)
  38. for childNum in range(length):
  39. result = _get(node.childNodes[childNum], nodeId)
  40. if result: return result
  41. def get(node, nodeId):
  42. """
  43. Get a node with the specified C{nodeId} as any of the C{class},
  44. C{id} or C{pattern} attributes. If there is no such node, raise
  45. L{NodeLookupError}.
  46. """
  47. result = _get(node, nodeId)
  48. if result: return result
  49. raise NodeLookupError(nodeId)
  50. def getIfExists(node, nodeId):
  51. """
  52. Get a node with the specified C{nodeId} as any of the C{class},
  53. C{id} or C{pattern} attributes. If there is no such node, return
  54. L{None}.
  55. """
  56. return _get(node, nodeId)
  57. def getAndClear(node, nodeId):
  58. """Get a node with the specified C{nodeId} as any of the C{class},
  59. C{id} or C{pattern} attributes. If there is no such node, raise
  60. L{NodeLookupError}. Remove all child nodes before returning.
  61. """
  62. result = get(node, nodeId)
  63. if result:
  64. clearNode(result)
  65. return result
  66. def clearNode(node):
  67. """
  68. Remove all children from the given node.
  69. """
  70. node.childNodes[:] = []
  71. def locateNodes(nodeList, key, value, noNesting=1):
  72. """
  73. Find subnodes in the given node where the given attribute
  74. has the given value.
  75. """
  76. returnList = []
  77. if not isinstance(nodeList, type([])):
  78. return locateNodes(nodeList.childNodes, key, value, noNesting)
  79. for childNode in nodeList:
  80. if not hasattr(childNode, 'getAttribute'):
  81. continue
  82. if str(childNode.getAttribute(key)) == value:
  83. returnList.append(childNode)
  84. if noNesting:
  85. continue
  86. returnList.extend(locateNodes(childNode, key, value, noNesting))
  87. return returnList
  88. def superSetAttribute(node, key, value):
  89. if not hasattr(node, 'setAttribute'): return
  90. node.setAttribute(key, value)
  91. if node.hasChildNodes():
  92. for child in node.childNodes:
  93. superSetAttribute(child, key, value)
  94. def superPrependAttribute(node, key, value):
  95. if not hasattr(node, 'setAttribute'): return
  96. old = node.getAttribute(key)
  97. if old:
  98. node.setAttribute(key, value+'/'+old)
  99. else:
  100. node.setAttribute(key, value)
  101. if node.hasChildNodes():
  102. for child in node.childNodes:
  103. superPrependAttribute(child, key, value)
  104. def superAppendAttribute(node, key, value):
  105. if not hasattr(node, 'setAttribute'): return
  106. old = node.getAttribute(key)
  107. if old:
  108. node.setAttribute(key, old + '/' + value)
  109. else:
  110. node.setAttribute(key, value)
  111. if node.hasChildNodes():
  112. for child in node.childNodes:
  113. superAppendAttribute(child, key, value)
  114. def gatherTextNodes(iNode, dounescape=0, joinWith=""):
  115. """Visit each child node and collect its text data, if any, into a string.
  116. For example::
  117. >>> doc=microdom.parseString('<a>1<b>2<c>3</c>4</b></a>')
  118. >>> gatherTextNodes(doc.documentElement)
  119. '1234'
  120. With dounescape=1, also convert entities back into normal characters.
  121. @return: the gathered nodes as a single string
  122. @rtype: str
  123. """
  124. gathered=[]
  125. gathered_append=gathered.append
  126. slice=[iNode]
  127. while len(slice)>0:
  128. c=slice.pop(0)
  129. if hasattr(c, 'nodeValue') and c.nodeValue is not None:
  130. if dounescape:
  131. val=unescape(c.nodeValue)
  132. else:
  133. val=c.nodeValue
  134. gathered_append(val)
  135. slice[:0]=c.childNodes
  136. return joinWith.join(gathered)
  137. class RawText(microdom.Text):
  138. """This is an evil and horrible speed hack. Basically, if you have a big
  139. chunk of XML that you want to insert into the DOM, but you don't want to
  140. incur the cost of parsing it, you can construct one of these and insert it
  141. into the DOM. This will most certainly only work with microdom as the API
  142. for converting nodes to xml is different in every DOM implementation.
  143. This could be improved by making this class a Lazy parser, so if you
  144. inserted this into the DOM and then later actually tried to mutate this
  145. node, it would be parsed then.
  146. """
  147. def writexml(self, writer, indent="", addindent="", newl="", strip=0, nsprefixes=None, namespace=None):
  148. writer.write("%s%s%s" % (indent, self.data, newl))
  149. def findNodes(parent, matcher, accum=None):
  150. if accum is None:
  151. accum = []
  152. if not parent.hasChildNodes():
  153. return accum
  154. for child in parent.childNodes:
  155. # print child, child.nodeType, child.nodeName
  156. if matcher(child):
  157. accum.append(child)
  158. findNodes(child, matcher, accum)
  159. return accum
  160. def findNodesShallowOnMatch(parent, matcher, recurseMatcher, accum=None):
  161. if accum is None:
  162. accum = []
  163. if not parent.hasChildNodes():
  164. return accum
  165. for child in parent.childNodes:
  166. # print child, child.nodeType, child.nodeName
  167. if matcher(child):
  168. accum.append(child)
  169. if recurseMatcher(child):
  170. findNodesShallowOnMatch(child, matcher, recurseMatcher, accum)
  171. return accum
  172. def findNodesShallow(parent, matcher, accum=None):
  173. if accum is None:
  174. accum = []
  175. if not parent.hasChildNodes():
  176. return accum
  177. for child in parent.childNodes:
  178. if matcher(child):
  179. accum.append(child)
  180. else:
  181. findNodes(child, matcher, accum)
  182. return accum
  183. def findElementsWithAttributeShallow(parent, attribute):
  184. """
  185. Return an iterable of the elements which are direct children of C{parent}
  186. and which have the C{attribute} attribute.
  187. """
  188. return findNodesShallow(parent,
  189. lambda n: getattr(n, 'tagName', None) is not None and
  190. n.hasAttribute(attribute))
  191. def findElements(parent, matcher):
  192. """
  193. Return an iterable of the elements which are children of C{parent} for
  194. which the predicate C{matcher} returns true.
  195. """
  196. return findNodes(
  197. parent,
  198. lambda n, matcher=matcher: getattr(n, 'tagName', None) is not None and
  199. matcher(n))
  200. def findElementsWithAttribute(parent, attribute, value=None):
  201. if value:
  202. return findElements(
  203. parent,
  204. lambda n, attribute=attribute, value=value:
  205. n.hasAttribute(attribute) and n.getAttribute(attribute) == value)
  206. else:
  207. return findElements(
  208. parent,
  209. lambda n, attribute=attribute: n.hasAttribute(attribute))
  210. def findNodesNamed(parent, name):
  211. return findNodes(parent, lambda n, name=name: n.nodeName == name)
  212. def writeNodeData(node, oldio):
  213. for subnode in node.childNodes:
  214. if hasattr(subnode, 'data'):
  215. oldio.write(u"" + subnode.data)
  216. else:
  217. writeNodeData(subnode, oldio)
  218. def getNodeText(node):
  219. oldio = StringIO()
  220. writeNodeData(node, oldio)
  221. return oldio.getvalue()
  222. def getParents(node):
  223. l = []
  224. while node:
  225. l.append(node)
  226. node = node.parentNode
  227. return l
  228. def namedChildren(parent, nodeName):
  229. """namedChildren(parent, nodeName) -> children (not descendants) of parent
  230. that have tagName == nodeName
  231. """
  232. return [n for n in parent.childNodes if getattr(n, 'tagName', '')==nodeName]