diff options
| author | Michael Williamson <mike@zwobble.org> | 2014-04-22 11:59:49 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2014-04-22 11:59:49 +0100 |
| commit | 0326c56e24f7333381c7ef8c17aba0ac8380c02b (patch) | |
| tree | 2e687550e65551245c900b1d332fe5c5175ae8a5 | |
| parent | 354114f396bd864b199c3f46852acc1beb3c5672 (diff) | |
Add styleName property to Paragraph
| -rw-r--r-- | lib/documents.js | 1 | ||||
| -rw-r--r-- | lib/docx/document-xml-reader.js | 8 | ||||
| -rw-r--r-- | lib/docx/styles-reader.js | 2 | ||||
| -rw-r--r-- | tests/docx/document-xml-reader.tests.js | 23 |
4 files changed, 28 insertions, 6 deletions
diff --git a/lib/documents.js b/lib/documents.js index 8463da0..4ccd93a 100644 --- a/lib/documents.js +++ b/lib/documents.js @@ -21,6 +21,7 @@ function Paragraph(children, properties) { type: types.paragraph, children: children, styleId: properties.styleId || null, + styleName: properties.styleName || null, numbering: properties.numbering || null, alignment: properties.alignment || null }; diff --git a/lib/docx/document-xml-reader.js b/lib/docx/document-xml-reader.js index fc4b630..da95a36 100644 --- a/lib/docx/document-xml-reader.js +++ b/lib/docx/document-xml-reader.js @@ -13,6 +13,7 @@ function DocumentXmlReader(options) { var contentTypes = options.contentTypes; var docxFile = options.docxFile; var numbering = options.numbering; + var styles = options.styles; function convertXmlToDocument(documentXml) { @@ -68,7 +69,12 @@ function DocumentXmlReader(options) { var styleElement = element.first("w:pStyle"); if (styleElement) { - properties.styleId = styleElement.attributes["w:val"]; + var styleId = styleElement.attributes["w:val"]; + properties.styleId = styleId; + if (styleId) { + var style = styles.findParagraphStyleById(styleId); + properties.styleName = style.name; + } } var alignElement = element.first("w:jc"); if (alignElement) { diff --git a/lib/docx/styles-reader.js b/lib/docx/styles-reader.js index 75776fc..d38c510 100644 --- a/lib/docx/styles-reader.js +++ b/lib/docx/styles-reader.js @@ -1,5 +1,5 @@ exports.readStylesXml = readStylesXml; - +exports.Styles = Styles; exports.defaultStyles = new Styles({}, {}); function Styles(paragraphStyles, characterStyles) { diff --git a/tests/docx/document-xml-reader.tests.js b/tests/docx/document-xml-reader.tests.js index 9a96d1e..8e28081 100644 --- a/tests/docx/document-xml-reader.tests.js +++ b/tests/docx/document-xml-reader.tests.js @@ -4,6 +4,7 @@ var DocumentXmlReader = require("../../lib/docx/document-xml-reader").DocumentXm var documents = require("../../lib/documents"); var XmlElement = require("../../lib/xmlreader").Element; var Numbering = require("../../lib/docx/numbering-xml").Numbering; +var Styles = require("../../lib/docx/styles-reader").Styles; var testing = require("../testing"); var test = testing.test; @@ -14,8 +15,8 @@ function readXmlElement(element, options) { return new DocumentXmlReader(options).readXmlElement(element); } -function readXmlElementValue(element) { - var result = readXmlElement(element); +function readXmlElementValue(element, options) { + var result = readXmlElement(element, options); assert.deepEqual(result.messages, []); return result.value; } @@ -33,14 +34,28 @@ describe("readXmlElement: ", function() { assert.deepEqual(paragraph.styleId, null); }); - test("paragraph has style name read from paragraph properties if present", function() { + test("paragraph has style ID 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]); - var paragraph = readXmlElementValue(paragraphXml); + + var styles = new Styles({"Heading1": {name: "Heading 1"}}, {}); + + var paragraph = readXmlElementValue(paragraphXml, {styles: styles}); assert.deepEqual(paragraph.styleId, "Heading1"); }); + test("paragraph has style name read from paragraph properties and styles", 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 paragraph = readXmlElementValue(paragraphXml, {styles: styles}); + assert.deepEqual(paragraph.styleName, "Heading 1"); + }); + test("paragraph has justification read from paragraph properties if present", function() { var justificationXml = new XmlElement("w:jc", {"w:val": "center"}, []); var propertiesXml = new XmlElement("w:pPr", {}, [justificationXml]); |
