Skip to content

Commit 9ca8f46

Browse files
author
Steve Canny
committed
oxml: add CT_Text.new()
1 parent f539c77 commit 9ca8f46

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-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, CT_R
17+
from docx.oxml.text import CT_P, CT_R, CT_Text
1818

1919

2020
# ===========================================================================
@@ -24,3 +24,4 @@
2424
register_custom_element_class('w:body', CT_Body)
2525
register_custom_element_class('w:p', CT_P)
2626
register_custom_element_class('w:r', CT_R)
27+
register_custom_element_class('w:t', CT_Text)

docx/oxml/text.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,16 @@ def new():
4848
"""
4949
xml = '<w:r %s/>' % nsdecls('w')
5050
return oxml_fromstring(xml)
51+
52+
53+
class CT_Text(OxmlBaseElement):
54+
"""
55+
``<w:t>`` element, containing a sequence of characters within a run.
56+
"""
57+
@staticmethod
58+
def new(text):
59+
"""
60+
Return a new ``<w:t>`` element.
61+
"""
62+
xml = '<w:t %s>%s</w:t>' % (nsdecls('w'), text)
63+
return oxml_fromstring(xml)

tests/oxml/test_text.py

Lines changed: 10 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, CT_R
12+
from docx.oxml.text import CT_P, CT_R, CT_Text
1313

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

1616

1717
class DescribeCT_P(object):
@@ -35,3 +35,11 @@ class DescribeCT_R(object):
3535
def it_can_construct_a_new_r_element(self):
3636
r = CT_R.new()
3737
assert r.xml == an_r().with_nsdecls().xml
38+
39+
40+
class DescribeCT_Text(object):
41+
42+
def it_can_construct_a_new_t_element(self):
43+
text = 'foobar'
44+
t = CT_Text.new(text)
45+
assert t.xml == a_t(text).with_nsdecls().xml

tests/unitdata.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,23 @@ def xml(self):
134134
return '%s<w:r%s/>\n' % (indent, self._nsdecls)
135135

136136

137+
class CT_TextBuilder(BaseBuilder):
138+
"""
139+
Test data builder for a CT_Text (<w:t>) XML element that appears within a
140+
run element.
141+
"""
142+
def __init__(self, text):
143+
"""Establish instance variables with default values"""
144+
super(CT_TextBuilder, self).__init__()
145+
self._text = text
146+
147+
@property
148+
def xml(self):
149+
"""Return element XML based on attribute settings"""
150+
indent = ' ' * self._indent
151+
return '%s<w:t%s>%s</w:t>\n' % (indent, self._nsdecls, self._text)
152+
153+
137154
class CT_SectPrBuilder(BaseBuilder):
138155
"""
139156
Test data builder for a CT_SectPr (<w:sectPr>) XML element that appears
@@ -161,6 +178,11 @@ def a_p():
161178
return CT_PBuilder()
162179

163180

181+
def a_t(text):
182+
"""Return a CT_TextBuilder instance"""
183+
return CT_TextBuilder(text)
184+
185+
164186
def a_sectPr():
165187
"""Return a CT_SectPr instance"""
166188
return CT_SectPrBuilder()

0 commit comments

Comments
 (0)