Skip to content

Commit e67fe26

Browse files
Benjamin Toornstrascanny
authored andcommitted
bmk: add _PartBookmarkFinder._iter_starts()
1 parent a45bf20 commit e67fe26

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

docx/bookmark.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from itertools import chain
88

9+
from docx.oxml.ns import qn
910
from docx.shared import lazyproperty
1011

1112

@@ -78,13 +79,23 @@ def _iter_start_end_pairs(self):
7879
continue
7980
yield (bookmarkStart, bookmarkEnd)
8081

82+
@lazyproperty
83+
def _all_starts_and_ends(self):
84+
"""list of all `w:bookmarkStart` and `w:bookmarkEnd` elements in part.
85+
86+
Elements appear in document order.
87+
"""
88+
raise NotImplementedError
89+
8190
def _iter_starts(self):
8291
"""Generate (idx, bookmarkStart) elements in story.
8392
8493
The *idx* value indicates the location of the bookmarkStart element
8594
among all the bookmarkStart and bookmarkEnd elements in the story.
8695
"""
87-
raise NotImplementedError
96+
for idx, element in enumerate(self._all_starts_and_ends):
97+
if element.tag == qn("w:bookmarkStart"):
98+
yield idx, element
8899

89100
def _matching_end(self, bookmarkStart, idx):
90101
"""Return the `w:bookmarkEnd` element corresponding to *bookmarkStart*.

tests/test_bookmark.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,30 @@ def it_iterates_start_end_pairs_to_help(
164164
(bookmarkStarts[2], bookmarkEnds[2]),
165165
]
166166

167+
def it_iterates_bookmarkStart_elements_to_help(self, _all_starts_and_ends_prop_):
168+
starts_and_ends = (
169+
element("w:bookmarkStart"),
170+
element("w:bookmarkEnd"),
171+
element("w:bookmarkStart"),
172+
element("w:bookmarkEnd"),
173+
element("w:bookmarkStart"),
174+
element("w:bookmarkEnd"),
175+
)
176+
_all_starts_and_ends_prop_.return_value = list(starts_and_ends)
177+
finder = _PartBookmarkFinder(None)
178+
179+
starts = list(finder._iter_starts())
180+
181+
assert starts == [
182+
(0, starts_and_ends[0]), (2, starts_and_ends[2]), (4, starts_and_ends[4])
183+
]
184+
167185
# fixture components ---------------------------------------------
168186

187+
@pytest.fixture
188+
def _all_starts_and_ends_prop_(self, request):
189+
return property_mock(request, _PartBookmarkFinder, '_all_starts_and_ends')
190+
169191
@pytest.fixture
170192
def _init_(self, request):
171193
return initializer_mock(request, _PartBookmarkFinder)

0 commit comments

Comments
 (0)