diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-05-02 11:32:44 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-05-02 11:32:44 +0100 |
| commit | 1b54156c52caf3f2e9fe7eef29ba5ce6bd098ded (patch) | |
| tree | 27ef84d722fd7d0529b79c3639c5498ea98e6a1c | |
| parent | 7348d90d8eb3120e901c0c7adf284c22e44ac5d0 (diff) | |
Read colSpan for table cells
| -rw-r--r-- | lib/documents.js | 6 | ||||
| -rw-r--r-- | lib/docx/body-reader.js | 9 | ||||
| -rw-r--r-- | 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); |
