Skip to content

Commit 525d1a4

Browse files
committed
Fixed unittests by wrapping all texts in <p>
1 parent 8b907c3 commit 525d1a4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+764
-475
lines changed

pydocx/export/base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,6 @@ def export_run(self, run):
341341
if self.first_pass:
342342
if self.captured_runs is not None:
343343
self.captured_runs.append(run)
344-
345344
# TODO squash multiple sequential text nodes into one?
346345
results = self.yield_nested(run.children, self.export_node)
347346
if run.effective_properties:

pydocx/export/html.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,12 @@ def export_footnote(self, footnote):
258258
return tag.apply(results, allow_empty=False)
259259

260260
def get_paragraph_tag(self, paragraph):
261-
if self.in_table_cell and paragraph.parent.properties.is_continue_vertical_merge:
262-
# We ignore such paragraphs here because are added via rowspan
263-
return
264-
elif paragraph.is_empty:
261+
if isinstance(paragraph.parent, wordprocessing.TableCell):
262+
cell_properties = paragraph.parent.properties
263+
if cell_properties and cell_properties.is_continue_vertical_merge:
264+
# We ignore such paragraphs here because are added via rowspan
265+
return
266+
if paragraph.is_empty:
265267
return HtmlTag('p', custom_text=HTML_WHITE_SPACE)
266268

267269
heading_style = paragraph.heading_style
@@ -291,7 +293,7 @@ def export_paragraph(self, paragraph):
291293
tag = self.get_paragraph_tag(paragraph)
292294
if tag:
293295
attrs = self.get_paragraph_styles(paragraph)
294-
tag.attrs = attrs
296+
tag.attrs.update(attrs)
295297
results = tag.apply(results)
296298

297299
for result in results:
@@ -345,6 +347,11 @@ def get_paragraph_property_spacing(self, paragraph):
345347
if self.first_pass:
346348
return style
347349

350+
try:
351+
current_par_index = self.paragraphs.index(paragraph)
352+
except ValueError:
353+
return style
354+
348355
previous_paragraph = None
349356
next_paragraph = None
350357
previous_paragraph_spacing = None
@@ -353,7 +360,6 @@ def get_paragraph_property_spacing(self, paragraph):
353360
spacing_before = None
354361

355362
current_paragraph_spacing = paragraph.get_spacing()
356-
current_par_index = self.paragraphs.index(paragraph)
357363

358364
if current_par_index > 0:
359365
previous_paragraph = self.paragraphs[current_par_index - 1]

pydocx/openxml/wordprocessing/paragraph.py

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

88
from pydocx.models import XmlModel, XmlCollection, XmlChild
99
from pydocx.openxml.wordprocessing.bookmark import Bookmark
10+
from pydocx.openxml.wordprocessing.br import Break
1011
from pydocx.openxml.wordprocessing.deleted_run import DeletedRun
1112
from pydocx.openxml.wordprocessing.hyperlink import Hyperlink
1213
from pydocx.openxml.wordprocessing.inserted_run import InsertedRun
@@ -48,11 +49,14 @@ def is_empty(self):
4849

4950
# we may have cases when a paragraph has a Bookmark with name '_GoBack'
5051
# and we should treat it as empty paragraph
51-
if len(self.children) == 1 and \
52-
isinstance(self.children[0], Bookmark) and \
53-
self.children[0].name in ('_GoBack',):
54-
return True
55-
52+
if len(self.children) == 1:
53+
first_child = self.children[0]
54+
if isinstance(first_child, Bookmark) and \
55+
first_child.name in ('_GoBack',):
56+
return True
57+
# We can have cases when only run properties are defined and no text
58+
elif not first_child.children:
59+
return True
5660
return False
5761

5862
@property

tests/export/html/test_field_code.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ def test_spanning_multiple_paragraphs(self):
206206

