@@ -761,7 +761,7 @@ def _visit(node: NodeItem) -> None:
761761 current_parent ,
762762 node_cref ,
763763 )
764- child_item . parent = node_ref
764+ self . _move_item_to_parent ( item = child_item , new_parent = node )
765765
766766 _visit (child_item )
767767
@@ -771,6 +771,24 @@ def _visit(node: NodeItem) -> None:
771771 if root .children :
772772 _visit (root )
773773
774+ def _move_item_to_parent (self , item : NodeItem , new_parent : NodeItem ) -> None :
775+ """Move a node to a new parent while keeping the tree links consistent."""
776+ item_ref = item .get_ref ()
777+ new_parent_ref = new_parent .get_ref ()
778+
779+ if item .parent :
780+ old_parent = item .parent .resolve (self .doc )
781+ old_parent .children = [
782+ child_ref
783+ for child_ref in old_parent .children
784+ if child_ref .cref != item_ref .cref
785+ ]
786+
787+ item .parent = new_parent_ref
788+
789+ if all (child_ref .cref != item_ref .cref for child_ref in new_parent .children ):
790+ new_parent .children .append (item_ref )
791+
774792 def _reset_list_state (self ):
775793 """Reset list processing state for clean conversion."""
776794 self .list_manager .clear ()
@@ -1429,18 +1447,9 @@ def _group_inside_cell(group: GroupItem) -> bool:
14291447 continue
14301448 seen_refs .add (ref .cref )
14311449 rich_cell = True
1432- item_parent = (
1433- parent_group .parent .resolve (self .doc )
1434- if parent_group .parent
1435- else None
1450+ self ._move_item_to_parent (
1451+ item = parent_group , new_parent = table_item
14361452 )
1437- if (
1438- item_parent
1439- and item_parent != table_item
1440- and ref in item_parent .children
1441- ):
1442- item_parent .children .remove (ref )
1443- parent_group .parent = table_item .get_ref ()
14441453 provs_in_cell .append (ref )
14451454 continue
14461455
@@ -1453,19 +1462,9 @@ def _group_inside_cell(group: GroupItem) -> bool:
14531462 continue
14541463 seen_refs .add (ref .cref )
14551464 rich_cell = True
1456- item_parent = (
1457- item .parent .resolve (self .doc )
1458- if item .parent
1459- else None
1465+ self ._move_item_to_parent (
1466+ item = item , new_parent = table_item
14601467 )
1461- # Only remove from parent if parent is not the table itself
1462- if (
1463- item_parent
1464- and item_parent != table_item
1465- and item .get_ref () in item_parent .children
1466- ):
1467- item_parent .children .remove (item .get_ref ())
1468- item .parent = table_item .get_ref ()
14691468 provs_in_cell .append (ref )
14701469 if rich_cell :
14711470 # Get Ref
@@ -1480,9 +1479,10 @@ def _group_inside_cell(group: GroupItem) -> bool:
14801479 parent = table_item ,
14811480 )
14821481 for pr in provs_in_cell :
1483- group_element .children .append (pr )
14841482 pr_item = pr .resolve (self .doc )
1485- pr_item .parent = group_element .get_ref ()
1483+ self ._move_item_to_parent (
1484+ item = pr_item , new_parent = group_element
1485+ )
14861486 ref_for_rich_cell = group_element .get_ref ()
14871487
14881488 cell = RichTableCell (
0 commit comments