Skip to content

Commit

Permalink
Handle highlight value of none
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed Jun 13, 2024
1 parent b874c82 commit 9892cf6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion mammoth/docx/body_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def run(element):
is_strikethrough = read_boolean_element(properties.find_child("w:strike"))
is_all_caps = read_boolean_element(properties.find_child("w:caps"))
is_small_caps = read_boolean_element(properties.find_child("w:smallCaps"))
highlight = properties.find_child_or_null("w:highlight").attributes.get("w:val")
highlight = read_highlight_value(properties.find_child_or_null("w:highlight").attributes.get("w:val"))

def add_complex_field_hyperlink(children):
hyperlink_kwargs = current_hyperlink_kwargs()
Expand Down Expand Up @@ -139,6 +139,12 @@ def read_boolean_element(element):
def read_underline_element(element):
return element and element.attributes.get("w:val") not in [None, "false", "0", "none"]

def read_highlight_value(value):
if not value or value == "none":
return None
else:
return value

def paragraph(element):
properties = element.find_child_or_null("w:pPr")

Expand Down
5 changes: 5 additions & 0 deletions tests/docx/body_xml_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,11 @@ def test_run_has_highlight_read_from_properties(self):
run = self._read_run_with_properties([highlight_xml])
assert_equal("yellow", run.highlight)

def test_when_highlight_is_none_then_run_has_no_highlight(self):
highlight_xml = xml_element("w:highlight", {"w:val": "none"})
run = self._read_run_with_properties([highlight_xml])
assert_equal(None, run.highlight)

def _read_run_with_properties(self, properties, styles=None):
properties_xml = xml_element("w:rPr", {}, properties)
run_xml = xml_element("w:r", {}, [properties_xml])
Expand Down

0 comments on commit 9892cf6

Please sign in to comment.