Skip to content

Commit 4ad97ed

Browse files
ondrej-111scanny
authored andcommitted
hdr: add _Header.is_linked_to_previous
1 parent e8d43a2 commit 4ad97ed

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
@@ -224,3 +224,22 @@ class _Header(BlockItemContainer):
224224
def __init__(self, sectPr, document_part):
225225
self._sectPr = sectPr
226226
self._document_part = document_part
227+
228+
@property
229+
def is_linked_to_previous(self):
230+
"""True if this header uses the header definition of the preceding section.
231+
232+
False if this header has an explicit definition.
233+
234+
Assigning ``True`` to this property removes any header definition for this
235+
section, causing it to "inherit" the header definition of the prior section.
236+
Assigning ``False`` causes a new, empty header definition to be added for this
237+
section, but only if no existing definition is present.
238+
"""
239+
# ---absence of a header (definition) part indicates "linked" behavior---
240+
return not self._has_header_part
241+
242+
@property
243+
def _has_header_part(self):
244+
"""True if a header is explicitly defined for this section."""
245+
raise NotImplementedError

tests/test_section.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from docx.shared import Inches
1313

1414
from .unitutil.cxml import element, xml
15-
from .unitutil.mock import call, class_mock, instance_mock
15+
from .unitutil.mock import call, class_mock, instance_mock, property_mock
1616

1717

1818
class DescribeSections(object):
@@ -355,3 +355,30 @@ def _Header_(self, request):
355355
@pytest.fixture
356356
def header_(self, request):
357357
return instance_mock(request, _Header)
358+
359+
360+
class Describe_Header(object):
361+
362+
def it_knows_when_its_linked_to_the_previous_header(
363+
self, is_linked_fixture, _has_header_part_prop_
364+
):
365+
has_header_part, expected_value = is_linked_fixture
366+
_has_header_part_prop_.return_value = has_header_part
367+
header = _Header(None, None)
368+
369+
is_linked = header.is_linked_to_previous
370+
371+
assert is_linked is expected_value
372+
373+
# fixtures -------------------------------------------------------
374+
375+
@pytest.fixture(params=[(False, True), (True, False)])
376+
def is_linked_fixture(self, request):
377+
has_header_part, expected_value = request.param
378+
return has_header_part, expected_value
379+
380+
# fixture components ---------------------------------------------
381+
382+
@pytest.fixture
383+
def _has_header_part_prop_(self, request):
384+
return property_mock(request, _Header, "_has_header_part")

0 commit comments

Comments
 (0)