@@ -459,10 +459,8 @@ def parse_table_data(
459459 rich_table_cell = self ._is_rich_table_cell (html_cell )
460460 if rich_table_cell :
461461 # Parse table cell sub-tree for Rich Cells content:
462- table_level = self .level
463- provs_in_cell = self ._walk (html_cell , doc )
464- # After walking sub-tree in cell, restore previously set level
465- self .level = table_level
462+ with self ._use_table_cell_context ():
463+ provs_in_cell = self ._walk (html_cell , doc )
466464
467465 group_name = f"rich_cell_group_{ len (doc .tables )} _{ col_idx } _{ start_row_span + row_idx } "
468466 rich_table_cell , ref_for_rich_cell = (
@@ -829,6 +827,21 @@ def _use_footer(self, tag: Tag, doc: DoclingDocument):
829827 self .level -= 1
830828 self .content_layer = current_layer
831829
830+ @contextmanager
831+ def _use_table_cell_context (self ):
832+ """Preserve the hierarchy level and parents during table cell processing.
833+
834+ While the context manager is active, the hierarchy level and parents can be modified.
835+ When exiting, the original level and parents are restored.
836+ """
837+ original_level = self .level
838+ original_parents = self .parents .copy ()
839+ try :
840+ yield
841+ finally :
842+ self .level = original_level
843+ self .parents = original_parents
844+
832845 def _handle_heading (self , tag : Tag , doc : DoclingDocument ) -> list [RefItem ]:
833846 added_ref = []
834847 tag_name = tag .name .lower ()
0 commit comments