aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2017-11-03 16:19:28 +0000
committerMichael Williamson <mike@zwobble.org>2017-11-03 16:19:28 +0000
commitc378280e344dd6209ec41975b4c879d297ad76f9 (patch)
tree3fca2fd67a1554c8446577b1606c4187fb32668d
parentb6514e40cf8a8a84ca7d341465b3e9fe439be24f (diff)
Read table style applied to tables
-rw-r--r--lib/documents.js7
-rw-r--r--lib/docx/body-reader.js20
-rw-r--r--test/docx/body-reader.tests.js18
3 files changed, 42 insertions, 3 deletions
diff --git a/lib/documents.js b/lib/documents.js
index a6d5dd0..100d004 100644
--- a/lib/documents.js
+++ b/lib/documents.js
@@ -148,10 +148,13 @@ function Image(options) {
};
}
-function Table(children) {
+function Table(children, properties) {
+ properties = properties || {};
return {
type: types.table,
- children: children
+ children: children,
+ styleId: properties.styleId || null,
+ styleName: properties.styleName || null
};
}
diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js
index c612e5c..e21b422 100644
--- a/lib/docx/body-reader.js
+++ b/lib/docx/body-reader.js
@@ -81,6 +81,10 @@ function BodyReader(options) {
return readStyle(element, "w:rStyle", "Run", styles.findCharacterStyleById);
}
+ function readTableStyle(element) {
+ return readStyle(element, "w:tblStyle", "Table", styles.findTableStyleById);
+ }
+
function readStyle(element, styleTagName, styleType, findStyleById) {
var messages = [];
var styleElement = element.first(styleTagName);
@@ -293,9 +297,23 @@ function BodyReader(options) {
function readTable(element) {
+ var propertiesResult = readTableProperties(element.firstOrEmpty("w:tblPr"));
return readXmlElements(element.children)
.flatMap(calculateRowSpans)
- .map(documents.Table);
+ .flatMap(function(children) {
+ return propertiesResult.map(function(properties) {
+ return documents.Table(children, properties);
+ });
+ });
+ }
+
+ function readTableProperties(element) {
+ return readTableStyle(element).map(function(style) {
+ return {
+ styleId: style.styleId,
+ styleName: style.name
+ };
+ });
}
function readTableRow(element) {
diff --git a/test/docx/body-reader.tests.js b/test/docx/body-reader.tests.js
index f84f0e8..eb01cb4 100644
--- a/test/docx/body-reader.tests.js
+++ b/test/docx/body-reader.tests.js
@@ -549,6 +549,24 @@ test("w:tbl is read as document table element", function() {
]));
});
+test("table has no style if it has no properties", function() {
+ var tableXml = new XmlElement("w:tbl", {}, []);
+ var table = readXmlElementValue(tableXml);
+ assert.deepEqual(table.styleId, null);
+});
+
+test("table has style ID and name read from paragraph properties if present", function() {
+ var styleXml = new XmlElement("w:tblStyle", {"w:val": "TableNormal"}, []);
+ var propertiesXml = new XmlElement("w:tblPr", {}, [styleXml]);
+ var tableXml = new XmlElement("w:tbl", {}, [propertiesXml]);
+
+ var styles = new Styles({}, {}, {"TableNormal": {name: "Normal Table"}});
+
+ var table = readXmlElementValue(tableXml, {styles: styles});
+ assert.deepEqual(table.styleId, "TableNormal");
+ assert.deepEqual(table.styleName, "Normal Table");
+});
+
test("w:tblHeader marks table row as header", function() {
var tableXml = new XmlElement("w:tbl", {}, [
new XmlElement("w:tr", {}, [