Skip to content
Merged
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
8 changes: 4 additions & 4 deletions docling_core/types/doc/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -3486,16 +3486,16 @@ def _move_subtree(self, *, old_subroot: NodeItem, new_subroot: NodeItem, pos: in
if old_subroot.parent is None:
raise ValueError("Can not move the root")

# unlink old subroot from its previous parent
previous_parent: NodeItem = old_subroot.parent.resolve(doc=self)
previous_parent.children.remove(old_subroot.get_ref())

# link new subroot => old subroot
if pos is not None:
new_subroot.children.insert(pos, old_subroot.get_ref())
else:
new_subroot.children.append(old_subroot.get_ref())

# unlink old subroot from its previous parent
previous_parent: NodeItem = old_subroot.parent.resolve(doc=self)
previous_parent.children.remove(old_subroot.get_ref())

# link old subroot => new subroot
old_subroot.parent = new_subroot.get_ref()

Expand Down
8 changes: 8 additions & 0 deletions test/test_docling_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1549,6 +1549,14 @@ def test_misplaced_list_items():
exp_doc = DoclingDocument.load_from_yaml(exp_file)
assert doc == exp_doc

def test_moving_within_same_parent():
doc = DoclingDocument(name="")
doc.add_text(label=DocItemLabel.TEXT, text="bar")
foo = doc.add_text(label=DocItemLabel.TEXT, text="foo")
assert foo.parent is not None
doc._move_subtree(old_subroot=foo, new_subroot=foo.parent.resolve(doc), pos=0)
assert [it.text for it, _ in doc.iterate_items() if isinstance(it, TextItem)] == ["foo", "bar"]


def test_export_with_precision():
doc = DoclingDocument.load_from_yaml(filename="test/data/doc/dummy_doc_2.yaml")
Expand Down
Loading