aboutsummaryrefslogtreecommitdiff
path: root/lib/html
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 /lib/html
parent5428d4daeebd8d1fa8a67b20e2f8a238d3b0abbf (diff)
Call freshElement to create self-closing elements
Diffstat (limited to 'lib/html')
-rw-r--r--lib/html/ast.js20
-rw-r--r--lib/html/index.js1
2 files changed, 16 insertions, 5 deletions
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;