Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d0bd28e

Browse files
DKWoodsSteve Canny
authored andcommitted
tabs: add TabStops.__len__()
1 parent a723006 commit d0bd28e

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

docx/oxml/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
181181
from .text.paragraph import CT_P
182182
register_element_cls('w:p', CT_P)
183183

184-
from .text.parfmt import CT_Ind, CT_Jc, CT_PPr, CT_Spacing
184+
from .text.parfmt import CT_Ind, CT_Jc, CT_PPr, CT_Spacing, CT_TabStops
185185
register_element_cls('w:ind', CT_Ind)
186186
register_element_cls('w:jc', CT_Jc)
187187
register_element_cls('w:keepLines', CT_OnOff)
@@ -190,6 +190,7 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
190190
register_element_cls('w:pPr', CT_PPr)
191191
register_element_cls('w:pStyle', CT_String)
192192
register_element_cls('w:spacing', CT_Spacing)
193+
register_element_cls('w:tabs', CT_TabStops)
193194
register_element_cls('w:widowControl', CT_OnOff)
194195

195196
from .text.run import CT_Br, CT_R, CT_Text

docx/oxml/text/parfmt.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
from ...shared import Length
99
from ..simpletypes import ST_SignedTwipsMeasure, ST_TwipsMeasure
1010
from ..xmlchemy import (
11-
BaseOxmlElement, OptionalAttribute, RequiredAttribute, ZeroOrOne
11+
BaseOxmlElement, OneOrMore, OptionalAttribute, RequiredAttribute,
12+
ZeroOrOne
1213
)
1314

1415

@@ -50,6 +51,7 @@ class CT_PPr(BaseOxmlElement):
5051
pageBreakBefore = ZeroOrOne('w:pageBreakBefore', successors=_tag_seq[4:])
5152
widowControl = ZeroOrOne('w:widowControl', successors=_tag_seq[6:])
5253
numPr = ZeroOrOne('w:numPr', successors=_tag_seq[7:])
54+
tabs = ZeroOrOne('w:tabs', successors=_tag_seq[11:])
5355
spacing = ZeroOrOne('w:spacing', successors=_tag_seq[22:])
5456
ind = ZeroOrOne('w:ind', successors=_tag_seq[23:])
5557
jc = ZeroOrOne('w:jc', successors=_tag_seq[27:])
@@ -311,3 +313,10 @@ class CT_Spacing(BaseOxmlElement):
311313
before = OptionalAttribute('w:before', ST_TwipsMeasure)
312314
line = OptionalAttribute('w:line', ST_SignedTwipsMeasure)
313315
lineRule = OptionalAttribute('w:lineRule', WD_LINE_SPACING)
316+
317+
318+
class CT_TabStops(BaseOxmlElement):
319+
"""
320+
``<w:tabs>`` element, container for a sorted sequence of tab stops.
321+
"""
322+
tab = OneOrMore('w:tab', successors=())

docx/text/tabstops.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ def __init__(self, element):
2525
super(TabStops, self).__init__(element, None)
2626
self._pPr = element
2727

28+
def __len__(self):
29+
tabs = self._pPr.tabs
30+
if tabs is None:
31+
return 0
32+
return len(tabs.tab_lst)
33+
2834

2935
class TabStop(ElementProxy):
3036
"""

features/tab-access-tabs.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Feature: Access TabStop objects
44
I need methods to create, access, and remove a tab stop
55

66

7-
@wip
87
Scenario Outline: TabStops.__len__
98
Given a tab_stops having <count> tab stops
109
Then len(tab_stops) is <count>

tests/text/test_tabstops.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Test suite for the docx.text.tabstops module, containing the TabStops and
5+
TabStop objects.
6+
"""
7+
8+
from __future__ import (
9+
absolute_import, division, print_function, unicode_literals
10+
)
11+
12+
from docx.text.tabstops import TabStops
13+
14+
import pytest
15+
16+
from ..unitutil.cxml import element
17+
18+
19+
class DescribeTabStops(object):
20+
21+
def it_knows_its_length(self, len_fixture):
22+
tab_stops, expected_value = len_fixture
23+
assert len(tab_stops) == expected_value
24+
25+
# fixture --------------------------------------------------------
26+
27+
@pytest.fixture(params=[
28+
('w:pPr', 0),
29+
('w:pPr/w:tabs/w:tab{w:pos=2880}', 1),
30+
])
31+
def len_fixture(self, request):
32+
tab_stops_cxml, expected_value = request.param
33+
tab_stops = TabStops(element(tab_stops_cxml))
34+
return tab_stops, expected_value

0 commit comments

Comments
 (0)