diff options
| author | Michael Williamson <mike@zwobble.org> | 2019-03-17 10:24:44 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2019-03-17 10:24:44 +0000 |
| commit | 59d569588c22a429b6da8a696119b7b35a6603a9 (patch) | |
| tree | 70f29c19ec1ba318c282759ffe9f596652938f24 | |
| parent | fcd3d606cfcd58e715705aaae0627a425e5484a5 (diff) | |
Read numbering styles
| -rw-r--r-- | lib/docx/styles-reader.js | 35 | ||||
| -rw-r--r-- | test/docx/styles-reader.tests.js | 31 |
2 files changed, 57 insertions, 9 deletions
diff --git a/lib/docx/styles-reader.js b/lib/docx/styles-reader.js index ae288ab..d99dbb7 100644 --- a/lib/docx/styles-reader.js +++ b/lib/docx/styles-reader.js @@ -2,7 +2,7 @@ exports.readStylesXml = readStylesXml; exports.Styles = Styles; exports.defaultStyles = new Styles({}, {}); -function Styles(paragraphStyles, characterStyles, tableStyles) { +function Styles(paragraphStyles, characterStyles, tableStyles, numberingStyles) { return { findParagraphStyleById: function(styleId) { return paragraphStyles[styleId]; @@ -12,32 +12,40 @@ function Styles(paragraphStyles, characterStyles, tableStyles) { }, findTableStyleById: function(styleId) { return tableStyles[styleId]; + }, + findNumberingStyleById: function(styleId) { + return numberingStyles[styleId]; } }; } -Styles.EMPTY = new Styles({}, {}, {}); +Styles.EMPTY = new Styles({}, {}, {}, {}); function readStylesXml(root) { var paragraphStyles = {}; var characterStyles = {}; var tableStyles = {}; - + var numberingStyles = {}; + var styles = { "paragraph": paragraphStyles, "character": characterStyles, "table": tableStyles }; - + root.getElementsByTagName("w:style").forEach(function(styleElement) { var style = readStyleElement(styleElement); - var styleSet = styles[style.type]; - if (styleSet) { - styleSet[style.styleId] = style; + if (style.type === "numbering") { + numberingStyles[style.styleId] = readNumberingStyleElement(styleElement); + } else { + var styleSet = styles[style.type]; + if (styleSet) { + styleSet[style.styleId] = style; + } } }); - - return new Styles(paragraphStyles, characterStyles, tableStyles); + + return new Styles(paragraphStyles, characterStyles, tableStyles, numberingStyles); } function readStyleElement(styleElement) { @@ -51,3 +59,12 @@ function styleName(styleElement) { var nameElement = styleElement.first("w:name"); return nameElement ? nameElement.attributes["w:val"] : null; } + +function readNumberingStyleElement(styleElement) { + var numId = styleElement + .firstOrEmpty("w:pPr") + .firstOrEmpty("w:numPr") + .firstOrEmpty("w:numId") + .attributes["w:val"]; + return {numId: numId}; +} diff --git a/test/docx/styles-reader.tests.js b/test/docx/styles-reader.tests.js index 43cba98..0997d2c 100644 --- a/test/docx/styles-reader.tests.js +++ b/test/docx/styles-reader.tests.js @@ -79,6 +79,37 @@ test('style name is null if w:name element does not exist', function() { assert.equal(styles.findCharacterStyleById("Heading1Char").name, null); }); +test('numbering style is null if no style with that ID exists', function() { + var styles = readStylesXml( + new XmlElement("w:styles", {}, []) + ); + assert.equal(styles.findNumberingStyleById("List1"), null); +}); + +test('numbering style has null numId if style has no paragraph properties', function() { + var styles = readStylesXml( + new XmlElement("w:styles", {}, [ + new XmlElement("w:style", {"w:type": "numbering", "w:styleId": "List1"}) + ]) + ); + assert.equal(styles.findNumberingStyleById("List1").numId, null); +}); + +test('numbering style has numId read from paragraph properties', function() { + var styles = readStylesXml( + new XmlElement("w:styles", {}, [ + new XmlElement("w:style", {"w:type": "numbering", "w:styleId": "List1"}, [ + new XmlElement("w:pPr", {}, [ + new XmlElement("w:numPr", {}, [ + new XmlElement("w:numId", {"w:val": "42"}) + ]) + ]) + ]) + ]) + ); + assert.equal(styles.findNumberingStyleById("List1").numId, "42"); +}); + function paragraphStyleElement(id, name) { return styleElement("paragraph", id, name); } |
