summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-11-28 17:48:31 +0000
committerMichael Williamson <mike@zwobble.org>2016-11-28 17:48:31 +0000
commit611e0c9af7c1948ad46c5471a426ec751e73a1ea (patch)
tree28954bd7f66bd306345f36eebca20df1bd1b07ee
parent5428d4daeebd8d1fa8a67b20e2f8a238d3b0abbf (diff)
Call freshElement to create self-closing elements
-rw-r--r--lib/document-to-html.js2
-rw-r--r--lib/html/ast.js20
-rw-r--r--lib/html/index.js1
-rw-r--r--lib/images.js2
-rw-r--r--test/document-to-html.tests.js2
5 files changed, 19 insertions, 8 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js
index c8a9900..d223d82 100644
--- a/lib/document-to-html.js
+++ b/lib/document-to-html.js
@@ -350,7 +350,7 @@ function DocumentConversion(options, comments) {
"tableRow": convertTableRow,
"tableCell": convertTableCell,
"lineBreak": function(element, messages) {
- return [Html.selfClosingElement("br")];
+ return [Html.freshElement("br")];
}
};
return {
diff --git a/lib/html/ast.js b/lib/html/ast.js
index 0dd8120..c949550 100644
--- a/lib/html/ast.js
+++ b/lib/html/ast.js
@@ -8,9 +8,22 @@ function nonFreshElement(tagName, attributes, children) {
}
function freshElement(tagName, attributes, children) {
- return elementWithTag(
- htmlPaths.element(tagName, attributes, {fresh: true}),
- children);
+ if (isVoidElement(tagName, children)) {
+ return selfClosingElement(tagName, attributes);
+ } else {
+ var tag = htmlPaths.element(tagName, attributes, {fresh: true});
+ return elementWithTag(tag, children);
+ }
+}
+
+var voidTagNames = {
+ "br": true,
+ "hr": true,
+ "img": true
+};
+
+function isVoidElement(tagName, children) {
+ return (!children || children.length === 0) && voidTagNames[tagName];
}
function elementWithTag(tag, children) {
@@ -43,6 +56,5 @@ var forceWrite = {
exports.freshElement = freshElement;
exports.nonFreshElement = nonFreshElement;
exports.elementWithTag = elementWithTag;
-exports.selfClosingElement = selfClosingElement;
exports.text = text;
exports.forceWrite = forceWrite;
diff --git a/lib/html/index.js b/lib/html/index.js
index 600d783..3b6a190 100644
--- a/lib/html/index.js
+++ b/lib/html/index.js
@@ -3,7 +3,6 @@ var ast = require("./ast");
exports.freshElement = ast.freshElement;
exports.nonFreshElement = ast.nonFreshElement;
exports.elementWithTag = ast.elementWithTag;
-exports.selfClosingElement = ast.selfClosingElement;
exports.text = ast.text;
exports.forceWrite = ast.forceWrite;
diff --git a/lib/images.js b/lib/images.js
index fdc3601..fef0ca3 100644
--- a/lib/images.js
+++ b/lib/images.js
@@ -10,7 +10,7 @@ exports.imgElement = function(func) {
if (element.altText) {
attributes.alt = element.altText;
}
- return [Html.selfClosingElement("img", attributes)];
+ return [Html.freshElement("img", attributes)];
});
};
};
diff --git a/test/document-to-html.tests.js b/test/document-to-html.tests.js
index 2543779..c739900 100644
--- a/test/document-to-html.tests.js
+++ b/test/document-to-html.tests.js
@@ -581,7 +581,7 @@ test('can add custom handler for images', function() {
var converter = new DocumentConverter({
convertImage: function(element, messages) {
return element.read("utf8").then(function(altText) {
- return [Html.selfClosingElement("img", {alt: altText})];
+ return [Html.freshElement("img", {alt: altText})];
});
}
});