aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2014-04-22 11:33:59 +0100
committerMichael Williamson <mike@zwobble.org>2014-04-22 11:33:59 +0100
commit15bf7ff1863049a7cad09719fa90b0e416b7715b (patch)
treed4281becce7ef2e7f62a7dc93717f306bc312909
parent7cad7ca1dcdf48ac826c54e33629bb961c7380dd (diff)
Read character styles from styles.xml
-rw-r--r--lib/docx/styles-reader.js16
-rw-r--r--tests/docx/styles-reader.tests.js38
2 files changed, 46 insertions, 8 deletions
diff --git a/lib/docx/styles-reader.js b/lib/docx/styles-reader.js
index d896d3d..fd3425c 100644
--- a/lib/docx/styles-reader.js
+++ b/lib/docx/styles-reader.js
@@ -1,25 +1,31 @@
exports.readStylesXml = readStylesXml;
-function Styles(paragraphStyles) {
+function Styles(paragraphStyles, characterStyles) {
return {
findParagraphStyleById: function(styleId) {
return paragraphStyles[styleId];
+ },
+ findCharacterStyleById: function(styleId) {
+ return characterStyles[styleId];
}
};
}
function readStylesXml(xml) {
- var paragraphStyles = [];
+ var paragraphStyles = {};
+ var characterStyles = {};
xml.root.getElementsByTagName("w:style").forEach(function(styleElement) {
var style = readStyleElement(styleElement);
- paragraphStyles[style.styleId] = style;
+ var styleSet = style.type === "paragraph" ? paragraphStyles : characterStyles;
+ styleSet[style.styleId] = style;
});
- return new Styles(paragraphStyles);
+ return new Styles(paragraphStyles, characterStyles);
}
function readStyleElement(styleElement) {
+ var type = styleElement.attributes["w:type"];
var styleId = styleElement.attributes["w:styleId"];
- return {styleId: styleId};
+ return {type: type, styleId: styleId};
}
diff --git a/tests/docx/styles-reader.tests.js b/tests/docx/styles-reader.tests.js
index dd82602..274f822 100644
--- a/tests/docx/styles-reader.tests.js
+++ b/tests/docx/styles-reader.tests.js
@@ -18,11 +18,43 @@ describe('readStylesXml', function() {
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"}, [])
- ])
+ paragraphStyleElement("Heading1", "Heading 1")
])
});
assert.equal(styles.findParagraphStyleById("Heading1").styleId, "Heading1");
});
+
+ test('character style can be found by ID', function() {
+ var styles = readStylesXml({
+ root: new XmlElement("w:styles", {}, [
+ characterStyleElement("Heading1Char", "Heading 1 Char")
+ ])
+ });
+ assert.equal(styles.findCharacterStyleById("Heading1Char").styleId, "Heading1Char");
+ });
+
+ test('paragraph and character styles are distinct', function() {
+ var styles = readStylesXml({
+ root: new XmlElement("w:styles", {}, [
+ paragraphStyleElement("Heading1", "Heading 1"),
+ characterStyleElement("Heading1Char", "Heading 1 Char")
+ ])
+ });
+ assert.equal(styles.findCharacterStyleById("Heading1"), null);
+ assert.equal(styles.findParagraphStyleById("Heading1Char"), null);
+ });
});
+
+function paragraphStyleElement(id, name) {
+ return styleElement("paragraph", id, name);
+}
+
+function characterStyleElement(id, name) {
+ return styleElement("character", id, name);
+}
+
+function styleElement(type, id, name) {
+ return new XmlElement("w:style", {"w:type": type, "w:styleId": id}, [
+ new XmlElement("w:name", {"w:val": name}, [])
+ ]);
+}