Skip to content

Commit 14bbaf2

Browse files
committed
hdr: add _BaseHeaderFooter.is_linked_to_p.. getter
1 parent f75b6c1 commit 14bbaf2

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

docx/section.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,25 @@ def __init__(self, sectPr, document_part):
218218
self._sectPr = sectPr
219219
self._document_part = document_part
220220

221+
@property
222+
def is_linked_to_previous(self):
223+
"""True if this header/footer uses the definition from the preceding section.
224+
225+
False if this header/footer has an explicit definition.
226+
227+
Assigning ``True`` to this property removes the header/footer definition for
228+
this section, causing it to "inherit" the corresponding definition of the prior
229+
section. Assigning ``False`` causes a new, empty definition to be added for this
230+
section, but only if no definition is already present.
231+
"""
232+
# ---absence of a header/footer part indicates "linked" behavior---
233+
return not self._has_definition
234+
235+
@property
236+
def _has_definition(self):
237+
"""True if this header/footer has a related part containing its definition."""
238+
raise NotImplementedError("must be implemented by each subclass")
239+
221240

222241
class _Footer(_BaseHeaderFooter):
223242
"""Page footer."""

tests/test_section.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from docx.enum.section import WD_ORIENT, WD_SECTION
1010
from docx.parts.document import DocumentPart
1111
from docx.parts.hdrftr import HeaderPart
12-
from docx.section import _Footer, _Header, Section, Sections
12+
from docx.section import _BaseHeaderFooter, _Footer, _Header, Section, Sections
1313
from docx.shared import Inches
1414

1515
from .unitutil.cxml import element, xml
@@ -358,6 +358,33 @@ def header_(self, request):
358358
return instance_mock(request, _Header)
359359

360360

361+
class Describe_BaseHeaderFooter(object):
362+
363+
def it_knows_when_its_linked_to_the_previous_header(
364+
self, is_linked_get_fixture, _has_definition_prop_
365+
):
366+
has_definition, expected_value = is_linked_get_fixture
367+
_has_definition_prop_.return_value = has_definition
368+
header = _BaseHeaderFooter(None, None)
369+
370+
is_linked = header.is_linked_to_previous
371+
372+
assert is_linked is expected_value
373+
374+
# fixtures -------------------------------------------------------
375+
376+
@pytest.fixture(params=[(False, True), (True, False)])
377+
def is_linked_get_fixture(self, request):
378+
has_definition, expected_value = request.param
379+
return has_definition, expected_value
380+
381+
# fixture components ---------------------------------------------
382+
383+
@pytest.fixture
384+
def _has_definition_prop_(self, request):
385+
return property_mock(request, _BaseHeaderFooter, "_has_definition")
386+
387+
361388
class Describe_Header(object):
362389

363390
def it_knows_when_its_linked_to_the_previous_header(

0 commit comments

Comments
 (0)