Skip to content

Commit 9b6461d

Browse files
committed
Merge pull request #23 from OpenScienceFramework/issue_23
Deleted text in a list breaks setting of list attributes
2 parents dd7be17 + c31d882 commit 9b6461d

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

pydocx/DocxParser.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ def _set_list_attributes(self, el):
202202
list_elements = el.find_all('numId')
203203
for li in list_elements:
204204
parent = li.find_ancestor_with_tag('p')
205+
# Deleted text in a list will have a numId but no ilvl.
206+
if parent.find_first('ilvl') is None:
207+
continue
205208
parent.is_list_item = True
206209
parent.num_id = parent.find_first('numId').attrib['val']
207210
parent.ilvl = parent.find_first('ilvl').attrib['val']

pydocx/tests/templates/p.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
<w:pStyle w:val="style0"/>
44
{% if is_list %}
55
<w:numPr>
6+
{% if ilvl != None %}
67
<w:ilvl w:val="{{ ilvl }}"/>
8+
{% endif %}
9+
{% if numId != None %}
710
<w:numId w:val="{{ numId }}"/>
11+
{% endif %}
812
</w:numPr>
913
{% endif %}
1014
</w:pPr>

pydocx/tests/test_xml.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,3 +716,30 @@ def get_xml(self):
716716

717717
xml = DXB.xml(body)
718718
return xml
719+
720+
721+
class MissingIlvl(_TranslationTestCase):
722+
expected_output = '''
723+
<html><body>
724+
<ol data-list-type="decimal">
725+
<li>AAA<br/>
726+
BBB
727+
</li>
728+
<li>CCC</li>
729+
</ol>
730+
</body></html>
731+
'''
732+
733+
def get_xml(self):
734+
li_text = [
735+
('AAA', 0, 1),
736+
('BBB', None, 1), # Because why not.
737+
('CCC', 0, 1),
738+
]
739+
lis = ''
740+
for text, ilvl, numId in li_text:
741+
lis += DXB.li(text=text, ilvl=ilvl, numId=numId)
742+
body = lis
743+
744+
xml = DXB.xml(body)
745+
return xml

0 commit comments

Comments
 (0)