|
20 | 20 | #XXX - TODO; make the default interface more ElementTree-like
|
21 | 21 | # rather than DOM-like
|
22 | 22 |
|
| 23 | +class Node(object): |
| 24 | + def __init__(self, name): |
| 25 | + """Node representing an item in the tree. |
| 26 | + name - The tag name associated with the node |
| 27 | + parent - The parent of the current node (or None for the root node) |
| 28 | + value - The value of the current node (applies to text nodes and |
| 29 | + comments |
| 30 | + attributes - a dict holding name, value pairs for attributes of the node |
| 31 | + childNodes - a list of child nodes of the current node. This must |
| 32 | + include all elements but not necessarily other node types |
| 33 | + _flags - A list of miscellaneous flags that can be set on the node |
| 34 | + """ |
| 35 | + self.name = name |
| 36 | + self.parent = None |
| 37 | + self.value = None |
| 38 | + self.attributes = {} |
| 39 | + self.childNodes = [] |
| 40 | + self._flags = [] |
| 41 | + |
| 42 | + def __str__(self): |
| 43 | + attributesStr = " ".join(["%s=\"%s\""%(name, value) |
| 44 | + for name, value in |
| 45 | + self.attributes.iteritems()]) |
| 46 | + if attributesStr: |
| 47 | + return "<%s %s>"%(self.name,attributesStr) |
| 48 | + else: |
| 49 | + return "<%s>"%(self.name) |
| 50 | + |
| 51 | + def __repr__(self): |
| 52 | + return "<%s %s>" % (self.__class__, self.name) |
| 53 | + |
| 54 | + def appendChild(self, node): |
| 55 | + """Insert node as a child of the current node""" |
| 56 | + raise NotImplementedError |
| 57 | + |
| 58 | + def insertText(self, data, insertBefore=None): |
| 59 | + """Insert data as text in the current node, positioned before the |
| 60 | + start of node insertBefore or to the end of the node's text. |
| 61 | + """ |
| 62 | + raise NotImplementedError |
| 63 | + |
| 64 | + def insertBefore(self, node, refNode): |
| 65 | + """Insert node as a child of the current node, before refNode in the |
| 66 | + list of child nodes. Raises ValueError if refNode is not a child of |
| 67 | + the current node""" |
| 68 | + raise NotImplementedError |
| 69 | + |
| 70 | + def removeChild(self, node): |
| 71 | + """Remove node from the children of the current node""" |
| 72 | + raise NotImplementedError |
| 73 | + |
| 74 | + def reparentChildren(self, newParent): |
| 75 | + """Move all the children of the current node to newParent. |
| 76 | + This is needed so that trees that don't store text as nodes move the |
| 77 | + text in the correct way""" |
| 78 | + #XXX - should this method be made more general? |
| 79 | + for child in self.childNodes: |
| 80 | + newParent.appendChild(child) |
| 81 | + self.childNodes = [] |
| 82 | + |
| 83 | + def cloneNode(self): |
| 84 | + """Return a shallow copy of the current node i.e. a node with the same |
| 85 | + name and attributes but with no parent or child nodes""" |
| 86 | + raise NotImplementedError |
| 87 | + |
| 88 | + |
| 89 | + def hasContent(self): |
| 90 | + """Return true if the node has children or text, false otherwise |
| 91 | + """ |
| 92 | + raise NotImplementedError |
| 93 | + |
23 | 94 | class TreeBuilder(object):
|
24 |
| - """Base treebuilder implementation""" |
| 95 | + """Base treebuilder implementation |
| 96 | + documentClass - the class to use for the bottommost node of a document |
| 97 | + elementClass - the class to use for HTML Elements |
| 98 | + commentClass - the class to use for comments |
| 99 | + doctypeClass - the class to use for doctypes |
| 100 | + """ |
25 | 101 |
|
26 | 102 | #Document class
|
27 | 103 | documentClass = None
|
@@ -231,15 +307,11 @@ def generateImpliedEndTags(self, exclude=None):
|
231 | 307 | # self.processEndTag(name)
|
232 | 308 | self.generateImpliedEndTags(exclude)
|
233 | 309 |
|
234 |
| - def reparentChildren(self, oldParent, newParent): |
235 |
| - """Move all the children of oldParent to newParent. This is needed do |
236 |
| - that trees that don't store text as nodes move the text in the correct |
237 |
| - way""" |
238 |
| - #XXX - should this method be made more general? |
239 |
| - for child in oldParent.childNodes: |
240 |
| - newParent.appendChild(child) |
241 |
| - oldParent.childNodes = [] |
242 |
| - |
243 | 310 | def getDocument(self):
|
244 | 311 | "Return the final tree"
|
245 | 312 | return self.document
|
| 313 | + |
| 314 | + def testSerializer(self, node): |
| 315 | + """Serialize the subtree of node in the format required by unit tests |
| 316 | + node - the node from which to start serializing""" |
| 317 | + raise NotImplementedError |
0 commit comments