Skip to content

Commit ba9e945

Browse files
author
Steve Canny
committed
oxml: add CT_R.new()
1 parent a7e2f60 commit ba9e945

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

docx/oxml/__init__.py

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

1515
from docx.oxml.base import register_custom_element_class
1616
from docx.oxml.parts import CT_Body
17-
from docx.oxml.text import CT_P
17+
from docx.oxml.text import CT_P, CT_R
1818

1919

2020
# ===========================================================================
@@ -23,3 +23,4 @@
2323

2424
register_custom_element_class('w:body', CT_Body)
2525
register_custom_element_class('w:p', CT_P)
26+
register_custom_element_class('w:r', CT_R)

docx/oxml/text.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,16 @@ def new():
3232
xml = '<w:p %s/>' % nsdecls('w')
3333
p = oxml_fromstring(xml)
3434
return p
35+
36+
37+
class CT_R(OxmlBaseElement):
38+
"""
39+
``<w:r>`` element, containing the properties and text for a run.
40+
"""
41+
@staticmethod
42+
def new():
43+
"""
44+
Return a new ``<w:r>`` element.
45+
"""
46+
xml = '<w:r %s/>' % nsdecls('w')
47+
return oxml_fromstring(xml)

tests/oxml/test_text.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
"""Test suite for the docx.oxml.text module."""
1111

12-
from docx.oxml.text import CT_P
12+
from docx.oxml.text import CT_P, CT_R
1313

14-
from ..unitdata import a_p
14+
from ..unitdata import a_p, an_r
1515

1616

1717
class DescribeCT_P(object):
@@ -20,3 +20,10 @@ def it_can_construct_a_new_p_element(self):
2020
p = CT_P.new()
2121
expected_xml = a_p().with_nsdecls().xml
2222
assert p.xml == expected_xml
23+
24+
25+
class DescribeCT_R(object):
26+
27+
def it_can_construct_a_new_r_element(self):
28+
r = CT_R.new()
29+
assert r.xml == an_r().with_nsdecls().xml

tests/unitdata.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@ def xml(self):
101101
return xml
102102

103103

104+
class CT_RBuilder(BaseBuilder):
105+
"""
106+
Test data builder for a CT_R (<w:r>) XML element that appears within the
107+
body element of a document.xml file.
108+
"""
109+
def __init__(self):
110+
"""Establish instance variables with default values"""
111+
super(CT_RBuilder, self).__init__()
112+
113+
@property
114+
def xml(self):
115+
"""Return element XML based on attribute settings"""
116+
indent = ' ' * self._indent
117+
return '%s<w:r%s/>\n' % (indent, self._nsdecls)
118+
119+
104120
class CT_SectPrBuilder(BaseBuilder):
105121
"""
106122
Test data builder for a CT_SectPr (<w:sectPr>) XML element that appears
@@ -131,3 +147,8 @@ def a_p():
131147
def a_sectPr():
132148
"""Return a CT_SectPr instance"""
133149
return CT_SectPrBuilder()
150+
151+
152+
def an_r():
153+
"""Return a CT_RBuilder instance"""
154+
return CT_RBuilder()

0 commit comments

Comments
 (0)