21
21
22
22
23
23
class Node (object ):
24
+ """Represents an item in the tree"""
24
25
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 document 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
26
+ """Creates a Node
27
+
28
+ :arg name: The tag name associated with the node
29
+
34
30
"""
31
+ # The tag name assocaited with the node
35
32
self .name = name
33
+ # The parent of the current node (or None for the document node)
36
34
self .parent = None
35
+ # The value of the current node (applies to text nodes and comments)
37
36
self .value = None
37
+ # A dict holding name -> value pairs for attributes of the node
38
38
self .attributes = {}
39
+ # A list of child nodes of the current node. This must include all
40
+ # elements but not necessarily other node types.
39
41
self .childNodes = []
42
+ # A list of miscellaneous flags that can be set on the node.
40
43
self ._flags = []
41
44
42
45
def __str__ (self ):
@@ -53,30 +56,51 @@ def __repr__(self):
53
56
54
57
def appendChild (self , node ):
55
58
"""Insert node as a child of the current node
59
+
60
+ :arg node: the node to insert
61
+
56
62
"""
57
63
raise NotImplementedError
58
64
59
65
def insertText (self , data , insertBefore = None ):
60
66
"""Insert data as text in the current node, positioned before the
61
67
start of node insertBefore or to the end of the node's text.
68
+
69
+ :arg data: the data to insert
70
+
71
+ :arg insertBefore: True if you want to insert the text before the node
72
+ and False if you want to insert it after the node
73
+
62
74
"""
63
75
raise NotImplementedError
64
76
65
77
def insertBefore (self , node , refNode ):
66
78
"""Insert node as a child of the current node, before refNode in the
67
79
list of child nodes. Raises ValueError if refNode is not a child of
68
- the current node"""
80
+ the current node
81
+
82
+ :arg node: the node to insert
83
+
84
+ :arg refNode: the child node to insert the node before
85
+
86
+ """
69
87
raise NotImplementedError
70
88
71
89
def removeChild (self , node ):
72
90
"""Remove node from the children of the current node
91
+
92
+ :arg node: the child node to remove
93
+
73
94
"""
74
95
raise NotImplementedError
75
96
76
97
def reparentChildren (self , newParent ):
77
98
"""Move all the children of the current node to newParent.
78
99
This is needed so that trees that don't store text as nodes move the
79
100
text in the correct way
101
+
102
+ :arg newParent: the node to move all this node's children to
103
+
80
104
"""
81
105
# XXX - should this method be made more general?
82
106
for child in self .childNodes :
@@ -121,10 +145,12 @@ def nodesEqual(self, node1, node2):
121
145
122
146
class TreeBuilder (object ):
123
147
"""Base treebuilder implementation
124
- documentClass - the class to use for the bottommost node of a document
125
- elementClass - the class to use for HTML Elements
126
- commentClass - the class to use for comments
127
- doctypeClass - the class to use for doctypes
148
+
149
+ * documentClass - the class to use for the bottommost node of a document
150
+ * elementClass - the class to use for HTML Elements
151
+ * commentClass - the class to use for comments
152
+ * doctypeClass - the class to use for doctypes
153
+
128
154
"""
129
155
# pylint:disable=not-callable
130
156
@@ -144,6 +170,11 @@ class TreeBuilder(object):
144
170
fragmentClass = None
145
171
146
172
def __init__ (self , namespaceHTMLElements ):
173
+ """Create a TreeBuilder
174
+
175
+ :arg namespaceHTMLElements: whether or not to namespace HTML elements
176
+
177
+ """
147
178
if namespaceHTMLElements :
148
179
self .defaultNamespace = "http://www.w3.org/1999/xhtml"
149
180
else :
@@ -367,17 +398,20 @@ def generateImpliedEndTags(self, exclude=None):
367
398
self .generateImpliedEndTags (exclude )
368
399
369
400
def getDocument (self ):
370
- "Return the final tree"
401
+ """ Return the final tree"" "
371
402
return self .document
372
403
373
404
def getFragment (self ):
374
- "Return the final fragment"
405
+ """ Return the final fragment"" "
375
406
# assert self.innerHTML
376
407
fragment = self .fragmentClass ()
377
408
self .openElements [0 ].reparentChildren (fragment )
378
409
return fragment
379
410
380
411
def testSerializer (self , node ):
381
412
"""Serialize the subtree of node in the format required by unit tests
382
- node - the node from which to start serializing"""
413
+
414
+ :arg node: the node from which to start serializing
415
+
416
+ """
383
417
raise NotImplementedError
0 commit comments