diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-07-03 12:24:07 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-07-03 12:24:07 +0100 |
| commit | f2db2bcf40c2c497a33b1f087b67111c3351a406 (patch) | |
| tree | 192f541a76a8fe91cf0ec5a002ccae38a4879ced | |
| parent | 36e3ce588ad6f55d28a52ff9e10b6bfdb52e0a7b (diff) | |
Read optional attributes of comments
| -rw-r--r-- | mammoth/docx/comments_xml.py | 7 | ||||
| -rw-r--r-- | tests/docx/comments_xml_tests.py | 20 |
2 files changed, 25 insertions, 2 deletions
diff --git a/mammoth/docx/comments_xml.py b/mammoth/docx/comments_xml.py index fbf1bc4..6d35c12 100644 --- a/mammoth/docx/comments_xml.py +++ b/mammoth/docx/comments_xml.py @@ -10,12 +10,15 @@ def create_comments_reader(body_reader): def _read_comment_element(element): + def read_optional_attribute(name): + return element.attributes.get(name, "").strip() or None + return body_reader.read_all(element.children).map(lambda body: documents.comment( comment_id=element.attributes["w:id"], body=body, - author_name=None, - author_initials=None, + author_name=read_optional_attribute("w:author"), + author_initials=read_optional_attribute("w:initials"), )) return read_comments_xml_element diff --git a/tests/docx/comments_xml_tests.py b/tests/docx/comments_xml_tests.py index 5025a21..4963fe0 100644 --- a/tests/docx/comments_xml_tests.py +++ b/tests/docx/comments_xml_tests.py @@ -25,3 +25,23 @@ def when_optional_attributes_of_comment_are_missing_then_they_are_as_none(): comment, = comments.value assert_equal(None, comment.author_name) assert_equal(None, comment.author_initials) + + +@istest +def when_optional_attributes_of_comment_are_blank_then_they_are_as_none(): + comments = create_comments_reader(body_xml.reader())(xml_element("w:comments", {}, [ + xml_element("w:comment", {"w:id": "1", "w:author": " ", "w:initials": " "}, []), + ])) + comment, = comments.value + assert_equal(None, comment.author_name) + assert_equal(None, comment.author_initials) + + +@istest +def when_optional_attributes_of_comment_are_not_blank_then_they_are_read(): + comments = create_comments_reader(body_xml.reader())(xml_element("w:comments", {}, [ + xml_element("w:comment", {"w:id": "1", "w:author": "The Piemaker", "w:initials": "TP"}, []), + ])) + comment, = comments.value + assert_equal("The Piemaker", comment.author_name) + assert_equal("TP", comment.author_initials) |
