An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Normalize pasted web semantic element styles https://github.com/siyuan-note/siyuan/issues/19155
Daniel447feef0b90a
diff --git a/kernel/api/lute_clipboard_style.go b/kernel/api/lute_clipboard_style.goindex 31b497961d..e7b4cdc53f 100644--- a/kernel/api/lute_clipboard_style.go+++ b/kernel/api/lute_clipboard_style.go@@ -81,6 +81,24 @@ func normalizeBrowserClipboardStyle(dom string, documentSource bool) string {changed = changed || n.Attr[i].Val != style}})+ // 语义元素由编辑器提供基础外观,内部独立设置的强调样式继续保留。+ doc.Find("h1,h2,h3,h4,h5,h6,pre,code,blockquote,ul,ol,li,table,thead,tbody,tfoot,tr,th,td").Each(func(_ int, selection *goquery.Selection) {+ n := selection.Get(0)+ remove := map[string]bool{"font-family": true, "font-size": true}+ switch n.Data {+ case "table", "thead", "tbody", "tfoot", "tr", "th", "td":+ // 表格颜色可能表达状态,仅由正文主题基准规则清理相同颜色。+ default:+ remove["color"], remove["background-color"] = true, true+ }+ changed = filterClipboardNodeStyle(n, remove) || changed+ if n.Data == "pre" || n.Data == "code" {+ // 代码的空白、语言和文本由语义结构保留,着色交给编辑器的代码渲染器。+ selection.Find("[style]").Each(func(_ int, child *goquery.Selection) {+ changed = filterClipboardNodeStyle(child.Get(0), remove) || changed+ })+ }+ })}var roots []*nethtml.Nodevar baseline map[string]string@@ -185,6 +203,17 @@ func normalizeBrowserClipboardStyle(dom string, documentSource bool) string {return ret}+func filterClipboardNodeStyle(n *nethtml.Node, remove map[string]bool) bool {+ for i := range n.Attr {+ if n.Attr[i].Key == "style" {+ style := n.Attr[i].Val+ n.Attr[i].Val = filterClipboardStyle(style, remove)+ return n.Attr[i].Val != style+ }+ }+ return false+}+func isClipboardDocumentBoundary(n *nethtml.Node) bool {for _, attr := range n.Attr {key, value := strings.ToLower(attr.Key), strings.ToLower(attr.Val)diff --git a/kernel/api/lute_clipboard_style_test.go b/kernel/api/lute_clipboard_style_test.goindex 048e86d1cf..5dcbfea29b 100644--- a/kernel/api/lute_clipboard_style_test.go+++ b/kernel/api/lute_clipboard_style_test.go@@ -272,3 +272,89 @@ func TestBrowserClipboardTransparentBackground(t *testing.T) {t.Errorf("transparent text inherited a background: %s", output)}}++func TestBrowserClipboardSemanticAppearance(t *testing.T) {+ style := `color: white; background-color: black; font-family: Arial; font-size: 14px;`+ for _, tc := range []struct {+ tag, content, blockType string+ }{+ {"h2", "heading", "NodeHeading"},+ {"blockquote", "<p>quote</p>", "NodeBlockquote"},+ {"ul", "<li>first<ul><li>nested</li></ul></li>", "NodeList"},+ {"ol", "<li>first</li><li>second</li>", "NodeList"},+ {"code", "inline", "NodeParagraph"},+ {"pre", "<code class=\"language-go\"> first\n\tsecond\n</code>", "NodeCodeBlock"},+ } {+ t.Run(tc.tag, func(t *testing.T) {+ input := "<" + tc.tag + " style='" + style + "'>" + tc.content + "</" + tc.tag + ">"+ output := normalizeBrowserClipboardStyle(input, false)+ before, _ := goquery.NewDocumentFromReader(strings.NewReader(input))+ after, _ := goquery.NewDocumentFromReader(strings.NewReader(output))+ want, _ := before.Find(tc.tag).First().Html()+ got, _ := after.Find(tc.tag).First().Html()+ if got != want {+ t.Errorf("semantic content changed: want %q, got %q", want, got)+ }+ lute := util.NewLute()+ lute.SetHTMLTag2TextMark(true)+ blockDOM := lute.HTML2BlockDOM(output)+ if !strings.Contains(blockDOM, tc.blockType) {+ t.Errorf("semantic type lost: %s", blockDOM)+ }+ for _, property := range clipboardThemeProperties {+ if strings.Contains(blockDOM, property+":") {+ t.Errorf("default style %s survived: %s", property, blockDOM)+ }+ }+ if again := normalizeBrowserClipboardStyle(output, false); again != output {+ t.Fatal("semantic normalization is not idempotent")+ }+ })+ }+}++func TestBrowserClipboardSemanticLocalFormatting(t *testing.T) {+ input := `<h2 style="color:black;font-size:24px"><strong>heading</strong><span style="color:red;font-size:30px">emphasis</span></h2>` ++ `<blockquote style="color:gray"><p><u>underline</u><mark>highlight</mark></p></blockquote>` ++ `<ol start="3"><li style="font-family:Arial;color:black"><span style="background:yellow">item</span></li></ol>`+ output := normalizeBrowserClipboardStyle(input, false)+ doc, _ := goquery.NewDocumentFromReader(strings.NewReader(output))+ if start, _ := doc.Find("ol").Attr("start"); start != "3" || doc.Find("strong,u,mark").Length() != 3 {+ t.Fatal("semantic attributes or emphasis lost: " + output)+ }+ for _, want := range []string{"color:red", "font-size:30px", "background:yellow"} {+ if !strings.Contains(output, want) {+ t.Errorf("local style %s lost: %s", want, output)+ }+ }+ for _, wrapper := range []string{`<div data-lark-html-role="root">`, `<div class="MsoNormal">`, `<div data-type="NodeParagraph">`} {+ protected := wrapper + input + `</div>`+ if actual := normalizeBrowserClipboardStyle(protected, false); actual != protected {+ t.Error("document semantic formatting changed: " + actual)+ }+ }+ if actual := normalizeBrowserClipboardStyle(input, true); actual != input {+ t.Error("Office clipboard semantic formatting changed: " + actual)+ }+}++func TestBrowserClipboardCodeAndTable(t *testing.T) {+ code := "<pre style='background:black'><code class='language-go' style='color:white'><span style='color:red'> first</span>\n\tsecond\n</code></pre>"+ output := normalizeBrowserClipboardStyle(code, false)+ doc, _ := goquery.NewDocumentFromReader(strings.NewReader(output))+ if doc.Find("code").Text() != " first\n\tsecond\n" {+ t.Fatal("code whitespace changed: " + output)+ }+ if language, _ := doc.Find("code").Attr("class"); language != "language-go" || strings.Contains(output, "color:") {+ t.Fatal("code language or coloring changed incorrectly: " + output)+ }+ table := `<table style="font-family:Arial"><thead><tr><th style="font-size:14px;background:gray">header</th></tr></thead><tbody><tr><td rowspan="2" style="color:red;background:yellow;font-size:14px">status</td></tr><tr></tr></tbody></table>`+ output = normalizeBrowserClipboardStyle(table, false)+ doc, _ = goquery.NewDocumentFromReader(strings.NewReader(output))+ if rowspan, _ := doc.Find("td").Attr("rowspan"); rowspan != "2" || doc.Find("th").Text() != "header" {+ t.Fatal("table structure lost: " + output)+ }+ if strings.Contains(output, "font-family") || strings.Contains(output, "font-size") || !strings.Contains(output, "color:red;background:yellow") || !strings.Contains(output, "background:gray") {+ t.Fatal("table appearance changed incorrectly: " + output)+ }+}