summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-02-04 08:20:54 +0000
committerMichael Williamson <mike@zwobble.org>2016-02-04 13:34:16 +0000
commit53804571b26abb1d020a4fc6a05be546c787b30a (patch)
tree4b278eee9e4e5e03ec5f22e3f380c7acbf32041f
parentb7c2373a32a64050986288a2eb68c4fd69f7e7eb (diff)
Handle missing paragraph styles in the source document
-rw-r--r--lib/docx/body-reader.js10
-rw-r--r--tests/docx/body-reader.tests.js14
2 files changed, 17 insertions, 7 deletions
diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js
index 618474e..ecf3e12 100644
--- a/lib/docx/body-reader.js
+++ b/lib/docx/body-reader.js
@@ -95,6 +95,7 @@ function BodyReader(options) {
var properties = {
type: "paragraphProperties"
};
+ var messages = [];
var styleElement = element.first("w:pStyle");
if (styleElement) {
@@ -102,7 +103,12 @@ function BodyReader(options) {
properties.styleId = styleId;
if (styleId) {
var style = styles.findParagraphStyleById(styleId);
- properties.styleName = style.name;
+ if (style) {
+ properties.styleName = style.name;
+ } else {
+ messages.push(warning(
+ "Style with ID " + styleId + " was referenced but not defined in the document"));
+ }
}
}
var alignElement = element.first("w:jc");
@@ -111,7 +117,7 @@ function BodyReader(options) {
}
properties.numbering = readNumberingProperties(element.firstOrEmpty("w:numPr"));
- return elementResult(properties);
+ return elementResultWithMessages(properties, messages);
},
"w:r": function(element) {
return readXmlElements(element.children)
diff --git a/tests/docx/body-reader.tests.js b/tests/docx/body-reader.tests.js
index d1a5585..9c93569 100644
--- a/tests/docx/body-reader.tests.js
+++ b/tests/docx/body-reader.tests.js
@@ -46,7 +46,7 @@ describe("readXmlElement: ", function() {
assert.deepEqual(paragraph.styleId, null);
});
- test("paragraph has style ID read from paragraph properties if present", function() {
+ test("paragraph has style ID and name read from paragraph properties if present", function() {
var styleXml = new XmlElement("w:pStyle", {"w:val": "Heading1"}, []);
var propertiesXml = new XmlElement("w:pPr", {}, [styleXml]);
var paragraphXml = new XmlElement("w:p", {}, [propertiesXml]);
@@ -55,17 +55,21 @@ describe("readXmlElement: ", function() {
var paragraph = readXmlElementValue(paragraphXml, {styles: styles});
assert.deepEqual(paragraph.styleId, "Heading1");
+ assert.deepEqual(paragraph.styleName, "Heading 1");
});
- test("paragraph has style name read from paragraph properties and styles", function() {
+ test("warning is emitted when paragraph style cannot be found", function() {
var styleXml = new XmlElement("w:pStyle", {"w:val": "Heading1"}, []);
var propertiesXml = new XmlElement("w:pPr", {}, [styleXml]);
var paragraphXml = new XmlElement("w:p", {}, [propertiesXml]);
- var styles = new Styles({"Heading1": {name: "Heading 1"}}, {});
+ var styles = new Styles({}, {});
- var paragraph = readXmlElementValue(paragraphXml, {styles: styles});
- assert.deepEqual(paragraph.styleName, "Heading 1");
+ var result = readXmlElement(paragraphXml, {styles: styles});
+ var paragraph = result.value;
+ assert.deepEqual(paragraph.styleId, "Heading1");
+ assert.deepEqual(paragraph.styleName, null);
+ assert.deepEqual(result.messages, [warning("Style with ID Heading1 was referenced but not defined in the document")]);
});
test("paragraph has justification read from paragraph properties if present", function() {