Skip to content

Commit 6172268

Browse files
committed
Move cloneNode into individual node classes
1 parent 38b3a04 commit 6172268

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/html5lib/treebuilders/simpletree.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,7 @@ def removeChild(self, node):
6262
node.parent = None
6363

6464
def cloneNode(self):
65-
newNode = type(self)(self.name)
66-
if hasattr(self, 'namespace'):
67-
newNode.namespace = self.namespace
68-
if hasattr(self, 'attributes'):
69-
for attr, value in self.attributes.iteritems():
70-
newNode.attributes[attr] = value
71-
newNode.value = self.value
72-
return newNode
65+
raise NotImplementedError
7366

7467
def hasContent(self):
7568
"""Return true if the node has children or text"""
@@ -112,11 +105,17 @@ def printTree(self):
112105
tree += child.printTree(2)
113106
return tree
114107

108+
def cloneNode(self):
109+
return Document()
110+
115111
class DocumentFragment(Document):
116112
type = 2
117113
def __unicode__(self):
118114
return "#document-fragment"
119115

116+
def cloneNode(self):
117+
return DocumentFragment()
118+
120119
class DocumentType(Node):
121120
type = 3
122121
def __init__(self, name, publicId, systemId):
@@ -140,6 +139,9 @@ def __unicode__(self):
140139
def hilite(self):
141140
return '<code class="markup doctype">&lt;!DOCTYPE %s></code>' % self.name
142141

142+
def cloneNode(self):
143+
return DocumentType(self.name, self.publicId, self.systemId)
144+
143145
class TextNode(Node):
144146
type = 4
145147
def __init__(self, value):
@@ -154,6 +156,9 @@ def toxml(self):
154156

155157
hilite = toxml
156158

159+
def cloneNode(self):
160+
return TextNode(self.value)
161+
157162
class Element(Node):
158163
type = 5
159164
def __init__(self, name, namespace=None):
@@ -206,6 +211,12 @@ def printTree(self, indent):
206211
tree += child.printTree(indent)
207212
return tree
208213

214+
def cloneNode(self):
215+
newNode = Element(self.name)
216+
for attr, value in self.attributes.iteritems():
217+
newNode.attributes[attr] = value
218+
return newNode
219+
209220
class CommentNode(Node):
210221
type = 6
211222
def __init__(self, data):
@@ -221,6 +232,9 @@ def toxml(self):
221232
def hilite(self):
222233
return '<code class="markup comment">&lt;!--%s--></code>' % escape(self.data)
223234

235+
def cloneNode(self):
236+
return CommentNode(self.data)
237+
224238
class TreeBuilder(_base.TreeBuilder):
225239
documentClass = Document
226240
doctypeClass = DocumentType

0 commit comments

Comments
 (0)