Skip to content

Commit 34a475b

Browse files
author
Steve Canny
committed
oxml: convert CT_R to xmlchemy
1 parent 6d12360 commit 34a475b

File tree

2 files changed

+14
-67
lines changed

2 files changed

+14
-67
lines changed

docx/oxml/text.py

Lines changed: 14 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -106,50 +106,32 @@ class CT_R(BaseOxmlElement):
106106
"""
107107
``<w:r>`` element, containing the properties and text for a run.
108108
"""
109+
rPr = ZeroOrOne('w:rPr')
110+
t = ZeroOrMore('w:t')
109111
br = ZeroOrMore('w:br')
112+
drawing = ZeroOrMore('w:drawing')
110113

111-
def add_drawing(self, inline_or_anchor):
112-
"""
113-
Return a newly appended ``CT_Drawing`` (``<w:drawing>``) child
114-
element having *inline_or_anchor* as its child.
115-
"""
116-
drawing = OxmlElement('w:drawing')
117-
self.append(drawing)
118-
drawing.append(inline_or_anchor)
119-
return drawing
114+
def _insert_rPr(self, rPr):
115+
self.insert(0, rPr)
116+
return rPr
120117

121118
def add_t(self, text):
122119
"""
123-
Return a newly added CT_T (<w:t>) element containing *text*.
120+
Return a newly added ``<w:t>`` element containing *text*.
124121
"""
125-
t = CT_Text.new(text)
122+
t = self._add_t(text=text)
126123
if len(text.strip()) < len(text):
127124
t.set(qn('xml:space'), 'preserve')
128-
self.append(t)
129125
return t
130126

131-
def get_or_add_rPr(self):
132-
"""
133-
Return the rPr child element, newly added if not present.
134-
"""
135-
rPr = self.rPr
136-
if rPr is None:
137-
rPr = self._add_rPr()
138-
return rPr
139-
140-
@classmethod
141-
def new(cls):
142-
"""
143-
Return a new ``<w:r>`` element.
144-
"""
145-
return OxmlElement('w:r')
146-
147-
@property
148-
def rPr(self):
127+
def add_drawing(self, inline_or_anchor):
149128
"""
150-
``<w:rPr>`` child element or None if not present.
129+
Return a newly appended ``CT_Drawing`` (``<w:drawing>``) child
130+
element having *inline_or_anchor* as its child.
151131
"""
152-
return self.find(qn('w:rPr'))
132+
drawing = self._add_drawing()
133+
drawing.append(inline_or_anchor)
134+
return drawing
153135

154136
@property
155137
def style(self):
@@ -171,13 +153,6 @@ def style(self, style):
171153
rPr = self.get_or_add_rPr()
172154
rPr.style = style
173155

174-
@property
175-
def t_lst(self):
176-
"""
177-
Sequence of <w:t> elements in this paragraph.
178-
"""
179-
return self.findall(qn('w:t'))
180-
181156
@property
182157
def underline(self):
183158
"""
@@ -194,14 +169,6 @@ def underline(self, value):
194169
rPr = self.get_or_add_rPr()
195170
rPr.underline = value
196171

197-
def _add_rPr(self):
198-
"""
199-
Return a newly added rPr child element. Assumes one is not present.
200-
"""
201-
rPr = CT_RPr.new()
202-
self.insert(0, rPr)
203-
return rPr
204-
205172

206173
class CT_RPr(BaseOxmlElement):
207174
"""
@@ -699,14 +666,6 @@ class CT_Text(BaseOxmlElement):
699666
"""
700667
``<w:t>`` element, containing a sequence of characters within a run.
701668
"""
702-
@classmethod
703-
def new(cls, text):
704-
"""
705-
Return a new ``<w:t>`` element.
706-
"""
707-
t = OxmlElement('w:t')
708-
t.text = text
709-
return t
710669

711670

712671
class CT_Underline(BaseOxmlElement):

tests/oxml/test_text.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ def it_can_set_the_paragraph_style(self):
8181

8282
class DescribeCT_R(object):
8383

84-
def it_can_construct_a_new_r_element(self):
85-
r = CT_R.new()
86-
assert r.xml == an_r().with_nsdecls().xml()
87-
8884
def it_can_add_a_t_to_itself(self):
8985
text = 'foobar'
9086
r = an_r().with_nsdecls().element
@@ -111,11 +107,3 @@ def it_has_a_sequence_of_the_t_elms_it_contains(self):
111107
assert len(r.t_lst) == expected_len
112108
for t in r.t_lst:
113109
assert isinstance(t, CT_Text)
114-
115-
116-
class DescribeCT_Text(object):
117-
118-
def it_can_construct_a_new_t_element(self):
119-
text = 'foobar'
120-
t = CT_Text.new(text)
121-
assert t.xml == a_t().with_nsdecls().with_text(text).xml()

0 commit comments

Comments
 (0)