From 9501fce9e3c7c36b67c559328bd587311bf45690 Mon Sep 17 00:00:00 2001 From: Jason Ward Date: Fri, 17 May 2013 12:05:31 -0400 Subject: [PATCH 1/2] refs #23: Added a test showing that list items with no ilvls were throwing errors. --- pydocx/tests/templates/p.xml | 4 ++++ pydocx/tests/test_xml.py | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/pydocx/tests/templates/p.xml b/pydocx/tests/templates/p.xml index ab376ca4..1954e6c7 100644 --- a/pydocx/tests/templates/p.xml +++ b/pydocx/tests/templates/p.xml @@ -3,8 +3,12 @@ {% if is_list %} + {% if ilvl != None %} + {% endif %} + {% if numId != None %} + {% endif %} {% endif %} diff --git a/pydocx/tests/test_xml.py b/pydocx/tests/test_xml.py index 8b6d04aa..1ca2b44e 100644 --- a/pydocx/tests/test_xml.py +++ b/pydocx/tests/test_xml.py @@ -716,3 +716,30 @@ def get_xml(self): xml = DXB.xml(body) return xml + + +class MissingIlvl(_TranslationTestCase): + expected_output = ''' + +
    +
  1. AAA
    + BBB +
  2. +
  3. CCC
  4. +
+ + ''' + + 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 From f486348bd5917dd369974d6cd69d72367969660c Mon Sep 17 00:00:00 2001 From: Jason Ward Date: Fri, 17 May 2013 12:06:09 -0400 Subject: [PATCH 2/2] refs #23: early continue if parent does not have an ilvl --- pydocx/DocxParser.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pydocx/DocxParser.py b/pydocx/DocxParser.py index 1654b912..3a3c4393 100644 --- a/pydocx/DocxParser.py +++ b/pydocx/DocxParser.py @@ -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']