diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-02-04 08:28:03 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-02-04 13:34:16 +0000 |
| commit | 8408269bfec3cd2f92e02075cb05ef1a4772402d (patch) | |
| tree | 9697ff393175d9f966932a68c9cdf10e44faeb45 | |
| parent | 53804571b26abb1d020a4fc6a05be546c787b30a (diff) | |
Handle missing run styles in the source document
| -rw-r--r-- | lib/docx/body-reader.js | 21 | ||||
| -rw-r--r-- | tests/docx/body-reader.tests.js | 16 |
2 files changed, 25 insertions, 12 deletions
diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js index ecf3e12..3837d4a 100644 --- a/lib/docx/body-reader.js +++ b/lib/docx/body-reader.js @@ -37,8 +37,9 @@ function BodyReader(options) { var properties = { type: "runProperties" }; + var messages = []; - readRunStyle(properties, element); + readRunStyle(properties, element, messages); var verticalAlignmentElement = element.first("w:vertAlign"); if (verticalAlignmentElement) { @@ -50,17 +51,21 @@ function BodyReader(options) { properties.isItalic = !!element.first("w:i"); properties.isStrikethrough = !!element.first("w:strike"); - return elementResult(properties); + return elementResultWithMessages(properties, messages); } - function readRunStyle(properties, element) { + function readRunStyle(properties, element, messages) { var styleElement = element.first("w:rStyle"); if (styleElement) { var styleId = styleElement.attributes["w:val"]; properties.styleId = styleId; if (styleId) { var style = styles.findCharacterStyleById(styleId); - properties.styleName = style.name; + if (style) { + properties.styleName = style.name; + } else { + messages.push(undefinedStyleWarning("Run", styleId)); + } } } } @@ -106,8 +111,7 @@ function BodyReader(options) { if (style) { properties.styleName = style.name; } else { - messages.push(warning( - "Style with ID " + styleId + " was referenced but not defined in the document")); + messages.push(undefinedStyleWarning("Paragraph", styleId)); } } } @@ -269,6 +273,11 @@ function BodyReader(options) { [] : warning("Image of type " + contentType + " is unlikely to display in web browsers"); return elementResultWithMessages(image, warnings); } + + function undefinedStyleWarning(type, styleId) { + return warning( + type + " style with ID " + styleId + " was referenced but not defined in the document"); + } } var supportedImageTypes = { diff --git a/tests/docx/body-reader.tests.js b/tests/docx/body-reader.tests.js index 9c93569..e36bccc 100644 --- a/tests/docx/body-reader.tests.js +++ b/tests/docx/body-reader.tests.js @@ -69,7 +69,7 @@ describe("readXmlElement: ", function() { 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")]); + assert.deepEqual(result.messages, [warning("Paragraph style with ID Heading1 was referenced but not defined in the document")]); }); test("paragraph has justification read from paragraph properties if present", function() { @@ -146,7 +146,7 @@ describe("readXmlElement: ", function() { assert.deepEqual(run.styleId, null); }); - test("run has style ID read from run properties if present", function() { + test("run has style ID and name read from run properties if present", function() { var runStyleXml = new XmlElement("w:rStyle", {"w:val": "Heading1Char"}); var runXml = runWithProperties([runStyleXml]); @@ -154,16 +154,20 @@ describe("readXmlElement: ", function() { var run = readXmlElementValue(runXml, {styles: styles}); assert.deepEqual(run.styleId, "Heading1Char"); + assert.deepEqual(run.styleName, "Heading 1 Char"); }); - test("run has style name read from run properties and styles", function() { + test("warning is emitted when run style cannot be found", function() { var runStyleXml = new XmlElement("w:rStyle", {"w:val": "Heading1Char"}); var runXml = runWithProperties([runStyleXml]); - var styles = new Styles({}, {"Heading1Char": {name: "Heading 1 Char"}}); + var styles = new Styles({}, {}); - var run = readXmlElementValue(runXml, {styles: styles}); - assert.deepEqual(run.styleName, "Heading 1 Char"); + var result = readXmlElement(runXml, {styles: styles}); + var run = result.value; + assert.deepEqual(run.styleId, "Heading1Char"); + assert.deepEqual(run.styleName, null); + assert.deepEqual(result.messages, [warning("Run style with ID Heading1Char was referenced but not defined in the document")]); }); test("isBold is false if bold element is not present", function() { |
