diff options
| author | Michael Williamson <mike@zwobble.org> | 2019-03-17 20:53:23 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2019-03-17 20:53:23 +0000 |
| commit | e568765535b48643e1377dfd8479920fa755feb0 (patch) | |
| tree | 5853eace48c39cc3e074e5f4ca6591db7da33f09 | |
| parent | 82f8b03022950069c4ee2239e9fa4368fbecd661 (diff) | |
Follow w:numStyleLink in w:abstractNum
| -rw-r--r-- | NEWS | 12 | ||||
| -rw-r--r-- | lib/docx/numbering-xml.js | 27 | ||||
| -rw-r--r-- | test/docx/numbering-xml.tests.js | 26 |
3 files changed, 53 insertions, 12 deletions
@@ -1,3 +1,7 @@ +# 1.4.8 + +* Improve list support by following w:numStyleLink in w:abstractNum. + # 1.4.7 * Update xmlbuilder dependency. @@ -117,7 +121,7 @@ * Generate messages of type "error" instead of "warning" when image conversion throws an exception. - + * Use alt text title as alt text for images when the alt text description is blank. @@ -215,7 +219,7 @@ * Generate warnings for not-understood style mappings and continue, rather than stopping with an error. - + * Add support for embedded style maps. * Add support for linked images. @@ -340,9 +344,9 @@ * Allow paragraphs and runs to be matched by style name. For instance, to match a paragraph with the style name `Heading 1`: - + p[style-name='Heading 1'] - + * Use an array of raw strings rather than an array of style mappings when passing options to convertToHtml. diff --git a/lib/docx/numbering-xml.js b/lib/docx/numbering-xml.js index 028bf01..90e479e 100644 --- a/lib/docx/numbering-xml.js +++ b/lib/docx/numbering-xml.js @@ -2,16 +2,24 @@ exports.readNumberingXml = readNumberingXml; exports.Numbering = Numbering; exports.defaultNumbering = new Numbering({}); -function Numbering(nums, abstractNums) { - return { - findLevel: function(numId, level) { - var num = nums[numId]; - if (num) { +function Numbering(nums, abstractNums, styles) { + function findLevel(numId, level) { + var num = nums[numId]; + if (num) { + var abstractNum = abstractNums[num.abstractNumId]; + if (abstractNum.numStyleLink == null) { return abstractNums[num.abstractNumId].levels[level]; } else { - return null; + var style = styles.findNumberingStyleById(abstractNum.numStyleLink); + return findLevel(style.numId, level); } + } else { + return null; } + } + + return { + findLevel: findLevel }; } @@ -22,7 +30,7 @@ function readNumberingXml(root, options) { var abstractNums = readAbstractNums(root); var nums = readNums(root, abstractNums); - return new Numbering(nums, abstractNums); + return new Numbering(nums, abstractNums, options.styles); } function readAbstractNums(root) { @@ -44,7 +52,10 @@ function readAbstractNum(element) { level: levelIndex }; }); - return {levels: levels}; + + var numStyleLink = element.firstOrEmpty("w:numStyleLink").attributes["w:val"]; + + return {levels: levels, numStyleLink: numStyleLink}; } function readNums(root) { diff --git a/test/docx/numbering-xml.tests.js b/test/docx/numbering-xml.tests.js index a9f825a..dd400a7 100644 --- a/test/docx/numbering-xml.tests.js +++ b/test/docx/numbering-xml.tests.js @@ -32,6 +32,32 @@ test('w:num element inherits levels from w:abstractNum', function() { })); }); + +test('when w:abstractNum has w:numStyleLink then style is used to find w:num', function() { + var numbering = readNumberingXml( + new XmlElement("w:numbering", {}, [ + new XmlElement("w:abstractNum", {"w:abstractNumId": "100"}, [ + new XmlElement("w:lvl", {"w:ilvl": "0"}, [ + new XmlElement("w:numFmt", {"w:val": "decimal"}) + ]) + ]), + new XmlElement("w:abstractNum", {"w:abstractNumId": "101"}, [ + new XmlElement("w:numStyleLink", {"w:val": "List1"}) + ]), + new XmlElement("w:num", {"w:numId": "200"}, [ + new XmlElement("w:abstractNumId", {"w:val": "100"}) + ]), + new XmlElement("w:num", {"w:numId": "201"}, [ + new XmlElement("w:abstractNumId", {"w:val": "101"}) + ]) + ]), + {styles: new stylesReader.Styles({}, {}, {}, {"List1": {numId: "200"}})} + ); + duck.assertThat(numbering.findLevel("201", "0"), duck.hasProperties({ + isOrdered: true + })); +}); + test('when styles is missing then error is thrown', function() { assert.throws(function() { readNumberingXml(new XmlElement("w:numbering", {}, [])); |
