diff options
| -rw-r--r-- | lib/document-to-html.js | 25 | ||||
| -rw-r--r-- | lib/html/ast.js | 28 | ||||
| -rw-r--r-- | lib/styles/document-matchers.js | 6 | ||||
| -rw-r--r-- | test/document-to-html.tests.js | 15 |
4 files changed, 52 insertions, 22 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js index a51afd0..3a30ed2 100644 --- a/lib/document-to-html.js +++ b/lib/document-to-html.js @@ -283,6 +283,23 @@ function DocumentConversion(options, comments) { ]; } + function convertBreak(element, messages) { + return htmlPathForBreak(element).wrap(function() { + return []; + }); + } + + function htmlPathForBreak(element) { + var style = findStyle(element); + if (style) { + return style.to; + } else if (element.breakType === "line") { + return htmlPaths.topLevelElement("br"); + } else { + return htmlPaths.empty; + } + } + function wrapChildrenInFreshElement(element, wrapElementName, messages) { var children = convertElements(element.children, messages); return [ @@ -349,13 +366,7 @@ function DocumentConversion(options, comments) { "table": convertTable, "tableRow": convertTableRow, "tableCell": convertTableCell, - "break": function(element, messages) { - if (element.breakType === "line") { - return [Html.freshElement("br")]; - } else { - return []; - } - } + "break": convertBreak }; return { convertToHtml: convertToHtml diff --git a/lib/html/ast.js b/lib/html/ast.js index c949550..34b9a3d 100644 --- a/lib/html/ast.js +++ b/lib/html/ast.js @@ -8,12 +8,8 @@ function nonFreshElement(tagName, attributes, children) { } function freshElement(tagName, attributes, children) { - if (isVoidElement(tagName, children)) { - return selfClosingElement(tagName, attributes); - } else { - var tag = htmlPaths.element(tagName, attributes, {fresh: true}); - return elementWithTag(tag, children); - } + var tag = htmlPaths.element(tagName, attributes, {fresh: true}); + return elementWithTag(tag, children); } var voidTagNames = { @@ -22,16 +18,20 @@ var voidTagNames = { "img": true }; -function isVoidElement(tagName, children) { - return (!children || children.length === 0) && voidTagNames[tagName]; +function elementWithTag(tag, children) { + if (isVoidElement(tag.tagName, children)) { + return selfClosingElement(tag.tagName, tag.attributes); + } else { + return { + type: "element", + tag: tag, + children: children || [] + }; + } } -function elementWithTag(tag, children) { - return { - type: "element", - tag: tag, - children: children || [] - }; +function isVoidElement(tagName, children) { + return (!children || children.length === 0) && voidTagNames[tagName]; } function selfClosingElement(tagName, attributes) { diff --git a/lib/styles/document-matchers.js b/lib/styles/document-matchers.js index 3f7fbdd..afc0a7d 100644 --- a/lib/styles/document-matchers.js +++ b/lib/styles/document-matchers.js @@ -5,6 +5,9 @@ exports.italic = new Matcher("italic"); exports.underline = new Matcher("underline"); exports.strikethrough = new Matcher("strikethrough"); exports.commentReference = new Matcher("commentReference"); +exports.lineBreak = new Matcher("break", {breakType: "line"}); +exports.pageBreak = new Matcher("break", {breakType: "page"}); +exports.columnBreak = new Matcher("break", {breakType: "column"}); exports.equalTo = equalTo; exports.startsWith = startsWith; @@ -32,7 +35,8 @@ Matcher.prototype.matches = function(element) { return element.type === this._elementType && (this._styleId === undefined || element.styleId === this._styleId) && (this._styleName === undefined || (element.styleName && this._styleName.operator(this._styleName.operand, element.styleName))) && - (this._listIndex === undefined || isList(element, this._listIndex, this._listIsOrdered)); + (this._listIndex === undefined || isList(element, this._listIndex, this._listIsOrdered)) && + (this._breakType === undefined || this._breakType === element.breakType); }; function isList(element, levelIndex, isOrdered) { diff --git a/test/document-to-html.tests.js b/test/document-to-html.tests.js index d21badb..8124ad1 100644 --- a/test/document-to-html.tests.js +++ b/test/document-to-html.tests.js @@ -450,6 +450,21 @@ test('breaks that are not line breaks are ignored', function() { }); }); +test('breaks can be mapped using style mappings', function() { + var converter = new DocumentConverter({ + styleMap: [ + { + from: documentMatchers.pageBreak, + to: htmlPaths.topLevelElement("hr") + } + ] + }); + + return converter.convertToHtml(documents.pageBreak).then(function(result) { + assert.equal(result.value, "<hr />"); + }); +}); + test('footnote reference is converted to superscript intra-page link', function() { var footnoteReference = new documents.NoteReference({ noteType: "footnote", |
