Skip to content

Commit eaf4cf6

Browse files
committed
tabs: add 'position in tab_stops' functionality
1 parent e0195d8 commit eaf4cf6

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

docx/oxml/text/parfmt.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,13 @@ class CT_TabStops(BaseOxmlElement):
333333
``<w:tabs>`` element, container for a sorted sequence of tab stops.
334334
"""
335335
tab = OneOrMore('w:tab', successors=())
336+
337+
def __contains__(self, position):
338+
"""
339+
Allows 'in' operator to test for Position duplication rather than
340+
looking at the entire tab_stop object
341+
"""
342+
for tab in self.tab_lst:
343+
if tab.pos == position:
344+
return True
345+
return False

docx/text/tabstops.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ class TabStops(ElementProxy):
2121

2222
__slots__ = ()
2323

24+
def __contains__(self, position):
25+
"""
26+
Enables 'in' operator to test for Position duplication
27+
"""
28+
return position in self._element.tabs
29+
2430
def __getitem__(self, idx):
2531
"""
2632
Enables list-style access by index.

features/tab-access-tabs.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ Feature: Adjust tab stops
2020
And I can access a tab stop by index
2121

2222

23-
@wip
2423
Scenario Outline: Determine if tab stops contain a specific tab
2524
Given a tab_stops having 3 tab stops
2625
Then <position> in tab_stops is <result>

tests/text/test_tabstops.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,23 @@ def it_raises_on_indexed_access_when_empty(self):
143143
with pytest.raises(IndexError):
144144
tab_stops[0]
145145

146+
def it_knows_if_it_contains_a_tab_stop_at_a_position(self, in_fixture):
147+
tab_stops, value, expected_result = in_fixture
148+
assert ((int(value) in tab_stops) == expected_result)
149+
146150
# fixture --------------------------------------------------------
147151

152+
@pytest.fixture(params=[
153+
('w:pPr/w:tabs/w:tab{w:pos=1}', Twips(1), 'True'),
154+
('w:pPr/w:tabs/w:tab{w:pos=1}', Twips(2), 'False'),
155+
])
156+
def in_fixture(self, request):
157+
pPr_cxml, value, expected_result = request.param
158+
pPr = element(pPr_cxml)
159+
tab_stops = TabStops(pPr)
160+
expected_result = (expected_result == 'True')
161+
return tab_stops, value, expected_result
162+
148163
@pytest.fixture(params=[
149164
('w:pPr/w:tabs/w:tab{w:pos=0}', 0),
150165
('w:pPr/w:tabs/(w:tab{w:pos=1},w:tab{w:pos=2},w:tab{w:pos=3})', 1),

0 commit comments

Comments
 (0)