Skip to content

Commit 7592a60

Browse files
author
Steve Canny
committed
oxml: convert CT_Tc to xmlchemy
1 parent 965aaf1 commit 7592a60

File tree

3 files changed

+18
-41
lines changed

3 files changed

+18
-41
lines changed

docx/oxml/table.py

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
from __future__ import absolute_import, print_function, unicode_literals
88

99
from . import OxmlElement
10-
from .ns import qn
11-
from .text import CT_P
12-
from .xmlchemy import BaseOxmlElement, OneAndOnlyOne, ZeroOrOne, ZeroOrMore
10+
from .xmlchemy import (
11+
BaseOxmlElement, OneAndOnlyOne, OneOrMore, ZeroOrOne, ZeroOrMore
12+
)
1313

1414

1515
class CT_Row(BaseOxmlElement):
@@ -92,19 +92,25 @@ class CT_Tc(BaseOxmlElement):
9292
"""
9393
``<w:tc>`` table cell element
9494
"""
95-
def add_p(self):
95+
tcPr = ZeroOrOne('w:tcPr') # bunches of successors, overriding insert
96+
p = OneOrMore('w:p')
97+
98+
def _insert_tcPr(self, tcPr):
9699
"""
97-
Return a new <w:p> element that has been added at the end of any
98-
existing cell content.
100+
``tcPr`` has a bunch of successors, but it comes first if it appears,
101+
so just overriding and using insert(0, ...) rather than spelling out
102+
successors.
99103
"""
100-
p = CT_P.new()
101-
self.append(p)
102-
return p
104+
self.insert(0, tcPr)
105+
return tcPr
103106

104107
def clear_content(self):
105108
"""
106109
Remove all content child elements, preserving the ``<w:tcPr>``
107-
element if present.
110+
element if present. Note that this leaves the ``<w:tc>`` element in
111+
an invalid state because it doesn't contain at least one block-level
112+
element. It's up to the caller to add a ``<w:p>`` or ``<w:tbl>``
113+
child element.
108114
"""
109115
new_children = []
110116
tcPr = self.tcPr
@@ -119,20 +125,5 @@ def new(cls):
119125
required EG_BlockLevelElt.
120126
"""
121127
tc = OxmlElement('w:tc')
122-
p = CT_P.new()
123-
tc.append(p)
128+
tc._add_p()
124129
return tc
125-
126-
@property
127-
def p_lst(self):
128-
"""
129-
List of <w:p> child elements.
130-
"""
131-
return self.findall(qn('w:p'))
132-
133-
@property
134-
def tcPr(self):
135-
"""
136-
<w:tcPr> child element or |None| if not present.
137-
"""
138-
return self.find(qn('w:tcPr'))

docx/oxml/text.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,6 @@ def get_or_add_pPr(self):
6161
pPr = self._add_pPr()
6262
return pPr
6363

64-
@staticmethod
65-
def new():
66-
"""
67-
Return a new ``<w:p>`` element.
68-
"""
69-
xml = '<w:p %s/>' % nsdecls('w')
70-
p = parse_xml(xml)
71-
return p
72-
7364
@property
7465
def pPr(self):
7566
"""

tests/oxml/test_text.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@
44
Test suite for the docx.oxml.text module.
55
"""
66

7-
from docx.oxml.text import CT_P, CT_PPr, CT_R, CT_Text
7+
from docx.oxml.text import CT_PPr, CT_R, CT_Text
88

99
from .unitdata.text import a_p, a_pPr, a_pStyle, a_t, an_r
1010

1111

1212
class DescribeCT_P(object):
1313

14-
def it_can_construct_a_new_p_element(self):
15-
p = CT_P.new()
16-
expected_xml = a_p().with_nsdecls().xml()
17-
assert p.xml == expected_xml
18-
1914
def it_has_a_sequence_of_the_runs_it_contains(self):
2015
p = a_p().with_nsdecls().with_child(an_r()).with_child(an_r()).element
2116
assert len(p.r_lst) == 2

0 commit comments

Comments
 (0)