Skip to content

Commit 3923fe8

Browse files
committed
hdr: add BaseStoryPart.next_id
1 parent 002e37f commit 3923fe8

File tree

5 files changed

+34
-43
lines changed

5 files changed

+34
-43
lines changed

docx/parts/document.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,6 @@ def inline_shapes(self):
8989
"""
9090
return InlineShapes(self._element.body, self)
9191

92-
@property
93-
def next_id(self):
94-
"""Next available positive integer id value in this document.
95-
96-
Calculated by incrementing maximum existing id value. Gaps in the
97-
existing id sequence are not filled. The id attribute value is unique
98-
in the document, without regard to the element type it appears on.
99-
"""
100-
id_str_lst = self._element.xpath('//@id')
101-
used_ids = [int(id_str) for id_str in id_str_lst if id_str.isdigit()]
102-
if not used_ids:
103-
return 1
104-
return max(used_ids) + 1
105-
10692
@lazyproperty
10793
def numbering_part(self):
10894
"""

docx/parts/story.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ def next_id(self):
6666
the existing id sequence are not filled. The id attribute value is unique in the
6767
document, without regard to the element type it appears on.
6868
"""
69-
raise NotImplementedError
69+
id_str_lst = self._element.xpath('//@id')
70+
used_ids = [int(id_str) for id_str in id_str_lst if id_str.isdigit()]
71+
if not used_ids:
72+
return 1
73+
return max(used_ids) + 1
7074

7175
@lazyproperty
7276
def _document_part(self):

features/hdr-header-footer.feature

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ Feature: Header and footer behaviors
4040
Then header.paragraphs[0].style.name == "Normal"
4141

4242

43-
@wip
4443
Scenario: _Header allows image insertion
4544
Given a _Run object from a header as run
4645
When I call run.add_picture()
@@ -83,7 +82,6 @@ Feature: Header and footer behaviors
8382
Then footer.paragraphs[0].style.name == "Normal"
8483

8584

86-
@wip
8785
Scenario: _Footer allows image insertion
8886
Given a _Run object from a footer as run
8987
When I call run.add_picture()

tests/parts/test_document.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from docx.styles.styles import Styles
2121

2222
from ..oxml.parts.unitdata.document import a_body, a_document
23-
from ..oxml.unitdata.text import a_p
2423
from ..unitutil.mock import class_mock, instance_mock, method_mock, property_mock
2524

2625

@@ -132,10 +131,6 @@ def and_it_creates_a_numbering_part_if_not_present(
132131
relate_to_.assert_called_once_with(document_part, numbering_part_, RT.NUMBERING)
133132
assert numbering_part is numbering_part_
134133

135-
def it_knows_the_next_available_xml_id(self, next_id_fixture):
136-
document, expected_id = next_id_fixture
137-
assert document.next_id == expected_id
138-
139134
def it_can_get_a_style_by_id(self, styles_prop_, styles_, style_):
140135
styles_prop_.return_value = styles_
141136
styles_.get_by_id.return_value = style_
@@ -222,27 +217,6 @@ def inline_shapes_fixture(self, request, InlineShapes_):
222217
document = DocumentPart(None, None, document_elm, None)
223218
return document, InlineShapes_, body_elm
224219

225-
@pytest.fixture(params=[
226-
((), 1),
227-
((1,), 2),
228-
((2,), 3),
229-
((1, 2, 3), 4),
230-
((1, 2, 4), 5),
231-
((0, 0), 1),
232-
((0, 0, 1, 3), 4),
233-
(('foo', 1, 2), 3),
234-
((1, 'bar'), 2)
235-
])
236-
def next_id_fixture(self, request):
237-
existing_ids, expected_id = request.param
238-
document_elm = a_document().with_nsdecls().element
239-
for n in existing_ids:
240-
p = a_p().with_nsdecls().element
241-
p.set('id', str(n))
242-
document_elm.append(p)
243-
document = DocumentPart(None, None, document_elm, None)
244-
return document, expected_id
245-
246220
@pytest.fixture
247221
def save_fixture(self, package_):
248222
document_part = DocumentPart(None, None, None, package_)

tests/parts/test_story.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from docx.parts.story import BaseStoryPart
1616
from docx.styles.style import BaseStyle
1717

18+
from ..unitutil.cxml import element
1819
from ..unitutil.file import snippet_text
1920
from ..unitutil.mock import instance_mock, method_mock, property_mock
2021

@@ -75,6 +76,14 @@ def it_can_create_a_new_pic_inline(self, get_or_add_image_, image_, next_id_prop
7576
image_.scaled_dimensions.assert_called_once_with(100, 200)
7677
assert inline.xml == expected_xml
7778

79+
def it_knows_the_next_available_xml_id(self, next_id_fixture):
80+
story_element, expected_value = next_id_fixture
81+
story_part = BaseStoryPart(None, None, story_element, None)
82+
83+
next_id = story_part.next_id
84+
85+
assert next_id == expected_value
86+
7887
def it_knows_the_main_document_part_to_help(self, package_, document_part_):
7988
package_.main_document_part = document_part_
8089
story_part = BaseStoryPart(None, None, None, package_)
@@ -83,6 +92,26 @@ def it_knows_the_main_document_part_to_help(self, package_, document_part_):
8392

8493
assert document_part is document_part_
8594

95+
# fixtures -------------------------------------------------------
96+
97+
@pytest.fixture(
98+
params=[
99+
(("w:document"), 1),
100+
(("w:document/w:p{id=1}"), 2),
101+
(("w:document/w:p{id=2}"), 3),
102+
(("w:hdr/(w:p{id=1},w:p{id=2},w:p{id=3})"), 4),
103+
(("w:hdr/(w:p{id=1},w:p{id=2},w:p{id=4})"), 5),
104+
(("w:hdr/(w:p{id=0},w:p{id=0})"), 1),
105+
(("w:ftr/(w:p{id=0},w:p{id=0},w:p{id=1},w:p{id=3})"), 4),
106+
(("w:ftr/(w:p{id=foo},w:p{id=1},w:p{id=2})"), 3),
107+
(("w:ftr/(w:p{id=1},w:p{id=bar})"), 2),
108+
]
109+
)
110+
def next_id_fixture(self, request):
111+
story_cxml, expected_value = request.param
112+
story_element = element(story_cxml)
113+
return story_element, expected_value
114+
86115
# fixture components ---------------------------------------------
87116

88117
@pytest.fixture

0 commit comments

Comments
 (0)