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>
This commit is contained in:
lin onetwo 2025-11-15 05:33:45 +08:00 committed by GitHub
parent cc348fee96
commit 3e1078eff1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 18 additions and 1 deletions

View file

@ -0,0 +1,12 @@
tags: $:/tags/wikitext-serialize-test-spec
title: Serialize/VoidElements
type: text/vnd.tiddlywiki
This tests void elements like <br/> and <br> (without `/`).
Line one<br>Line two
Line three<br/>Line four
<hr>
Images are also void: <img src="test.png">

View file

@ -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

View file

@ -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., <br> instead of <br/>)
result += "<" + tag + (attributes ? " " + attributes : "") + ">";
} else {
// Opening and closing tags
result += "<" + tag + (attributes ? " " + attributes : "") + ">" + children + "</" + tag + ">";