Skip to content

Commit 30b79f5

Browse files
author
Steve Canny
committed
run: add Run.clear()
1 parent 9c86462 commit 30b79f5

4 files changed

Lines changed: 40 additions & 1 deletion

File tree

docx/oxml/text.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ def add_drawing(self, inline_or_anchor):
149149
drawing.append(inline_or_anchor)
150150
return drawing
151151

152+
def clear_content(self):
153+
"""
154+
Remove all child elements except the ``<w:rPr>`` element if present.
155+
"""
156+
content_child_elms = self[1:] if self.rPr is not None else self[:]
157+
for child in content_child_elms:
158+
self.remove(child)
159+
152160
@property
153161
def style(self):
154162
"""

docx/text.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@ def bold(self):
172172
"""
173173
return 'b'
174174

175+
def clear(self):
176+
"""
177+
Return reference to this run after removing all its content. All run
178+
formatting is preserved.
179+
"""
180+
self._r.clear_content()
181+
return self
182+
175183
@boolproperty
176184
def complex_script(self):
177185
"""

features/run-clear-run.feature

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

66

7-
@wip
87
Scenario: Clear run content
98
Given a run having known text and formatting
109
When I clear the run

tests/test_text.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
)
1010

1111
from docx.enum.text import WD_BREAK, WD_UNDERLINE
12+
from docx.oxml import OxmlElement
1213
from docx.oxml.ns import qn
1314
from docx.oxml.text import CT_P, CT_R
1415
from docx.text import Paragraph, Run
@@ -210,6 +211,13 @@ def it_knows_the_text_it_contains(self, text_prop_fixture):
210211
run, expected_text = text_prop_fixture
211212
assert run.text == expected_text
212213

214+
def it_can_remove_its_content_while_preserving_formatting(
215+
self, clear_fixture):
216+
run, expected_xml = clear_fixture
217+
_run = run.clear()
218+
assert run._r.xml == expected_xml
219+
assert _run is run
220+
213221
# fixtures -------------------------------------------------------
214222

215223
@pytest.fixture(params=[
@@ -366,6 +374,22 @@ def bool_prop_set_fixture(self, request):
366374
expected_xml = an_r().with_nsdecls().with_child(rPr_bldr).xml()
367375
return run, bool_prop_name, value, expected_xml
368376

377+
@pytest.fixture(params=[
378+
('bi', 'foobar'), ('bi', None), ('', 'foobar'), ('', None)
379+
])
380+
def clear_fixture(self, request):
381+
formatting, text = request.param
382+
r = OxmlElement('w:r')
383+
if 'b' in formatting:
384+
r.get_or_add_rPr()._add_b().val = True
385+
if 'i' in formatting:
386+
r.get_or_add_rPr()._add_i().val = True
387+
expected_xml = r.xml
388+
if text is not None:
389+
r.add_t(text)
390+
run = Run(r)
391+
return run, expected_xml
392+
369393
@pytest.fixture(params=['Foobar', None])
370394
def style_get_fixture(self, request):
371395
style = request.param

0 commit comments

Comments
 (0)