Skip to content

Commit 076ad2b

Browse files
authored
fix: fix doc traversal for item deletion (#324)
Signed-off-by: Panos Vagenas <[email protected]>
1 parent d029ad8 commit 076ad2b

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

docling_core/types/doc/document.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,12 +1874,19 @@ def _insert_item_at_stack(
18741874

18751875
return item.get_ref()
18761876

1877-
def _delete_items(self, refs: list[RefItem]) -> bool:
1877+
def _delete_items(self, refs: list[RefItem]):
18781878
"""Delete document item using the self-reference."""
18791879
to_be_deleted_items: dict[tuple[int, ...], str] = {} # stack to cref
18801880

1881+
if not refs:
1882+
return
1883+
18811884
# Identify the to_be_deleted_items
1882-
for item, stack in self._iterate_items_with_stack(with_groups=True):
1885+
for item, stack in self._iterate_items_with_stack(
1886+
with_groups=True,
1887+
traverse_pictures=True,
1888+
included_content_layers={c for c in ContentLayer},
1889+
):
18831890
ref = item.get_ref()
18841891

18851892
if ref in refs:
@@ -1890,8 +1897,10 @@ def _delete_items(self, refs: list[RefItem]) -> bool:
18901897
if tuple(substack) in to_be_deleted_items:
18911898
to_be_deleted_items[tuple(stack)] = ref.cref
18921899

1893-
if len(to_be_deleted_items) == 0:
1894-
raise ValueError("Nothing to be deleted ...")
1900+
if len(to_be_deleted_items) < len(refs):
1901+
raise ValueError(
1902+
f"Cannot find all provided RefItems in doc: {[r.cref for r in refs]}"
1903+
)
18951904

18961905
# Clean the tree, reverse the order to not have to update
18971906
for stack_, ref_ in reversed(sorted(to_be_deleted_items.items())):
@@ -1931,8 +1940,6 @@ def _delete_items(self, refs: list[RefItem]) -> bool:
19311940
node=self.body, refs_to_be_deleted=refs, lookup=lookup
19321941
)
19331942

1934-
return True
1935-
19361943
# Update the references
19371944
def _update_ref_with_lookup(
19381945
self, item_label: str, item_index: int, lookup: dict[str, dict[int, int]]

0 commit comments

Comments
 (0)