summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2017-11-03 15:54:07 +0000
committerMichael Williamson <mike@zwobble.org>2017-11-03 15:54:07 +0000
commitb6514e40cf8a8a84ca7d341465b3e9fe439be24f (patch)
tree44bfdfd19dec3cddb62ca4760d4d5b11a39cf4d5
parenteaefb859906c56e44c6a0ae4efeb407cf92a8e7d (diff)
Read table styles
-rw-r--r--lib/docx/styles-reader.js11
-rw-r--r--test/docx/styles-reader.tests.js15
2 files changed, 22 insertions, 4 deletions
diff --git a/lib/docx/styles-reader.js b/lib/docx/styles-reader.js
index 4462cf0..7bf5475 100644
--- a/lib/docx/styles-reader.js
+++ b/lib/docx/styles-reader.js
@@ -2,13 +2,16 @@ exports.readStylesXml = readStylesXml;
exports.Styles = Styles;
exports.defaultStyles = new Styles({}, {});
-function Styles(paragraphStyles, characterStyles) {
+function Styles(paragraphStyles, characterStyles, tableStyles) {
return {
findParagraphStyleById: function(styleId) {
return paragraphStyles[styleId];
},
findCharacterStyleById: function(styleId) {
return characterStyles[styleId];
+ },
+ findTableStyleById: function(styleId) {
+ return tableStyles[styleId];
}
};
}
@@ -16,10 +19,12 @@ function Styles(paragraphStyles, characterStyles) {
function readStylesXml(root) {
var paragraphStyles = {};
var characterStyles = {};
+ var tableStyles = {};
var styles = {
"paragraph": paragraphStyles,
- "character": characterStyles
+ "character": characterStyles,
+ "table": tableStyles
};
root.getElementsByTagName("w:style").forEach(function(styleElement) {
@@ -30,7 +35,7 @@ function readStylesXml(root) {
}
});
- return new Styles(paragraphStyles, characterStyles);
+ return new Styles(paragraphStyles, characterStyles, tableStyles);
}
function readStyleElement(styleElement) {
diff --git a/test/docx/styles-reader.tests.js b/test/docx/styles-reader.tests.js
index 4c5da7e..43cba98 100644
--- a/test/docx/styles-reader.tests.js
+++ b/test/docx/styles-reader.tests.js
@@ -21,6 +21,15 @@ test('paragraph style can be found by ID', function() {
assert.equal(styles.findParagraphStyleById("Heading1").styleId, "Heading1");
});
+test('table style can be found by ID', function() {
+ var styles = readStylesXml(
+ new XmlElement("w:styles", {}, [
+ tableStyleElement("TableNormal", "Normal Table")
+ ])
+ );
+ assert.equal(styles.findTableStyleById("TableNormal").styleId, "TableNormal");
+});
+
test('character style can be found by ID', function() {
var styles = readStylesXml(
new XmlElement("w:styles", {}, [
@@ -44,7 +53,7 @@ test('paragraph and character styles are distinct', function() {
test('character and table styles are distinct', function() {
var styles = readStylesXml(
new XmlElement("w:styles", {}, [
- styleElement("table", "Heading1", "Heading 1")
+ tableStyleElement("Heading1", "Heading 1")
])
);
assert.equal(styles.findCharacterStyleById("Heading1"), null);
@@ -78,6 +87,10 @@ function characterStyleElement(id, name) {
return styleElement("character", id, name);
}
+function tableStyleElement(id, name) {
+ return styleElement("table", 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}, [])