X Tutup
Skip to content

Commit b0e3c80

Browse files
Victor VolleSteve Canny
authored andcommitted
run: add Run.style getter
1 parent 520d90f commit b0e3c80

File tree

6 files changed

+65
-13
lines changed

6 files changed

+65
-13
lines changed

docx/oxml/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
register_custom_element_class('w:pStyle', CT_String)
7373
register_custom_element_class('w:r', CT_R)
7474
register_custom_element_class('w:rPr', CT_RPr)
75+
register_custom_element_class('w:rStyle', CT_String)
7576
register_custom_element_class('w:rtl', CT_OnOff)
7677
register_custom_element_class('w:shadow', CT_OnOff)
7778
register_custom_element_class('w:smallCaps', CT_OnOff)

docx/oxml/text.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,17 @@ def rPr(self):
267267
"""
268268
return self.find(qn('w:rPr'))
269269

270+
@property
271+
def style(self):
272+
"""
273+
String contained in w:val attribute of <w:pStyle> grandchild, or
274+
|None| if that element is not present.
275+
"""
276+
rPr = self.rPr
277+
if rPr is None:
278+
return None
279+
return rPr.style
280+
270281
@property
271282
def t_lst(self):
272283
"""
@@ -638,6 +649,13 @@ def remove_webHidden(self):
638649
for webHidden in webHidden_lst:
639650
self.remove(webHidden)
640651

652+
@property
653+
def rStyle(self):
654+
"""
655+
``<w:rStyle>`` child element or None if not present.
656+
"""
657+
return self.find(qn('w:rStyle'))
658+
641659
@property
642660
def rtl(self):
643661
"""
@@ -680,6 +698,17 @@ def strike(self):
680698
"""
681699
return self.find(qn('w:strike'))
682700

701+
@property
702+
def style(self):
703+
"""
704+
String contained in <w:rStyle> child, or None if that element is not
705+
present.
706+
"""
707+
rStyle = self.rStyle
708+
if rStyle is None:
709+
return None
710+
return rStyle.val
711+
683712
@property
684713
def vanish(self):
685714
"""

docx/text.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,10 @@ def strike(self):
303303
@property
304304
def style(self):
305305
"""
306-
The string name of the character style applied to this run, or |None|
307-
if it has no directly-applied character style.
306+
Read/write. The string name of the character style applied to this
307+
run, or |None| if it has no directly-applied character style.
308308
"""
309-
raise NotImplementedError
309+
return self._r.style
310310

311311
@property
312312
def text(self):

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 <char style>
109
Then the style of the run is <char style>

tests/oxml/unitdata/text.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,9 @@ def an_rPr():
162162
return CT_RPrBuilder()
163163

164164

165+
def an_rStyle():
166+
return CT_StringBuilder('w:rStyle')
167+
168+
165169
def an_rtl():
166170
return CT_OnOffBuilder('w:rtl')

tests/test_text.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
a_b, a_bCs, a_br, a_caps, a_cs, a_dstrike, a_p, a_shadow, a_smallCaps,
2121
a_snapToGrid, a_specVanish, a_strike, a_t, a_vanish, a_webHidden,
2222
an_emboss, an_i, an_iCs, an_imprint, an_oMath, a_noProof, an_outline,
23-
an_r, an_rPr, an_rtl
23+
an_r, an_rPr, an_rStyle, an_rtl
2424
)
2525
from .unitutil import class_mock, instance_mock
2626

@@ -135,6 +135,10 @@ def it_can_change_its_bool_prop_settings(self, bool_prop_set_fixture):
135135
setattr(run, prop_name, value)
136136
assert run._r.xml == expected_xml
137137

138+
def it_knows_its_character_style(self, style_get_fixture):
139+
run, expected_style = style_get_fixture
140+
assert run.style == expected_style
141+
138142
def it_can_add_text(self, add_text_fixture):
139143
run, text_str, expected_xml, Text_ = add_text_fixture
140144
_text = run.add_text(text_str)
@@ -306,14 +310,18 @@ def bool_prop_set_fixture(self, request):
306310
expected_xml = an_r().with_nsdecls().with_child(rPr_bldr).xml()
307311
return run, bool_prop_name, value, expected_xml
308312

309-
@pytest.fixture
310-
def run(self):
311-
r = an_r().with_nsdecls().element
312-
return Run(r)
313-
314-
@pytest.fixture
315-
def Text_(self, request):
316-
return class_mock(request, 'docx.text.Text')
313+
@pytest.fixture(params=['Foobar', None])
314+
def style_get_fixture(self, request):
315+
style = request.param
316+
r_bldr = an_r().with_nsdecls()
317+
if style is not None:
318+
r_bldr.with_child(
319+
an_rPr().with_child(
320+
an_rStyle().with_val(style))
321+
)
322+
r = r_bldr.element
323+
run = Run(r)
324+
return run, style
317325

318326
@pytest.fixture
319327
def text_prop_fixture(self, Text_):
@@ -324,3 +332,14 @@ def text_prop_fixture(self, Text_):
324332
).element
325333
run = Run(r)
326334
return run, 'foobar'
335+
336+
# fixture components ---------------------------------------------
337+
338+
@pytest.fixture
339+
def run(self):
340+
r = an_r().with_nsdecls().element
341+
return Run(r)
342+
343+
@pytest.fixture
344+
def Text_(self, request):
345+
return class_mock(request, 'docx.text.Text')

0 commit comments

Comments
 (0)
X Tutup