fix: map highlight w:val="none" to None instead of raising#1560
Open
mithunvoe wants to merge 1 commit into
Open
fix: map highlight w:val="none" to None instead of raising#1560mithunvoe wants to merge 1 commit into
mithunvoe wants to merge 1 commit into
Conversation
`w:val="none"` is a valid `ST_HighlightColor` value that Word writes when a highlight is explicitly cleared from a run. It has no `WD_COLOR_INDEX` member, so reading `Font.highlight_color` on such a run raised `ValueError: WD_COLOR_INDEX has no XML mapping for 'none'` from `BaseXmlEnum.from_xml()`, which aborts parsing of any document containing the value. `none` semantically means "not highlighted", which python-docx already represents as `None` (the value returned when no `w:highlight` element is present), and which the `highlight_val` setter writes by removing the element. Map the explicit `none` value to `None` in the getter to match. `none` is the only `ST_HighlightColor` value absent from `WD_COLOR_INDEX`, so this closes the gap completely.
d312163 to
ce390d3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1559.
Problem
Reading
Font.highlight_coloron a run with<w:highlight w:val="none"/>raisesValueError: WD_COLOR_INDEX has no XML mapping for 'none'.noneis a validST_HighlightColorvalue that Word writes when a highlight is explicitly cleared. Because the error fires during parsing, it aborts processing of any document containing the value.Fix
nonemeans "not highlighted" — the same state python-docx already represents asNone(returned when now:highlightelement is present, and what thehighlight_valsetter writes by removing the element). The getter now maps the explicitnonevalue toNone.noneis the onlyST_HighlightColorvalue absent fromWD_COLOR_INDEX, so this closes the gap without introducing an enum member that would collide on MS-API value0withAUTO.Changes
src/docx/oxml/text/font.py:CT_RPr.highlight_valreturnsNoneforw:val="none".tests/text/test_font.py: added a parametrized case assertingw:val=nonereads asNone.Verification
pytest— full suite (1610 passed), including the new caseruff check/ruff format --check— cleanpyrighton the changed source — 0 errors