diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-01-03 00:06:50 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-01-03 00:08:32 +0000 |
| commit | f127d6bbed76d3aab49e950ddabe637f63d1913a (patch) | |
| tree | 274a52ee4467ca04cf1463f9ac924dabe20920e8 | |
| parent | ff7cd85b0e97aae22f4f4d954327fd8ddd6b1abb (diff) | |
Ignore child nodes that aren't elements when reading children
| -rw-r--r-- | NEWS | 4 | ||||
| -rw-r--r-- | mammoth/docx/body_xml.py | 5 | ||||
| -rw-r--r-- | tests/docx/body_xml_tests.py | 8 |
3 files changed, 15 insertions, 2 deletions
@@ -1,3 +1,7 @@ +# 0.3.27 + +* Handle XML where the child nodes of an element contains text nodes. + # 0.3.26 * Always use mc:Fallback when reading mc:AlternateContent elements. diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py index 680dede..f66eeae 100644 --- a/mammoth/docx/body_xml.py +++ b/mammoth/docx/body_xml.py @@ -3,7 +3,7 @@ import contextlib from .. import documents from .. import results from .. import lists -from .xmlparser import node_types +from .xmlparser import node_types, XmlElement def reader(numbering=None, @@ -293,7 +293,8 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f return handler(element) - def _read_xml_elements(elements): + def _read_xml_elements(nodes): + elements = filter(lambda node: isinstance(node, XmlElement), nodes) return _combine_results(map(read, elements)) \ .map(lambda values: lists.collect(values)) diff --git a/tests/docx/body_xml_tests.py b/tests/docx/body_xml_tests.py index d0288bd..ddfa9b2 100644 --- a/tests/docx/body_xml_tests.py +++ b/tests/docx/body_xml_tests.py @@ -516,6 +516,14 @@ class ReadXmlElementTests(object): ]) result = _read_and_get_document_xml_element(element) assert_equal("second", result[0].style_id) + + @istest + def text_nodes_are_ignored_when_reading_children(self): + element = xml_element("w:r", {}, [xml_text("[text]")]) + assert_equal( + documents.run([]), + _read_and_get_document_xml_element(element) + ) def _read_and_get_document_xml_element(*args, **kwargs): |
