Skip to content

Deleted text in a list breaks setting of list attributes #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 17, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pydocx/DocxParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ def _set_list_attributes(self, el):
list_elements = el.find_all('numId')
for li in list_elements:
parent = li.find_ancestor_with_tag('p')
# Deleted text in a list will have a numId but no ilvl.
if parent.find_first('ilvl') is None:
continue
parent.is_list_item = True
parent.num_id = parent.find_first('numId').attrib['val']
parent.ilvl = parent.find_first('ilvl').attrib['val']
Expand Down
4 changes: 4 additions & 0 deletions pydocx/tests/templates/p.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
<w:pStyle w:val="style0"/>
{% if is_list %}
<w:numPr>
{% if ilvl != None %}
<w:ilvl w:val="{{ ilvl }}"/>
{% endif %}
{% if numId != None %}
<w:numId w:val="{{ numId }}"/>
{% endif %}
</w:numPr>
{% endif %}
</w:pPr>
Expand Down
27 changes: 27 additions & 0 deletions pydocx/tests/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,3 +716,30 @@ def get_xml(self):

xml = DXB.xml(body)
return xml


class MissingIlvl(_TranslationTestCase):
expected_output = '''
<html><body>
<ol data-list-type="decimal">
<li>AAA<br/>
BBB
</li>
<li>CCC</li>
</ol>
</body></html>
'''

def get_xml(self):
li_text = [
('AAA', 0, 1),
('BBB', None, 1), # Because why not.
('CCC', 0, 1),
]
lis = ''
for text, ilvl, numId in li_text:
lis += DXB.li(text=text, ilvl=ilvl, numId=numId)
body = lis

xml = DXB.xml(body)
return xml