Skip to content

Commit 497e527

Browse files
committed
Create reparentChildren method, needed for elementtree; elementtree now passes all but two tests
--HG-- extra : convert_revision : svn%3Aacbfec75-9323-0410-a652-858a13e371e0/trunk%40398
1 parent 784c8e3 commit 497e527

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

src/parser.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -906,9 +906,7 @@ def endTagFormatting(self, name):
906906
clone = afeElement.cloneNode()
907907

908908
# Step 10
909-
for furthestBlockChild in furthestBlock.childNodes:
910-
clone.appendChild(furthestBlockChild)
911-
furthestBlock.childNodes = []
909+
self.tree.reparentChildren(furthestBlock, clone)
912910

913911
# Step 11
914912
furthestBlock.appendChild(clone)

src/treebuilders/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ def generateImpliedEndTags(self, exclude=None):
231231
# self.processEndTag(name)
232232
self.generateImpliedEndTags(exclude)
233233

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+
234243
def getDocument(self):
235244
"Return the final tree"
236245
return self.document

src/treebuilders/etree.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,8 @@ def cloneNode(self):
8686

8787
class Comment(Element):
8888
def __init__(self, data):
89-
self._element = ElementTree.Comment(data)
90-
self.name = None
91-
self.parent = None
89+
Element.__init__(self, Comment)
90+
self._element.text = data
9291

9392
def _getData(self):
9493
return self._element.text
@@ -114,7 +113,7 @@ def serializeElement(element, indent=0):
114113
rv.append("|%s<!DOCTYPE %s>"%(' '*indent, element.text))
115114
elif element.tag is Document:
116115
rv.append("#document")
117-
elif element.tag is ElementTree.Comment:
116+
elif element.tag is Comment:
118117
rv.append("|%s<!-- %s -->"%(' '*indent, element.text))
119118
else:
120119
rv.append("|%s<%s>"%(' '*indent, element.tag))
@@ -142,3 +141,11 @@ def testSerializer(self, element):
142141

143142
def getDocument(self):
144143
return self.document._element
144+
145+
def reparentChildren(self, oldParent, newParent):
146+
if newParent.childNodes:
147+
newParent.childNodes[-1]._element.tail += oldParent._element.text
148+
else:
149+
newParent._element.text += oldParent._element.text
150+
oldParent._element.text = ""
151+
base.TreeBuilder.reparentChildren(self, oldParent, newParent)

0 commit comments

Comments
 (0)