Skip to content

Commit f01a873

Browse files
author
Steve Canny
committed
txt: base Paragraph on Parented
1 parent 54d619f commit f01a873

File tree

5 files changed

+20
-19
lines changed

5 files changed

+20
-19
lines changed

docx/parts/document.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def add_paragraph(self):
127127
Return a paragraph newly added to the end of body content.
128128
"""
129129
p = self._body.add_p()
130-
return Paragraph(p)
130+
return Paragraph(p, self)
131131

132132
def add_table(self, rows, cols):
133133
"""
@@ -153,7 +153,7 @@ def clear_content(self):
153153

154154
@property
155155
def paragraphs(self):
156-
return [Paragraph(p) for p in self._body.p_lst]
156+
return [Paragraph(p, self) for p in self._body.p_lst]
157157

158158
@property
159159
def tables(self):

docx/table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def paragraphs(self):
9494
at least one block-level element. By default this is a single
9595
paragraph.
9696
"""
97-
return [Paragraph(p) for p in self._tc.p_lst]
97+
return [Paragraph(p, self) for p in self._tc.p_lst]
9898

9999
@write_only_property
100100
def text(self, text):

docx/text.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
from __future__ import absolute_import, print_function, unicode_literals
88

9-
from docx.enum.text import WD_BREAK
9+
from .enum.text import WD_BREAK
10+
from .shared import Parented
1011

1112

1213
def boolproperty(f):
@@ -54,12 +55,12 @@ def setter(obj, value):
5455
return property(getter, setter, doc=f.__doc__)
5556

5657

57-
class Paragraph(object):
58+
class Paragraph(Parented):
5859
"""
5960
Proxy object wrapping ``<w:p>`` element.
6061
"""
61-
def __init__(self, p):
62-
super(Paragraph, self).__init__()
62+
def __init__(self, p, parent):
63+
super(Paragraph, self).__init__(parent)
6364
self._p = p
6465

6566
def add_run(self, text=None, style=None):
@@ -110,7 +111,7 @@ def insert_paragraph_before(self, text=None, style=None):
110111
to the new paragraph.
111112
"""
112113
p = self._p.add_p_before()
113-
paragraph = Paragraph(p)
114+
paragraph = Paragraph(p, self._parent)
114115
if text:
115116
paragraph.add_run(text)
116117
if style is not None:

features/steps/paragraph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def given_a_paragraph_with_content_and_formatting(context):
5454
</w:r>
5555
</w:p>""" % (nsdecls('w'), TEST_STYLE)
5656
p = parse_xml(p_xml)
57-
context.paragraph = Paragraph(p)
57+
context.paragraph = Paragraph(p, None)
5858

5959

6060
# when ====================================================

tests/test_text.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def it_can_remove_its_content_while_preserving_formatting(
9090
])
9191
def add_run_fixture(self, request):
9292
before_cxml, text, style, after_cxml = request.param
93-
paragraph = Paragraph(element(before_cxml))
93+
paragraph = Paragraph(element(before_cxml), None)
9494
expected_xml = xml(after_cxml)
9595
return paragraph, text, style, expected_xml
9696

@@ -100,7 +100,7 @@ def add_run_fixture(self, request):
100100
])
101101
def alignment_get_fixture(self, request):
102102
cxml, expected_alignment_value = request.param
103-
paragraph = Paragraph(element(cxml))
103+
paragraph = Paragraph(element(cxml), None)
104104
return paragraph, expected_alignment_value
105105

106106
@pytest.fixture(params=[
@@ -114,7 +114,7 @@ def alignment_get_fixture(self, request):
114114
])
115115
def alignment_set_fixture(self, request):
116116
initial_cxml, new_alignment_value, expected_cxml = request.param
117-
paragraph = Paragraph(element(initial_cxml))
117+
paragraph = Paragraph(element(initial_cxml), None)
118118
expected_xml = xml(expected_cxml)
119119
return paragraph, new_alignment_value, expected_xml
120120

@@ -126,7 +126,7 @@ def alignment_set_fixture(self, request):
126126
])
127127
def clear_fixture(self, request):
128128
initial_cxml, expected_cxml = request.param
129-
paragraph = Paragraph(element(initial_cxml))
129+
paragraph = Paragraph(element(initial_cxml), None)
130130
expected_xml = xml(expected_cxml)
131131
return paragraph, expected_xml
132132

@@ -137,13 +137,13 @@ def clear_fixture(self, request):
137137
def insert_before_fixture(self, request):
138138
body_cxml, text, style, expected_cxml = request.param
139139
body = element(body_cxml)
140-
paragraph = Paragraph(body.find(qn('w:p')))
140+
paragraph = Paragraph(body.find(qn('w:p')), None)
141141
expected_xml = xml(expected_cxml)
142142
return paragraph, text, style, body, expected_xml
143143

144144
@pytest.fixture
145145
def runs_fixture(self, p_, Run_, r_, r_2_, runs_):
146-
paragraph = Paragraph(p_)
146+
paragraph = Paragraph(p_, None)
147147
run_, run_2_ = runs_
148148
return paragraph, Run_, r_, r_2_, run_, run_2_
149149

@@ -154,7 +154,7 @@ def runs_fixture(self, p_, Run_, r_, r_2_, runs_):
154154
])
155155
def style_get_fixture(self, request):
156156
p_cxml, expected_style = request.param
157-
paragraph = Paragraph(element(p_cxml))
157+
paragraph = Paragraph(element(p_cxml), None)
158158
return paragraph, expected_style
159159

160160
@pytest.fixture(params=[
@@ -171,7 +171,7 @@ def style_get_fixture(self, request):
171171
])
172172
def style_set_fixture(self, request):
173173
p_cxml, new_style_value, expected_cxml = request.param
174-
paragraph = Paragraph(element(p_cxml))
174+
paragraph = Paragraph(element(p_cxml), None)
175175
expected_xml = xml(expected_cxml)
176176
return paragraph, new_style_value, expected_xml
177177

@@ -188,12 +188,12 @@ def style_set_fixture(self, request):
188188
])
189189
def text_get_fixture(self, request):
190190
p_cxml, expected_text_value = request.param
191-
paragraph = Paragraph(element(p_cxml))
191+
paragraph = Paragraph(element(p_cxml), None)
192192
return paragraph, expected_text_value
193193

194194
@pytest.fixture
195195
def text_set_fixture(self):
196-
paragraph = Paragraph(element('w:p'))
196+
paragraph = Paragraph(element('w:p'), None)
197197
paragraph.add_run('must not appear in result')
198198
new_text_value = 'foo\tbar\rbaz\n'
199199
expected_text_value = 'foo\tbar\nbaz\n'

0 commit comments

Comments
 (0)