Tail handling in addnext

Bug #1976304 reported by Jonathan Hofinger
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
lxml
Fix Released
Medium
scoder

Bug Description

Downstream issue: https://gitlab.com/inkscape/extensions/-/issues/480

If elem has a tail, calling elem.addnext(elem2) inserts elem2 between elem and its tail, so this removes
elem's tail and moves it to elem2.

lxml decided that the tail belongs to the preceding element, so this relationship should stay intact over an addnext call. We're also not the first ones running into this issue, see https://github.com/lxml/lxml/blob/ea954da3c87bd8f6874f6bf4203e2ef5269ea383/TODO.txt#L33 or https://stackoverflow.com/questions/23282241/lxml-difference-between-element-addnext-and-insert-in-handling-tail.

Example code:

from lxml import etree
xml = etree.fromstring("<g><path/>tail</g>")
path = xml[0]
print(f"Tail of the first element: {path.tail}")
path.addnext(etree.Element("path"))
print(f"Tail of the first element after inserting: {path.tail}")
print(f"Full tree: {etree.tostring(xml)}")

Result:

Tail of the first element: tail
Tail of the first element after inserting: None
Full tree: b'<g><path/><path/>tail</g>'

Python : sys.version_info(major=3, minor=7, micro=8, releaselevel='final', serial=0)
lxml.etree : (4, 5, 2, 0)
libxml used : (2, 9, 5)
libxml compiled : (2, 9, 5)
libxslt used : (1, 1, 30)
libxslt compiled : (1, 1, 30)

Revision history for this message
scoder (scoder) wrote :

Right. This is a bug. This case was previously not covered by the test suite. I'll fix it in lxml 5.0. Thanks for the report.

Changed in lxml:
assignee: nobody → scoder (scoder)
importance: Undecided → Medium
milestone: none → 5.0
status: New → Confirmed
Revision history for this message
scoder (scoder) wrote :
Changed in lxml:
status: Confirmed → Fix Committed
scoder (scoder)
Changed in lxml:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.