aboutsummaryrefslogtreecommitdiff
path: root/lib/html
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-12-10 21:53:24 +0000
committerMichael Williamson <mike@zwobble.org>2016-12-10 21:53:24 +0000
commit0ad3a12213086e433c617fb951edaada6e7500f5 (patch)
tree4770aad36468db9a6c492d9614165739769915d5 /lib/html
parent72ad04159683e8958ff957695fc9bb224fc376ca (diff)
Remove self-closing tags from AST
Diffstat (limited to 'lib/html')
-rw-r--r--lib/html/ast.js40
-rw-r--r--lib/html/index.js15
-rw-r--r--lib/html/simplify.js6
3 files changed, 24 insertions, 37 deletions
diff --git a/lib/html/ast.js b/lib/html/ast.js
index 34b9a3d..4b9290d 100644
--- a/lib/html/ast.js
+++ b/lib/html/ast.js
@@ -12,33 +12,11 @@ function freshElement(tagName, attributes, children) {
return elementWithTag(tag, children);
}
-var voidTagNames = {
- "br": true,
- "hr": true,
- "img": true
-};
-
function elementWithTag(tag, children) {
- if (isVoidElement(tag.tagName, children)) {
- return selfClosingElement(tag.tagName, tag.attributes);
- } else {
- return {
- type: "element",
- tag: tag,
- children: children || []
- };
- }
-}
-
-function isVoidElement(tagName, children) {
- return (!children || children.length === 0) && voidTagNames[tagName];
-}
-
-function selfClosingElement(tagName, attributes) {
return {
- type: "selfClosingElement",
- tagName: tagName,
- attributes: attributes
+ type: "element",
+ tag: tag,
+ children: children || []
};
}
@@ -58,3 +36,15 @@ exports.nonFreshElement = nonFreshElement;
exports.elementWithTag = elementWithTag;
exports.text = text;
exports.forceWrite = forceWrite;
+
+var voidTagNames = {
+ "br": true,
+ "hr": true,
+ "img": true
+};
+
+function isVoidElement(node) {
+ return (node.children.length === 0) && voidTagNames[node.tag.tagName];
+}
+
+exports.isVoidElement = isVoidElement;
diff --git a/lib/html/index.js b/lib/html/index.js
index 3b6a190..810ab66 100644
--- a/lib/html/index.js
+++ b/lib/html/index.js
@@ -20,19 +20,18 @@ function writeNode(writer, node) {
var toStrings = {
element: generateElementString,
- selfClosingElement: generateSelfClosingElementString,
text: generateTextString,
forceWrite: function() { }
};
function generateElementString(writer, node) {
- writer.open(node.tag.tagName, node.tag.attributes);
- write(writer, node.children);
- writer.close(node.tag.tagName);
-}
-
-function generateSelfClosingElementString(writer, node) {
- writer.selfClosing(node.tagName, node.attributes);
+ if (ast.isVoidElement(node)) {
+ writer.selfClosing(node.tag.tagName, node.tag.attributes);
+ } else {
+ writer.open(node.tag.tagName, node.tag.attributes);
+ write(writer, node.children);
+ writer.close(node.tag.tagName);
+ }
}
function generateTextString(writer, node) {
diff --git a/lib/html/simplify.js b/lib/html/simplify.js
index e05bde2..8ba37eb 100644
--- a/lib/html/simplify.js
+++ b/lib/html/simplify.js
@@ -21,7 +21,6 @@ function collapseNode(node) {
var collapsers = {
element: collapseElement,
- selfClosingElement: identity,
text: identity,
forceWrite: identity
};
@@ -61,7 +60,6 @@ function flatMap(values, func) {
var emptiers = {
element: elementEmptier,
- selfClosingElement: neverEmpty,
text: textEmptier,
forceWrite: neverEmpty
};
@@ -72,10 +70,10 @@ function neverEmpty(node) {
function elementEmptier(element) {
var children = removeEmpty(element.children);
- if (children.length === 0) {
+ if (children.length === 0 && !ast.isVoidElement(element)) {
return [];
} else {
- return ast.elementWithTag(element.tag, children);
+ return [ast.elementWithTag(element.tag, children)];
}
}