Skip to content

Commit 3f1a01a

Browse files
author
Steve Canny
committed
run: rewrite Run.style getter
... to return a _CharacterStyle object rather than a styleId string.
1 parent a346cd7 commit 3f1a01a

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

docx/text/run.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from __future__ import absolute_import, print_function, unicode_literals
88

9+
from ..enum.style import WD_STYLE_TYPE
910
from ..enum.text import WD_BREAK
1011
from ..shared import ElementProxy, Parented
1112

@@ -133,13 +134,14 @@ def italic(self, value):
133134
@property
134135
def style(self):
135136
"""
136-
Read/write. The string style ID of the character style applied to
137-
this run, or |None| if it has no directly-applied character style.
138-
Setting this property to |None| causes any directly-applied character
139-
style to be removed such that the run inherits character formatting
140-
from its containing paragraph.
137+
Read/write. A |_CharacterStyle| object representing the character
138+
style applied to this run. The default character style for the
139+
document (often `Default Character Font`) is returned if the run has
140+
no directly-applied character style. Setting this property to |None|
141+
removes any directly-applied character style.
141142
"""
142-
return self._r.style
143+
style_id = self._r.style
144+
return self.part.get_style(style_id, WD_STYLE_TYPE.CHARACTER)
143145

144146
@style.setter
145147
def style(self, char_style):

features/par-add-run.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Feature: Add a run with optional text and style
1010
Then the run contains the text I specified
1111

1212

13-
@wip
1413
Scenario: Add a run specifying its style
1514
Given a paragraph
1615
When I add a run specifying the character style Emphasis

features/run-char-style.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Feature: Each run has a read/write style
44
I need the ability to get and set the character style of a run
55

66

7-
@wip
87
Scenario Outline: Get the character style of a run
98
Given a run having <style> style
109
Then run.style is styles['<value>']

tests/text/test_run.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
absolute_import, division, print_function, unicode_literals
99
)
1010

11+
from docx.enum.style import WD_STYLE_TYPE
1112
from docx.enum.text import WD_BREAK, WD_UNDERLINE
12-
from docx.parts.document import InlineShapes
13+
from docx.parts.document import DocumentPart, InlineShapes
1314
from docx.shape import InlineShape
1415
from docx.shared import Pt
1516
from docx.text.paragraph import Paragraph
@@ -18,7 +19,7 @@
1819
import pytest
1920

2021
from ..unitutil.cxml import element, xml
21-
from ..unitutil.mock import class_mock, instance_mock
22+
from ..unitutil.mock import class_mock, instance_mock, property_mock
2223

2324

2425
class DescribeRun(object):
@@ -33,8 +34,12 @@ def it_can_change_its_bool_prop_settings(self, bool_prop_set_fixture):
3334
assert run._r.xml == expected_xml
3435

3536
def it_knows_its_character_style(self, style_get_fixture):
36-
run, expected_style = style_get_fixture
37-
assert run.style == expected_style
37+
run, style_id_, style_ = style_get_fixture
38+
style = run.style
39+
run.part.get_style.assert_called_once_with(
40+
style_id_, WD_STYLE_TYPE.CHARACTER
41+
)
42+
assert style is style_
3843

3944
def it_can_change_its_character_style(self, style_set_fixture):
4045
run, style, expected_xml = style_set_fixture
@@ -220,14 +225,13 @@ def font_fixture(self, Font_, font_):
220225
run = Run(element('w:r'), None)
221226
return run, Font_, font_
222227

223-
@pytest.fixture(params=[
224-
('w:r', None),
225-
('w:r/w:rPr/w:rStyle{w:val=Foobar}', 'Foobar'),
226-
])
227-
def style_get_fixture(self, request):
228-
r_cxml, expected_style = request.param
228+
@pytest.fixture
229+
def style_get_fixture(self, part_prop_):
230+
style_id = 'Barfoo'
231+
r_cxml = 'w:r/w:rPr/w:rStyle{w:val=%s}' % style_id
229232
run = Run(element(r_cxml), None)
230-
return run, expected_style
233+
style_ = part_prop_.return_value.get_style.return_value
234+
return run, style_id, style_
231235

232236
@pytest.fixture(params=[
233237
('w:r', None,
@@ -313,6 +317,10 @@ def underline_raise_fixture(self, request):
313317

314318
# fixture components ---------------------------------------------
315319

320+
@pytest.fixture
321+
def document_part_(self, request):
322+
return instance_mock(request, DocumentPart)
323+
316324
@pytest.fixture
317325
def Font_(self, request, font_):
318326
return class_mock(request, 'docx.text.run.Font', return_value=font_)
@@ -331,6 +339,12 @@ def inline_shapes_(self, request, picture_):
331339
def paragraph_(self, request):
332340
return instance_mock(request, Paragraph)
333341

342+
@pytest.fixture
343+
def part_prop_(self, request, document_part_):
344+
return property_mock(
345+
request, Run, 'part', return_value=document_part_
346+
)
347+
334348
@pytest.fixture
335349
def picture_(self, request):
336350
return instance_mock(request, InlineShape)

0 commit comments

Comments
 (0)