From 3e1078eff1a572f0500e4b510cc08ed4c00db6d5 Mon Sep 17 00:00:00 2001 From: lin onetwo Date: Sat, 15 Nov 2025 05:33:45 +0800 Subject: [PATCH] Fix/serialize close html tag (#9437) * fix: should use tree.isSelfClosing || isVoidElement * docs: about html * fix: Void element without self-closing slash (e.g.,
instead of
) * 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> --- .../tiddlers/tests/data/serialize/VoidElements.tid | 12 ++++++++++++ .../tw5.com/tiddlers/releasenotes/5.4.0/#8258.tid | 1 + plugins/tiddlywiki/wikitext-serialize/rules/html.js | 6 +++++- 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 editions/test/tiddlers/tests/data/serialize/VoidElements.tid diff --git a/editions/test/tiddlers/tests/data/serialize/VoidElements.tid b/editions/test/tiddlers/tests/data/serialize/VoidElements.tid new file mode 100644 index 0000000000..22bd47f4ff --- /dev/null +++ b/editions/test/tiddlers/tests/data/serialize/VoidElements.tid @@ -0,0 +1,12 @@ +tags: $:/tags/wikitext-serialize-test-spec +title: Serialize/VoidElements +type: text/vnd.tiddlywiki + +This tests void elements like
and
(without `/`). + +Line one
Line two +Line three
Line four + +
+ +Images are also void: diff --git a/editions/tw5.com/tiddlers/releasenotes/5.4.0/#8258.tid b/editions/tw5.com/tiddlers/releasenotes/5.4.0/#8258.tid index e6f118ee20..b6de1a50dd 100644 --- a/editions/tw5.com/tiddlers/releasenotes/5.4.0/#8258.tid +++ b/editions/tw5.com/tiddlers/releasenotes/5.4.0/#8258.tid @@ -23,6 +23,7 @@ There is also a utility `serializeAttribute` for a single attribute node, like a * Separate serialize handlers for each WikiText rule as `module-type: wikiruleserializer` * Test suite with tag `$:/tags/wikitext-serialize-test-spec` * It uses each parser's name as rule (`nextMatch.rule.name`), each AST node that needs serialization has a `type` property matching the rule name +** HTML tags and widgets are handled by the `html` serializer !! Example Usage diff --git a/plugins/tiddlywiki/wikitext-serialize/rules/html.js b/plugins/tiddlywiki/wikitext-serialize/rules/html.js index ff9b7be67e..6de28bbd96 100644 --- a/plugins/tiddlywiki/wikitext-serialize/rules/html.js +++ b/plugins/tiddlywiki/wikitext-serialize/rules/html.js @@ -16,9 +16,13 @@ exports.serialize = function(tree,serialize) { // 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 : "") + "/>"; + result += "<" + tag + (attributes ? " " + attributes : "") + "/>"; + } else if(isVoidElement) { + // Void element without self-closing slash (e.g.,
instead of
) + result += "<" + tag + (attributes ? " " + attributes : "") + ">"; } else { // Opening and closing tags result += "<" + tag + (attributes ? " " + attributes : "") + ">" + children + "";