From a515d2ebe7ab8ed5e92164b7b75c4d573cb2716e Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 22 Apr 2017 15:50:00 +0100 Subject: Handle complex fields that do not have a "separate" fldChar. --- NEWS | 4 ++++ mammoth/docx/body_xml.py | 2 ++ tests/docx/body_xml_tests.py | 24 ++++++++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/NEWS b/NEWS index 7cef3b0..4d24c57 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,7 @@ +# 1.3.5 + +* Handle complex fields that do not have a "separate" fldChar. + # 1.3.4 * Add transforms.run. diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py index e8ddb01..b8d94cf 100644 --- a/mammoth/docx/body_xml.py +++ b/mammoth/docx/body_xml.py @@ -141,6 +141,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f def read_fld_char(element): fld_char_type = element.attributes.get("w:fldCharType") if fld_char_type == "begin": + complex_field_stack.append(complex_fields.unknown) del current_instr_text[:] elif fld_char_type == "end": complex_field_stack.pop() @@ -151,6 +152,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f complex_field = complex_fields.unknown else: complex_field = complex_fields.hyperlink(hyperlink_href) + complex_field_stack.pop() complex_field_stack.append(complex_field) return _empty_result diff --git a/tests/docx/body_xml_tests.py b/tests/docx/body_xml_tests.py index 5964acd..d112349 100644 --- a/tests/docx/body_xml_tests.py +++ b/tests/docx/body_xml_tests.py @@ -412,6 +412,30 @@ class ComplexFieldTests(object): self._is_empty_hyperlinked_run, is_empty_run, ))) + + @istest + def field_without_separate_fld_char_is_ignored(self): + element = xml_element("w:p", {}, [ + self._BEGIN_COMPLEX_FIELD, + self._HYPERLINK_INSTRTEXT, + self._SEPARATE_COMPLEX_FIELD, + self._BEGIN_COMPLEX_FIELD, + self._END_COMPLEX_FIELD, + _run_element_with_text("this is a hyperlink"), + self._END_COMPLEX_FIELD, + ]) + paragraph = _read_and_get_document_xml_element(element) + + assert_that(paragraph, is_paragraph(children=is_sequence( + is_empty_run, + self._is_empty_hyperlinked_run, + self._is_empty_hyperlinked_run, + self._is_empty_hyperlinked_run, + self._is_hyperlinked_run(children=is_sequence( + is_text("this is a hyperlink"), + )), + is_empty_run, + ))) @istest -- cgit v1.2.3