From 8408269bfec3cd2f92e02075cb05ef1a4772402d Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 4 Feb 2016 08:28:03 +0000 Subject: Handle missing run styles in the source document --- lib/docx/body-reader.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'lib') 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 = { -- cgit v1.2.3