Skip to content

fix issues with moving the tail after a node has been removed #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/lxml/ElementInclude.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ def _include(elem, loader=None, _parent_hrefs=None, base_url=None):
elif parent is None:
return text # replaced the root node!
else:
parent.text = (parent.text or "") + text + (e.tail or "")
parent.text = (parent.text or "") + text

parent.remove(e)
else:
raise FatalIncludeError(
Expand Down
2 changes: 1 addition & 1 deletion src/lxml/lxml.etree.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -820,8 +820,8 @@ cdef public class _Element [ type LxmlElementType, object LxmlElement ]:
if c_node.parent is not self._c_node:
raise ValueError, u"Element is not a child of this node."
c_next = element._c_node.next
tree.xmlUnlinkNode(c_node)
_moveTail(c_next, c_node)
tree.xmlUnlinkNode(c_node)
# fix namespace declarations
moveNodeToDocument(self._doc, c_node.doc, c_node)

Expand Down
36 changes: 36 additions & 0 deletions src/lxml/tests/test_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -2698,6 +2698,42 @@ def test_tostring_with_tail(self):
result = tostring(a, with_tail=True)
self.assertEquals(result, _bytes("<a><b/>bTAIL<c/></a>aTAIL"))

def test_remove_tail(self):
tostring = self.etree.tostring
Element = self.etree.Element
SubElement = self.etree.SubElement

a = Element('a')
a.text = "aTEXT"
a.tail = "aTAIL"
b = SubElement(a, 'b')
b.text = "bTEXT"
b.tail = "bTAIL"

a.remove(b)

result = tostring(a, with_tail=True)
self.assertEquals(result, _bytes("<a>aTEXTbTAIL</a>aTAIL"))

def test_remove_nested_tail(self):
tostring = self.etree.tostring
Element = self.etree.Element
SubElement = self.etree.SubElement

a = Element('a')
a.tail = "aTAIL"
b = SubElement(a, 'b')
b.text = "bTEXT"
b.tail = "bTAIL"
c = SubElement(a, 'c')
c.text = "cTEXT"
c.tail = "cTAIL"

a.remove(b)

result = tostring(a, with_tail=True)
self.assertEquals(result, _bytes("<a>bTAIL<c>cTEXT</c>cTAIL</a>aTAIL"))

def test_standalone(self):
tostring = self.etree.tostring
XML = self.etree.XML
Expand Down