TiddlyWiki5/plugins/tiddlywiki/wikitext-serialize/rules/html.js
lin onetwo 3e1078eff1
Fix/serialize close html tag (#9437)
* fix: should use tree.isSelfClosing || isVoidElement

* docs: about html

* fix: Void element without self-closing slash (e.g., <br> instead of <br/>)

* Update editions/test/tiddlers/tests/data/serialize/VoidElements.tid

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-14 21:33:45 +00:00

34 lines
1 KiB
JavaScript

/*\
title: $:/plugins/tiddlywiki/wikitext-serialize/rules/html.js
type: application/javascript
module-type: wikiruleserializer
\*/
"use strict";
exports.name = "html";
exports.serialize = function(tree,serialize) {
var tag = tree.tag;
var attributes = tree.orderedAttributes.map(function(attribute) {
return $tw.utils.serializeAttribute(attribute);
}).join(" ");
// Children
var children = tree.children ? serialize(tree.children) : "";
var result = "";
var isVoidElement = $tw.config.htmlVoidElements.indexOf(tag) !== -1;
// Self-closing tag
if(tree.isSelfClosing) {
result += "<" + tag + (attributes ? " " + attributes : "") + "/>";
} else if(isVoidElement) {
// Void element without self-closing slash (e.g., <br> instead of <br/>)
result += "<" + tag + (attributes ? " " + attributes : "") + ">";
} else {
// Opening and closing tags
result += "<" + tag + (attributes ? " " + attributes : "") + ">" + children + "</" + tag + ">";
}
if(tree.isBlock) {
result += "\n\n";
}
return result;
};