aboutsummaryrefslogtreecommitdiff
path: root/tests/docx/body_xml_tests.py
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2021-01-09 13:52:02 +0000
committerMichael Williamson <mike@zwobble.org>2021-01-09 13:52:02 +0000
commit801b505840b698858c48761c640d52a5fa5ec3fb (patch)
tree00285ea140888c94bde0c17d1cc061b521ca29f0 /tests/docx/body_xml_tests.py
parentd1910bc1e649deb9f0f69114b61023458f3d8d2f (diff)
Support numbering defined by paragraph style
Diffstat (limited to 'tests/docx/body_xml_tests.py')
-rw-r--r--tests/docx/body_xml_tests.py38
1 files changed, 35 insertions, 3 deletions
diff --git a/tests/docx/body_xml_tests.py b/tests/docx/body_xml_tests.py
index 20f89a9..0f47ca2 100644
--- a/tests/docx/body_xml_tests.py
+++ b/tests/docx/body_xml_tests.py
@@ -9,6 +9,7 @@ import funk
from mammoth import documents, results
from mammoth.docx.xmlparser import element as xml_element, text as xml_text
from mammoth.docx import body_xml
+from mammoth.docx.numbering_xml import Numbering
from mammoth.docx.relationships_xml import Relationships, Relationship
from mammoth.docx.styles_xml import Styles, Style
from .document_matchers import (
@@ -114,6 +115,26 @@ class ParagraphTests(object):
assert_equal(True, paragraph.numbering.is_ordered)
@istest
+ def numbering_on_paragraph_style_takes_precedence_over_numpr(self):
+ numbering_properties_xml = xml_element("w:numPr", {}, [
+ xml_element("w:ilvl", {"w:val": "1"}),
+ xml_element("w:numId", {"w:val": "42"}),
+ ])
+ properties_xml = xml_element("w:pPr", {}, [
+ xml_element("w:pStyle", {"w:val": "List"}),
+ numbering_properties_xml,
+ ])
+ paragraph_xml = xml_element("w:p", {}, [properties_xml])
+
+ numbering = _NumberingMap(
+ nums={"42": {"1": documents.numbering_level("1", False)}},
+ levels_by_paragraph_style_id={"List": documents.numbering_level("1", True)}
+ )
+ paragraph = _read_and_get_document_xml_element(paragraph_xml, numbering=numbering)
+
+ assert_equal(True, paragraph.numbering.is_ordered)
+
+ @istest
def numbering_properties_are_ignored_if_lvl_is_missing(self):
paragraph_xml = self._paragraph_with_numbering_properties([
xml_element("w:numId", {"w:val": "42"}),
@@ -1200,8 +1221,9 @@ def _read_and_get_document_xml_elements(*args, **kwargs):
def _read_and_get_document_xml(func, *args, **kwargs):
+ numbering = kwargs.pop("numbering", Numbering.EMPTY)
styles = kwargs.pop("styles", FakeStyles())
- result = _read_document_xml(func, *args, styles=styles, **kwargs)
+ result = _read_document_xml(func, *args, numbering=numbering, styles=styles, **kwargs)
assert_equal([], result.messages)
return result.value
@@ -1221,7 +1243,8 @@ def _read_document_xml_elements(*args, **kwargs):
def _read_document_xml(func, element, *args, **kwargs):
- reader = body_xml.reader(*args, **kwargs)
+ numbering = kwargs.pop("numbering", Numbering.EMPTY)
+ reader = body_xml.reader(*args, numbering=numbering, **kwargs)
return func(reader, element)
@@ -1338,8 +1361,17 @@ def _image_relationship(relationship_id, target):
class _NumberingMap(object):
- def __init__(self, nums):
+ def __init__(self, nums=None, levels_by_paragraph_style_id=None):
+ if nums is None:
+ nums = {}
+ if levels_by_paragraph_style_id is None:
+ levels_by_paragraph_style_id = {}
+
self._nums = nums
+ self._levels_by_paragraph_style_id = levels_by_paragraph_style_id
def find_level(self, num_id, level):
return self._nums[num_id][level]
+
+ def find_level_by_paragraph_style_id(self, style_id):
+ return self._levels_by_paragraph_style_id.get(style_id)