alle new
This commit is contained in:
erik committed 2026-10-01 06:46:51 +02:00
1 parent da64472251
commit 5b20adee48
1605 files changed
+396517 -1674

No files matched your search

+51
View File
@@ -0,0 +1,51 @@
var htmlPaths = require("../styles/html-paths");
function nonFreshElement(tagName, attributes, children) {
return elementWithTag(
htmlPaths.element(tagName, attributes, {fresh: false}),
children);
}
function freshElement(tagName, attributes, children) {
var tag = htmlPaths.element(tagName, attributes, {fresh: true});
return elementWithTag(tag, children);
}
function elementWithTag(tag, children) {
return {
type: "element",
tag: tag,
children: children || []
};
}
function text(value) {
return {
type: "text",
value: value
};
}
var forceWrite = {
type: "forceWrite"
};
exports.freshElement = freshElement;
exports.nonFreshElement = nonFreshElement;
exports.elementWithTag = elementWithTag;
exports.text = text;
exports.forceWrite = forceWrite;
var voidTagNames = {
"br": true,
"hr": true,
"img": true,
"input": true
};
function isVoidElement(node) {
return (node.children.length === 0) && voidTagNames[node.tag.tagName];
}
exports.isVoidElement = isVoidElement;
+41
View File
@@ -0,0 +1,41 @@
var ast = require("./ast");
exports.freshElement = ast.freshElement;
exports.nonFreshElement = ast.nonFreshElement;
exports.elementWithTag = ast.elementWithTag;
exports.text = ast.text;
exports.forceWrite = ast.forceWrite;
exports.simplify = require("./simplify");
function write(writer, nodes) {
nodes.forEach(function(node) {
writeNode(writer, node);
});
}
function writeNode(writer, node) {
toStrings[node.type](writer, node);
}
var toStrings = {
element: generateElementString,
text: generateTextString,
forceWrite: function() { }
};
function generateElementString(writer, node) {
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) {
writer.text(node.value);
}
exports.write = write;
+88
View File
@@ -0,0 +1,88 @@
var _ = require("underscore");
var ast = require("./ast");
function simplify(nodes) {
return collapse(removeEmpty(nodes));
}
function collapse(nodes) {
var children = [];
nodes.map(collapseNode).forEach(function(child) {
appendChild(children, child);
});
return children;
}
function collapseNode(node) {
return collapsers[node.type](node);
}
var collapsers = {
element: collapseElement,
text: identity,
forceWrite: identity
};
function collapseElement(node) {
return ast.elementWithTag(node.tag, collapse(node.children));
}
function identity(value) {
return value;
}
function appendChild(children, child) {
var lastChild = children[children.length - 1];
if (child.type === "element" && !child.tag.fresh && lastChild && lastChild.type === "element" && child.tag.matchesElement(lastChild.tag)) {
if (child.tag.separator) {
appendChild(lastChild.children, ast.text(child.tag.separator));
}
child.children.forEach(function(grandChild) {
// Mutation is fine since simplifying elements create a copy of the children.
appendChild(lastChild.children, grandChild);
});
} else {
children.push(child);
}
}
function removeEmpty(nodes) {
return flatMap(nodes, function(node) {
return emptiers[node.type](node);
});
}
function flatMap(values, func) {
return _.flatten(_.map(values, func), true);
}
var emptiers = {
element: elementEmptier,
text: textEmptier,
forceWrite: neverEmpty
};
function neverEmpty(node) {
return [node];
}
function elementEmptier(element) {
var children = removeEmpty(element.children);
if (children.length === 0 && !ast.isVoidElement(element)) {
return [];
} else {
return [ast.elementWithTag(element.tag, children)];
}
}
function textEmptier(node) {
if (node.value.length === 0) {
return [];
} else {
return [node];
}
}
module.exports = simplify;