Skip to content

Commit e6c64a0

Browse files
committed
hdr: add _Footer._drop_definition()
1 parent 3f62ffc commit e6c64a0

File tree

4 files changed

+52
-24
lines changed

4 files changed

+52
-24
lines changed

docx/oxml/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
9292
CT_SectPr,
9393
CT_SectType,
9494
)
95+
register_element_cls("w:footerReference", CT_HdrFtrRef)
9596
register_element_cls("w:hdr", CT_HdrFtr)
9697
register_element_cls("w:headerReference", CT_HdrFtrRef)
9798
register_element_cls("w:pgMar", CT_PageMar)

docx/oxml/section.py

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -192,30 +192,6 @@ def left_margin(self, value):
192192
pgMar = self.get_or_add_pgMar()
193193
pgMar.left = value
194194

195-
def remove_headerReference(self, type_):
196-
"""Return rId of w:headerReference child of *type_* after removing it."""
197-
headerReference = self.get_headerReference(type_)
198-
rId = headerReference.rId
199-
self.remove(headerReference)
200-
return rId
201-
202-
@property
203-
def right_margin(self):
204-
"""
205-
The value of the ``w:right`` attribute in the ``<w:pgMar>`` child
206-
element, as a |Length| object, or |None| if either the element or the
207-
attribute is not present.
208-
"""
209-
pgMar = self.pgMar
210-
if pgMar is None:
211-
return None
212-
return pgMar.right
213-
214-
@right_margin.setter
215-
def right_margin(self, value):
216-
pgMar = self.get_or_add_pgMar()
217-
pgMar.right = value
218-
219195
@property
220196
def orientation(self):
221197
"""
@@ -272,6 +248,37 @@ def preceding_sectPr(self):
272248
preceding_sectPrs = self.xpath("./preceding::w:sectPr[1]")
273249
return preceding_sectPrs[0] if len(preceding_sectPrs) > 0 else None
274250

251+
def remove_footerReference(self, type_):
252+
"""Return rId of w:footerReference child of *type_* after removing it."""
253+
footerReference = self.get_footerReference(type_)
254+
rId = footerReference.rId
255+
self.remove(footerReference)
256+
return rId
257+
258+
def remove_headerReference(self, type_):
259+
"""Return rId of w:headerReference child of *type_* after removing it."""
260+
headerReference = self.get_headerReference(type_)
261+
rId = headerReference.rId
262+
self.remove(headerReference)
263+
return rId
264+
265+
@property
266+
def right_margin(self):
267+
"""
268+
The value of the ``w:right`` attribute in the ``<w:pgMar>`` child
269+
element, as a |Length| object, or |None| if either the element or the
270+
attribute is not present.
271+
"""
272+
pgMar = self.pgMar
273+
if pgMar is None:
274+
return None
275+
return pgMar.right
276+
277+
@right_margin.setter
278+
def right_margin(self, value):
279+
pgMar = self.get_or_add_pgMar()
280+
pgMar.right = value
281+
275282
@property
276283
def start_type(self):
277284
"""

docx/section.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,11 @@ def _has_definition(self):
260260
class _Footer(_BaseHeaderFooter):
261261
"""Page footer."""
262262

263+
def _drop_definition(self):
264+
"""Remove footer definition (footer part) associated with this section."""
265+
rId = self._sectPr.remove_footerReference(WD_HEADER_FOOTER.PRIMARY)
266+
self._document_part.drop_rel(rId)
267+
263268
@property
264269
def _has_definition(self):
265270
"""True if a footer is defined for this section."""

tests/test_section.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,15 @@ def _has_definition_prop_(self, request):
423423

424424
class Describe_Footer(object):
425425

426+
def it_can_drop_the_related_footer_part_to_help(self, document_part_):
427+
sectPr = element("w:sectPr{r:a=b}/w:footerReference{w:type=default,r:id=rId42}")
428+
footer = _Footer(sectPr, document_part_)
429+
430+
footer._drop_definition()
431+
432+
assert sectPr.xml == xml("w:sectPr{r:a=b}")
433+
document_part_.drop_rel.assert_called_once_with("rId42")
434+
426435
def it_knows_when_it_has_a_definition_to_help(self, has_definition_fixture):
427436
sectPr, expected_value = has_definition_fixture
428437
footer = _Footer(sectPr, None)
@@ -443,6 +452,12 @@ def has_definition_fixture(self, request):
443452
sectPr = element(sectPr_cxml)
444453
return sectPr, expected_value
445454

455+
# fixture components ---------------------------------------------
456+
457+
@pytest.fixture
458+
def document_part_(self, request):
459+
return instance_mock(request, DocumentPart)
460+
446461

447462
class Describe_Header(object):
448463

0 commit comments

Comments
 (0)