Skip to content

Commit 0fc52d4

Browse files
Frank BattagliaSteve Canny
authored andcommitted
run: add Run.add_tab()
1 parent f77a632 commit 0fc52d4

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

docx/oxml/text.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class CT_R(BaseOxmlElement):
125125
rPr = ZeroOrOne('w:rPr')
126126
t = ZeroOrMore('w:t')
127127
br = ZeroOrMore('w:br')
128+
tab = ZeroOrMore('w:tab')
128129
drawing = ZeroOrMore('w:drawing')
129130

130131
def _insert_rPr(self, rPr):

docx/text.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ def add_break(self, break_type=WD_BREAK.LINE):
151151
if clear is not None:
152152
br.clear = clear
153153

154+
def add_tab(self):
155+
"""
156+
Add a ``<w:tab/>`` element at the end of the run, which Word
157+
interprets as a tab character.
158+
"""
159+
self._r._add_tab()
160+
154161
def add_text(self, text):
155162
"""
156163
Add a text element to this run.

features/run-add-content.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Feature: Add content to a run
44
I need a way to add each of the run content elements to a run
55

66

7-
@wip
87
Scenario: Add a tab
98
Given a run
109
When I add a tab

tests/oxml/unitdata/text.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ class CT_BrBuilder(BaseBuilder):
1414
__attrs__ = ('w:type', 'w:clear')
1515

1616

17+
class CT_EmptyBuilder(BaseBuilder):
18+
__nspfxs__ = ('w',)
19+
__attrs__ = ()
20+
21+
def __init__(self, tag):
22+
self.__tag__ = tag
23+
super(CT_EmptyBuilder, self).__init__()
24+
25+
1726
class CT_PBuilder(BaseBuilder):
1827
__tag__ = 'w:p'
1928
__nspfxs__ = ('w',)
@@ -110,6 +119,10 @@ def a_strike():
110119
return CT_OnOffBuilder('w:strike')
111120

112121

122+
def a_tab():
123+
return CT_EmptyBuilder('w:tab')
124+
125+
113126
def a_vanish():
114127
return CT_OnOffBuilder('w:vanish')
115128

tests/test_text.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
from .oxml.parts.unitdata.document import a_body
2020
from .oxml.unitdata.text import (
2121
a_b, a_bCs, a_br, a_caps, a_cs, a_dstrike, a_p, a_pPr, a_pStyle,
22-
a_shadow, a_smallCaps, a_snapToGrid, a_specVanish, a_strike, a_t, a_u,
23-
a_vanish, a_webHidden, an_emboss, an_i, an_iCs, an_imprint, an_oMath,
24-
a_noProof, an_outline, an_r, an_rPr, an_rStyle, an_rtl
22+
a_shadow, a_smallCaps, a_snapToGrid, a_specVanish, a_strike, a_t, a_tab,
23+
a_u, a_vanish, a_webHidden, an_emboss, an_i, an_iCs, an_imprint,
24+
an_oMath, a_noProof, an_outline, an_r, an_rPr, an_rStyle, an_rtl
2525
)
2626
from .unitutil import call, class_mock, instance_mock, Mock
2727

@@ -207,6 +207,11 @@ def it_can_add_a_break(self, add_break_fixture):
207207
run.add_break(break_type)
208208
assert run._r.xml == expected_xml
209209

210+
def it_can_add_a_tab(self, add_tab_fixture):
211+
run, expected_xml = add_tab_fixture
212+
run.add_tab()
213+
assert run._r.xml == expected_xml
214+
210215
def it_knows_the_text_it_contains(self, text_prop_fixture):
211216
run, expected_text = text_prop_fixture
212217
assert run.text == expected_text
@@ -241,6 +246,11 @@ def add_break_fixture(self, request, run):
241246
expected_xml = an_r().with_nsdecls().with_child(br_bldr).xml()
242247
return run, break_type, expected_xml
243248

249+
@pytest.fixture
250+
def add_tab_fixture(self, run):
251+
expected_xml = an_r().with_nsdecls().with_child(a_tab()).xml()
252+
return run, expected_xml
253+
244254
@pytest.fixture(params=['foobar', ' foo bar', 'bar foo '])
245255
def add_text_fixture(self, request, run, Text_):
246256
text_str = request.param

0 commit comments

Comments
 (0)