Skip to content

Commit 756d179

Browse files
author
Steve Canny
committed
xmlch: fix discovered bug in OptionalAttribute
Didn’t delete optional attribute on assign None when attribute has namespace prefix. Lookup of attribute needs to be on clark_name, not attr_name.
1 parent da43dac commit 756d179

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

docx/oxml/xmlchemy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ def _setter(self):
188188
"""
189189
def set_attr_value(obj, value):
190190
if value is None or value == self._default:
191-
if self._attr_name in obj.attrib:
192-
del obj.attrib[self._attr_name]
191+
if self._clark_name in obj.attrib:
192+
del obj.attrib[self._clark_name]
193193
return
194194
str_value = self._simple_type.to_xml(value)
195195
obj.set(self._clark_name, str_value)

tests/oxml/test_xmlchemy.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,11 +439,14 @@ def getter_fixture(self):
439439
parent = a_parent().with_nsdecls().with_optAttr('24').element
440440
return parent, 24
441441

442-
@pytest.fixture
443-
def setter_fixture(self):
442+
@pytest.fixture(params=[36, None])
443+
def setter_fixture(self, request):
444+
value = request.param
444445
parent = a_parent().with_nsdecls().with_optAttr('42').element
445-
value = 36
446-
expected_xml = a_parent().with_nsdecls().with_optAttr(value).xml()
446+
if value is None:
447+
expected_xml = a_parent().with_nsdecls().xml()
448+
else:
449+
expected_xml = a_parent().with_nsdecls().with_optAttr(value).xml()
447450
return parent, value, expected_xml
448451

449452

@@ -731,7 +734,7 @@ class CT_Parent(BaseOxmlElement):
731734
oooChild = OneAndOnlyOne('w:oooChild')
732735
zomChild = ZeroOrMore('w:zomChild', successors=('w:zooChild',))
733736
zooChild = ZeroOrOne('w:zooChild', successors=())
734-
optAttr = OptionalAttribute('optAttr', ST_IntegerType)
737+
optAttr = OptionalAttribute('w:optAttr', ST_IntegerType)
735738
reqAttr = RequiredAttribute('reqAttr', ST_IntegerType)
736739

737740

@@ -785,7 +788,7 @@ class CT_Choice2Builder(BaseBuilder):
785788
class CT_ParentBuilder(BaseBuilder):
786789
__tag__ = 'w:parent'
787790
__nspfxs__ = ('w',)
788-
__attrs__ = ('optAttr', 'reqAttr')
791+
__attrs__ = ('w:optAttr', 'reqAttr')
789792

790793

791794
class CT_OomChildBuilder(BaseBuilder):

0 commit comments

Comments
 (0)