Skip to content

Commit 2f2d0b4

Browse files
author
Steve Canny
committed
text: add Run.italic getter
1 parent 1c30ccb commit 2f2d0b4

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

docx/oxml/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from docx.oxml.text import CT_Br, CT_P, CT_PPr, CT_R, CT_RPr, CT_Text
4242
register_custom_element_class('w:b', CT_OnOff)
4343
register_custom_element_class('w:br', CT_Br)
44+
register_custom_element_class('w:i', CT_OnOff)
4445
register_custom_element_class('w:p', CT_P)
4546
register_custom_element_class('w:pPr', CT_PPr)
4647
register_custom_element_class('w:pStyle', CT_String)

docx/oxml/text.py

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

270+
@property
271+
def i(self):
272+
"""
273+
First ``<w:i>`` child element or None if none are present.
274+
"""
275+
return self.find(qn('w:i'))
276+
270277
@classmethod
271278
def new(cls):
272279
"""

docx/text.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ def italic(self):
124124
unconditionally. When |None|, the run will inherit its italic setting
125125
from its style hierarchy.
126126
"""
127+
rPr = self._r.rPr
128+
if rPr is None:
129+
return None
130+
i = rPr.i
131+
if i is None:
132+
return None
133+
return i.val
127134

128135
@property
129136
def text(self):

tests/oxml/unitdata/text.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ def a_t():
7878
return CT_TextBuilder()
7979

8080

81+
def an_i():
82+
return CT_OnOffBuilder('w:i')
83+
84+
8185
def an_r():
8286
return CT_RBuilder()
8387

tests/test_text.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from mock import call, create_autospec, Mock
1616

17-
from .oxml.unitdata.text import a_b, a_br, a_t, a_p, an_r, an_rPr
17+
from .oxml.unitdata.text import a_b, a_br, a_t, a_p, an_i, an_r, an_rPr
1818
from .unitutil import class_mock
1919

2020

@@ -94,6 +94,10 @@ def it_knows_if_its_bold(self, bold_get_fixture):
9494
run, is_bold = bold_get_fixture
9595
assert run.bold == is_bold
9696

97+
def it_knows_if_its_italic(self, italic_get_fixture):
98+
run, is_italic = italic_get_fixture
99+
assert run.italic == is_italic
100+
97101
def it_can_change_its_bold_setting(self, bold_set_fixture):
98102
run, bold_value, expected_xml = bold_set_fixture
99103
run.bold = bold_value
@@ -160,6 +164,20 @@ def bold_get_fixture(self, request):
160164
run = Run(r)
161165
return run, is_bold
162166

167+
@pytest.fixture(params=[True, False, None])
168+
def italic_get_fixture(self, request):
169+
is_italic = request.param
170+
r_bldr = an_r().with_nsdecls()
171+
if is_italic is not None:
172+
i_bldr = an_i()
173+
if is_italic is False:
174+
i_bldr.with_val('off')
175+
rPr_bldr = an_rPr().with_child(i_bldr)
176+
r_bldr.with_child(rPr_bldr)
177+
r = r_bldr.element
178+
run = Run(r)
179+
return run, is_italic
180+
163181
@pytest.fixture(params=[True, False, None])
164182
def bold_set_fixture(self, request):
165183
# run --------------------------

0 commit comments

Comments
 (0)