diff options
| author | Michael Williamson <mike@zwobble.org> | 2017-11-04 10:54:34 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2017-11-04 10:54:34 +0000 |
| commit | eae1dd0b509fbe201ba5f7ed530fd1bf8329904a (patch) | |
| tree | 02e69155d70c9f11f332dfcbffec9d9e6a6bb36e | |
| parent | 0748bff0572c57ce8a62a92f19071c16a0a76c02 (diff) | |
Use table style mappings during document conversion
| -rw-r--r-- | lib/document-to-html.js | 14 | ||||
| -rw-r--r-- | lib/styles/document-matchers.js | 5 | ||||
| -rw-r--r-- | test/document-to-html.tests.js | 17 |
3 files changed, 33 insertions, 3 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js index 0dac05e..8d1078b 100644 --- a/lib/document-to-html.js +++ b/lib/document-to-html.js @@ -219,7 +219,17 @@ function DocumentConversion(options, comments) { return idPrefix + suffix; } + var defaultTablePath = htmlPaths.elements([ + htmlPaths.element("table", {}, {fresh: true}) + ]); + function convertTable(element, messages, options) { + return findHtmlPath(element, defaultTablePath).wrap(function() { + return convertTableChildren(element, messages, options); + }); + } + + function convertTableChildren(element, messages, options) { var bodyIndex = _.findIndex(element.children, function(child) { return !child.type === documents.types.tableRow || !child.isHeader; }); @@ -249,9 +259,7 @@ function DocumentConversion(options, comments) { Html.freshElement("tbody", {}, bodyRows) ]; } - return [ - Html.freshElement("table", {}, [Html.forceWrite].concat(children)) - ]; + return [Html.forceWrite].concat(children); } function convertTableRow(element, messages, options) { diff --git a/lib/styles/document-matchers.js b/lib/styles/document-matchers.js index e3cae46..11b577f 100644 --- a/lib/styles/document-matchers.js +++ b/lib/styles/document-matchers.js @@ -1,5 +1,6 @@ exports.paragraph = paragraph; exports.run = run; +exports.table = table; exports.bold = new Matcher("bold"); exports.italic = new Matcher("italic"); exports.underline = new Matcher("underline"); @@ -21,6 +22,10 @@ function run(options) { return new Matcher("run", options); } +function table(options) { + return new Matcher("table", options); +} + function Matcher(elementType, options) { options = options || {}; this._elementType = elementType; diff --git a/test/document-to-html.tests.js b/test/document-to-html.tests.js index 861033b..8e6546b 100644 --- a/test/document-to-html.tests.js +++ b/test/document-to-html.tests.js @@ -419,6 +419,23 @@ test('docx table is converted to table in HTML', function() { }); }); +test('table style mappings can be used to map tables', function() { + var table = new documents.Table([], {styleName: "Normal Table"}); + var converter = new DocumentConverter({ + styleMap: [ + { + from: documentMatchers.table({styleName: documentMatchers.equalTo("Normal Table")}), + to: htmlPaths.topLevelElement("table", {"class": "normal-table"}) + } + ] + }); + + return converter.convertToHtml(table).then(function(result) { + var expectedHtml = '<table class="normal-table"></table>'; + assert.equal(result.value, expectedHtml); + }); +}); + test('header rows are wrapped in thead', function() { var table = new documents.Table([ new documents.TableRow([new documents.TableCell([])], {isHeader: true}), |
