aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2014-04-22 11:25:58 +0100
committerMichael Williamson <mike@zwobble.org>2014-04-22 11:25:58 +0100
commit7cad7ca1dcdf48ac826c54e33629bb961c7380dd (patch)
treed0b4005d2f05f7fe74d3cc9abd380100de615a6c
parentea1c98fb4fc0aee93c194e6a90cf9b71326705af (diff)
Read styles from styles.xml
-rw-r--r--lib/docx/styles-reader.js25
-rw-r--r--tests/docx/styles-reader.tests.js28
2 files changed, 53 insertions, 0 deletions
diff --git a/lib/docx/styles-reader.js b/lib/docx/styles-reader.js
new file mode 100644
index 0000000..d896d3d
--- /dev/null
+++ b/lib/docx/styles-reader.js
@@ -0,0 +1,25 @@
+exports.readStylesXml = readStylesXml;
+
+function Styles(paragraphStyles) {
+ return {
+ findParagraphStyleById: function(styleId) {
+ return paragraphStyles[styleId];
+ }
+ };
+}
+
+function readStylesXml(xml) {
+ var paragraphStyles = [];
+
+ xml.root.getElementsByTagName("w:style").forEach(function(styleElement) {
+ var style = readStyleElement(styleElement);
+ paragraphStyles[style.styleId] = style;
+ });
+
+ return new Styles(paragraphStyles);
+}
+
+function readStyleElement(styleElement) {
+ var styleId = styleElement.attributes["w:styleId"];
+ return {styleId: styleId};
+}
diff --git a/tests/docx/styles-reader.tests.js b/tests/docx/styles-reader.tests.js
new file mode 100644
index 0000000..dd82602
--- /dev/null
+++ b/tests/docx/styles-reader.tests.js
@@ -0,0 +1,28 @@
+var assert = require("assert");
+
+var duck = require("duck");
+
+var readStylesXml = require("../../lib/docx/styles-reader").readStylesXml;
+var XmlElement = require("../../lib/xmlreader").Element;
+var test = require("../testing").test;
+
+
+describe('readStylesXml', function() {
+ test('paragraph style is null if no style with that ID exists', function() {
+ var styles = readStylesXml({
+ root: new XmlElement("w:styles", {}, [])
+ });
+ assert.equal(styles.findParagraphStyleById("Heading1"), null);
+ });
+
+ test('paragraph style can be found by ID', function() {
+ var styles = readStylesXml({
+ root: new XmlElement("w:styles", {}, [
+ new XmlElement("w:style", {"w:styleId": "Heading1"}, [
+ new XmlElement("w:name", {"w:val": "Heading 1"}, [])
+ ])
+ ])
+ });
+ assert.equal(styles.findParagraphStyleById("Heading1").styleId, "Heading1");
+ });
+});