diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-02-04 08:54:04 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-02-04 13:34:16 +0000 |
| commit | 9d3874dad6af6234274399ba6cfbad00c3d0a5aa (patch) | |
| tree | 2ce38b2824665c867b7d58b61df7f9b6b3c1ae01 | |
| parent | 8408269bfec3cd2f92e02075cb05ef1a4772402d (diff) | |
Extract common code for reading styles
| -rw-r--r-- | lib/docx/body-reader.js | 54 | ||||
| -rw-r--r-- | tests/docx/body-reader.tests.js | 13 |
2 files changed, 31 insertions, 36 deletions
diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js index 3837d4a..4e7ff03 100644 --- a/lib/docx/body-reader.js +++ b/lib/docx/body-reader.js @@ -37,9 +37,6 @@ function BodyReader(options) { var properties = { type: "runProperties" }; - var messages = []; - - readRunStyle(properties, element, messages); var verticalAlignmentElement = element.first("w:vertAlign"); if (verticalAlignmentElement) { @@ -51,23 +48,38 @@ function BodyReader(options) { properties.isItalic = !!element.first("w:i"); properties.isStrikethrough = !!element.first("w:strike"); - return elementResultWithMessages(properties, messages); + return readRunStyle(element).map(function(style) { + properties.styleId = style.styleId; + properties.styleName = style.name; + return properties; + }); } - function readRunStyle(properties, element, messages) { - var styleElement = element.first("w:rStyle"); + function readParagraphStyle(element) { + return readStyle(element, "w:pStyle", "Paragraph", styles.findParagraphStyleById); + } + + function readRunStyle(element) { + return readStyle(element, "w:rStyle", "Run", styles.findCharacterStyleById); + } + + function readStyle(element, styleTagName, styleType, findStyleById) { + var messages = []; + var styleElement = element.first(styleTagName); + var styleId = null; + var name = null; if (styleElement) { - var styleId = styleElement.attributes["w:val"]; - properties.styleId = styleId; + styleId = styleElement.attributes["w:val"]; if (styleId) { - var style = styles.findCharacterStyleById(styleId); + var style = findStyleById(styleId); if (style) { - properties.styleName = style.name; + name = style.name; } else { - messages.push(undefinedStyleWarning("Run", styleId)); + messages.push(undefinedStyleWarning(styleType, styleId)); } } } + return elementResultWithMessages({styleId: styleId, name: name}, messages); } function noteReferenceReader(noteType) { @@ -100,28 +112,18 @@ function BodyReader(options) { var properties = { type: "paragraphProperties" }; - var messages = []; - var styleElement = element.first("w:pStyle"); - if (styleElement) { - var styleId = styleElement.attributes["w:val"]; - properties.styleId = styleId; - if (styleId) { - var style = styles.findParagraphStyleById(styleId); - if (style) { - properties.styleName = style.name; - } else { - messages.push(undefinedStyleWarning("Paragraph", styleId)); - } - } - } var alignElement = element.first("w:jc"); if (alignElement) { properties.alignment = alignElement.attributes["w:val"]; } properties.numbering = readNumberingProperties(element.firstOrEmpty("w:numPr")); - return elementResultWithMessages(properties, messages); + return readParagraphStyle(element).map(function(style) { + properties.styleId = style.styleId; + properties.styleName = style.name; + return properties; + }); }, "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 e36bccc..eaf1704 100644 --- a/tests/docx/body-reader.tests.js +++ b/tests/docx/body-reader.tests.js @@ -14,12 +14,8 @@ var test = testing.test; var createFakeDocxFile = testing.createFakeDocxFile; function readXmlElement(element, options) { - var styles = { - findParagraphStyleById: function(id) { - return {}; - } - }; - options = options || {styles: styles}; + options = Object.create(options || {}); + options.styles = options.styles || new Styles({}, {}); return new BodyReader(options).readXmlElement(element); } @@ -90,10 +86,7 @@ describe("readXmlElement: ", function() { var numbering = new Numbering({"42": {"1": {isOrdered: true, level: "1"}}}); - var reader = new BodyReader({ - numbering: numbering - }); - var paragraph = reader.readXmlElement(paragraphXml).value; + var paragraph = readXmlElementValue(paragraphXml, {numbering: numbering}); assert.deepEqual(paragraph.numbering, {level: "1", isOrdered: true}); }); |