207207
expected_html = '''
208208
<p>Link: </p>
209+
<p></p>
209210
<p><a href="http://www.google.com/">AAABBB</a></p>
210211
<p><a href="http://www.google.com/">CCC</a></p>
211212
<p><a href="http://www.google.com/">DDD</a>.</p>

tests/export/html/test_heading.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def test_heading_in_a_nested_list_numbering_is_preserved_with_strong(self):
274274
expected_html = '''
275275
<ol class="pydocx-list-style-type-decimal">
276276
<li>
277-
foo
277+
<p>foo</p>
278278
<ol class="pydocx-list-style-type-lowerLetter">
279279
<li>
280280
<strong>bar</strong>
@@ -351,10 +351,10 @@ def test_heading_in_nested_sub_list(self):
351351
expected_html = '''
352352
<ol class="pydocx-list-style-type-decimal">
353353
<li>
354-
foo
355-
<ol class="pydocx-list-style-type-lowerLetter">
356-
<li>bar</li>
357-
</ol>
354+
<p>foo</p>
355+
<ol class="pydocx-list-style-type-lowerLetter">
356+
<li><p>bar</p></li>
357+
</ol>
358358
</li>
359359
</ol>
360360
<h1>baz</h1>
@@ -416,8 +416,7 @@ def test_headings_in_list_surrounding_paragraph_stay_in_list_with_strong(self):
416416
<ol class="pydocx-list-style-type-decimal">
417417
<li>
418418
<strong>foo</strong>
419-
<br />
420-
bare paragraph
419+
<p>bare paragraph</p>
421420
</li>
422421
<li>
423422
<strong>bar</strong>
@@ -527,7 +526,7 @@ def test_heading_as_new_list_following_bare_paragraph_plus_list(self):
527526

528527
expected_html = '''
529528
<ol class="pydocx-list-style-type-decimal">
530-
<li>foo</li>
529+
<li><p>foo</p></li>
531530
</ol>
532531
<p>bare paragraph</p>
533532
<ol class="pydocx-list-style-type-decimal">
@@ -588,7 +587,10 @@ def test_heading_as_list_following_bare_paragraph_plus_list(self):
588587

589588
expected_html = '''
590589
<ol class="pydocx-list-style-type-decimal">
591-
<li>foo<br />bare paragraph</li>
590+
<li>
591+
<p>foo</p>
592+
<p>bare paragraph</p>
593+
</li>
592594
<li><strong>bar</strong></li>
593595
</ol>
594596
'''
@@ -662,13 +664,13 @@ def test_list_heading_table_paragraph(self):
662664

663665
expected_html = '''
664666
<ol class="pydocx-list-style-type-decimal">
665-
<li>single list item</li>
667+
<li><p>single list item</p></li>
666668
</ol>
667669
<h1>actual heading</h1>
668670
<p>before table</p>
669671
<table border="1">
670672
<tr>
671-
<td>foo</td>
673+
<td><p>foo</p></td>
672674
</tr>
673675
</table>
674676
<p>after table</p>
@@ -739,7 +741,7 @@ def test_single_lvl_list_has_precedence_over_headings(self):
739741
expected_html = '''
740742
<ol class="pydocx-list-style-type-decimal">
741743
<li><strong>foo</strong></li>
742-
<li>non-heading list item</li>
744+
<li><p>non-heading list item</p></li>
743745
<li><strong>bar</strong></li>
744746
</ol>
745747
'''

tests/export/html/test_hyperlink.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def test_no_link_text(self):
8686

8787
document.add(MainDocumentPart, document_xml, document_rels)
8888

89-
expected_html = ''
89+
expected_html = '<p>&#160;</p>'
9090
self.assert_document_generates_html(document, expected_html)
9191

9292
def test_undefined_relationship(self):

tests/export/html/test_markup_compatibility.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ def test_fallback_contains_a_table(self):
155155
<p>AAABBB</p>
156156
<table border="1">
157157
<tr>
158-
<td>CCC</td>
158+
<td>
159+
<p>CCC</p>
160+
</td>
159161
</tr>
160162
</table>
161163
<p>DDDEEE</p>
@@ -204,7 +206,9 @@ def test_fallback_has_invalid_children(self):
204206
<p>AAABBB
205207
<table border="1">
206208
<tr>
207-
<td>CCC</td>
209+
<td>
210+
<p>CCC</p>
211+
</td>
208212
</tr>
209213
</table>
210214
DDDEEE</p>

0 commit comments

Comments
 (0)