From 1b54156c52caf3f2e9fe7eef29ba5ce6bd098ded Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 2 May 2016 11:32:44 +0100 Subject: Read colSpan for table cells --- lib/documents.js | 6 ++++-- lib/docx/body-reader.js | 9 ++++++++- tests/docx/body-reader.tests.js | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/lib/documents.js b/lib/documents.js index 4e4211b..8b979c2 100644 --- a/lib/documents.js +++ b/lib/documents.js @@ -139,10 +139,12 @@ function TableRow(children) { }; } -function TableCell(children) { +function TableCell(children, options) { + options = options || {}; return { type: types.tableCell, - children: children + children: children, + colSpan: options.colSpan == null ? 1 : options.colSpan }; } diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js index 4e7ff03..126896c 100644 --- a/lib/docx/body-reader.js +++ b/lib/docx/body-reader.js @@ -164,7 +164,14 @@ function BodyReader(options) { return readXmlElements(element.children).map(documents.TableRow); }, "w:tc": function(element) { - return readXmlElements(element.children).map(documents.TableCell); + return readXmlElements(element.children).map(function(children) { + var gridSpan = element + .firstOrEmpty("w:tcPr") + .firstOrEmpty("w:gridSpan") + .attributes["w:val"]; + var colSpan = gridSpan ? parseInt(gridSpan) : 1; + return documents.TableCell(children, {colSpan: colSpan}); + }); }, "w:footnoteReference": noteReferenceReader("footnote"), "w:endnoteReference": noteReferenceReader("endnote"), diff --git a/tests/docx/body-reader.tests.js b/tests/docx/body-reader.tests.js index 779397c..546a21d 100644 --- a/tests/docx/body-reader.tests.js +++ b/tests/docx/body-reader.tests.js @@ -261,6 +261,45 @@ describe("readXmlElement: ", function() { ])); }); + test("w:tbl is read as document table element", function() { + var tableXml = new XmlElement("w:tbl", {}, [ + new XmlElement("w:tr", {}, [ + new XmlElement("w:tc", {}, [ + new XmlElement("w:p", {}, []) + ]) + ]) + ]); + var result = readXmlElement(tableXml); + assert.deepEqual(result.value, new documents.Table([ + new documents.TableRow([ + new documents.TableCell([ + new documents.Paragraph([]) + ]) + ]) + ])); + }); + + test("w:gridSpan is read as colSpan for table cell", function() { + var tableXml = new XmlElement("w:tbl", {}, [ + new XmlElement("w:tr", {}, [ + new XmlElement("w:tc", {}, [ + new XmlElement("w:tcPr", {}, [ + new XmlElement("w:gridSpan", {"w:val": "2"}) + ]), + new XmlElement("w:p", {}, []) + ]) + ]) + ]); + var result = readXmlElement(tableXml); + assert.deepEqual(result.value, new documents.Table([ + new documents.TableRow([ + new documents.TableCell([ + new documents.Paragraph([]) + ], {colSpan: 2}) + ]) + ])); + }); + test("emits warning on unrecognised element", function() { var unrecognisedElement = new XmlElement("w:not-an-element"); var result = readXmlElement(unrecognisedElement); -- cgit v1.2.3