Skip to content

Commit

Permalink
footnote: find_containing_document function improvment for platform…
Browse files Browse the repository at this point in the history
… parser and some additional comments
  • Loading branch information
Tibor committed Mar 16, 2024
1 parent 80ac69b commit 910f163
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docx/oxml/text/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ def clear_content(self):
"""
content_child_elms = self[1:] if self.rPr is not None else self[:]
for child in content_child_elms:
# We keep ``w:footnoteReference`` because of the
# platform `replace_special_chars_preprocessor` preprocessor.
if child.tag == qn('w:footnoteReference'):
continue
self.remove(child)
Expand Down
18 changes: 13 additions & 5 deletions docx/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,20 @@ def find_containing_document(element):
"""
from .document import Document
while True:
if not hasattr(element, '_parent'):
raise PythonDocxError(f'{type(element)} has no `_parent` property.')
if isinstance(element._parent, Document):
return element._parent
if hasattr(element, '_document_part'):
return element._document_part.document
elif hasattr(element, '_parent'):
if isinstance(element._parent, Document):
return element._parent
else:
element = element._parent
elif hasattr(element, 'parent'):
if isinstance(element.parent, Document):
return element.parent
else:
element = element.parent
else:
element = element._parent
raise PythonDocxError(f'{type(element)} couldn\'t find root Document.')


def is_valid_url(url):
Expand Down
2 changes: 1 addition & 1 deletion docx/text/paragraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ def image_parts(self):
"""
Return all image parts related to this paragraph.
"""
doc = self.part.document
doc = find_containing_document(self)
drawings = []
parts = []

Expand Down

0 comments on commit 910f163

Please sign in to comment.