Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Recognize all HTML void elements in database templates https://github.com/siyuan-note/siyuan/issues/20197

Daniel4f141d874aa1
diff --git a/kernel/util/misc.go b/kernel/util/misc.go
index 55493de6de..57fda68fc3 100644
--- a/kernel/util/misc.go
+++ b/kernel/util/misc.go
@@ -157,7 +157,8 @@ func HasUnclosedHtmlTag(htmlStr string) bool {
tagRe := regexp.MustCompile(`<(/?)([a-zA-Z0-9]+)[^>]*?>`)
selfClosing := map[string]bool{
- "br": true, "img": true, "hr": true, "input": true, "meta": true, "link": true,
+ "area": true, "base": true, "br": true, "col": true, "embed": true, "hr": true, "img": true,
+ "input": true, "link": true, "meta": true, "param": true, "source": true, "track": true, "wbr": true,
}
stack := []string{}
matches := tagRe.FindAllStringSubmatch(htmlStr, -1)
diff --git a/kernel/util/misc_html_test.go b/kernel/util/misc_html_test.go
new file mode 100644
index 0000000000..6dac56c8d3
--- /dev/null
+++ b/kernel/util/misc_html_test.go
@@ -0,0 +1,28 @@
+package util
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestHasUnclosedHTMLVoidElements(t *testing.T) {
+ for _, tag := range []string{"area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr"} {
+ t.Run(tag, func(t *testing.T) {
+ for _, value := range []string{"<" + tag + ">", "<" + tag + ` data-value="x">`, "<" + strings.ToUpper(tag) + ">", "<div><" + tag + "></div>"} {
+ if HasUnclosedHtmlTag(value) {
+ t.Errorf("void element was treated as unclosed: %s", value)
+ }
+ }
+ })
+ }
+ for _, value := range []string{`<table><colgroup><col span="2"></colgroup><tr><td>text</td></tr></table>`, `<video><source src="video.mp4"><track src="captions.vtt"></video>`, "<div><!-- <col> --><wbr></div>"} {
+ if HasUnclosedHtmlTag(value) {
+ t.Errorf("balanced markup was treated as unclosed: %s", value)
+ }
+ }
+ for _, value := range []string{"<div><wbr>", "<div><span></div>", "<div><!-- unclosed"} {
+ if !HasUnclosedHtmlTag(value) {
+ t.Errorf("unclosed markup was accepted: %s", value)
+ }
+ }
+}