Skip to content

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

:bug: Normalize pasted web link styles and transparent backgrounds https://github.com/siyuan-note/siyuan/issues/19155

Daniel9cb0cbbab254
diff --git a/kernel/api/lute_clipboard_style.go b/kernel/api/lute_clipboard_style.go
index ab1d5e44dc..31b497961d 100644
--- a/kernel/api/lute_clipboard_style.go
+++ b/kernel/api/lute_clipboard_style.go
@@ -17,6 +17,7 @@
package api
import (
+ "regexp"
"strings"
"github.com/88250/lute/parse"
@@ -26,13 +27,15 @@ import (
var clipboardThemeProperties = []string{"color", "background-color", "font-family", "font-size"}
+var clipboardTransparentColor = regexp.MustCompile(`(?i)^(transparent|#[0-9a-f]{3}0|#[0-9a-f]{6}00|(?:rgba|hsla)\([^)]*,\s*(?:0(?:\.0*)?|\.0+)%?\s*\)|(?:rgb|rgba|hsl|hsla)\([^)]*/\s*(?:0(?:\.0*)?|\.0+)%?\s*\))$`)
+
// normalizeBrowserClipboardStyle 移除浏览器复制正文时附带的共同主题样式,保留局部格式差异。
func normalizeBrowserClipboardStyle(dom string, documentSource bool) string {
lower := strings.ToLower(dom)
if strings.Contains(lower, "data-type=") {
return dom
}
- if !strings.Contains(lower, "-webkit-text-stroke-width") {
+ if !strings.Contains(lower, "style") {
return dom
}
doc, err := goquery.NewDocumentFromReader(strings.NewReader(dom))
@@ -44,6 +47,41 @@ func normalizeBrowserClipboardStyle(dom string, documentSource bool) string {
documentSource = true
}
})
+ changed := false
+ if !documentSource {
+ doc.Find("[style]").Each(func(_ int, selection *goquery.Selection) {
+ decl := parse.HTMLStyleDeclarations(clipboardNodeStyle(selection.Get(0)))
+ changed = changed || clipboardTransparentColor.MatchString(decl["background-color"])
+ })
+ // 网页链接自身的外观交给编辑器,子元素中的显式格式及语义标签继续保留。
+ doc.Find("a[href]").Each(func(_ int, selection *goquery.Selection) {
+ n := selection.Get(0)
+ for i := range n.Attr {
+ if n.Attr[i].Key != "style" {
+ continue
+ }
+ style := n.Attr[i].Val
+ remove := map[string]bool{"color": true, "background-color": true, "font-family": true, "font-size": true}
+ decl := parse.HTMLStyleDeclarations(style)
+ var retained strings.Builder
+ for _, key := range []string{"text-decoration", "text-decoration-line"} {
+ var parts []string
+ for _, part := range strings.Fields(decl[key]) {
+ if strings.EqualFold(part, "underline") {
+ remove[key] = true
+ } else {
+ parts = append(parts, part)
+ }
+ }
+ if remove[key] && len(parts) > 0 {
+ retained.WriteString(key + ": " + strings.Join(parts, " ") + ";")
+ }
+ }
+ n.Attr[i].Val = filterClipboardStyle(style, remove) + retained.String()
+ changed = changed || n.Attr[i].Val != style
+ }
+ })
+ }
var roots []*nethtml.Node
var baseline map[string]string
ambiguous := false
@@ -78,7 +116,10 @@ func normalizeBrowserClipboardStyle(dom string, documentSource bool) string {
}
collect(doc.Find("body").Get(0))
if len(roots) == 0 || ambiguous {
- return dom
+ roots = nil
+ if !changed {
+ return dom
+ }
}
var clean func(*nethtml.Node, map[string]string)
clean = func(n *nethtml.Node, inherited map[string]string) {
@@ -115,6 +156,28 @@ func normalizeBrowserClipboardStyle(dom string, documentSource bool) string {
for _, root := range roots {
clean(root, nil)
}
+ if !documentSource {
+ // 透明背景不生成文本样式;存在祖先背景时保留重置语义,避免被文本样式继承重新着色。
+ doc.Find("[style]").Each(func(_ int, selection *goquery.Selection) {
+ n := selection.Get(0)
+ for i := range n.Attr {
+ if n.Attr[i].Key != "style" {
+ continue
+ }
+ decl := parse.HTMLStyleDeclarations(n.Attr[i].Val)
+ if !clipboardTransparentColor.MatchString(decl["background-color"]) {
+ continue
+ }
+ n.Attr[i].Val = filterClipboardStyle(n.Attr[i].Val, map[string]bool{"background-color": true})
+ for parent := n.Parent; parent != nil; parent = parent.Parent {
+ if parse.HTMLStyleDeclarations(clipboardNodeStyle(parent))["background-color"] != "" {
+ n.Attr[i].Val += "background-color: initial;"
+ break
+ }
+ }
+ }
+ })
+ }
ret, err := doc.Find("body").Html()
if err != nil {
return dom
diff --git a/kernel/api/lute_clipboard_style_test.go b/kernel/api/lute_clipboard_style_test.go
index 0bcd130a8f..048e86d1cf 100644
--- a/kernel/api/lute_clipboard_style_test.go
+++ b/kernel/api/lute_clipboard_style_test.go
@@ -206,3 +206,69 @@ func TestDocumentClipboardAmbiguousStyles(t *testing.T) {
}
}
}
+
+func TestBrowserClipboardLinkAppearance(t *testing.T) {
+ doc, _ := goquery.NewDocumentFromReader(strings.NewReader(githubClipboardFixture(t)))
+ theme, _ := doc.Find("p").First().Attr("style")
+ for _, input := range []string{
+ `<p>before <a href="https://example.com" title="title" style="color:rgb(9,105,218);background-color:rgba(0,0,0,0);text-decoration:underline">link</a> after</p>`,
+ `<a href="https://example.com" title="title" style='` + theme + `text-decoration:underline'>link</a>`,
+ `<p style='` + theme + `'>before <a href="https://example.com" title="title" style="color:rgb(9,105,218);text-decoration-line:underline">link</a> after</p>`,
+ } {
+ output := normalizeBrowserClipboardStyle(input, false)
+ lute := util.NewLute()
+ lute.SetHTMLTag2TextMark(true)
+ blockDOM := lute.HTML2BlockDOM(output)
+ if !strings.Contains(blockDOM, `data-type="a"`) || !strings.Contains(blockDOM, `data-href="https://example.com"`) || !strings.Contains(blockDOM, `data-title="title"`) {
+ t.Errorf("link semantics lost: %s", blockDOM)
+ }
+ for _, unexpected := range []string{"color:", "font-family:", "font-size:", `data-type="a u"`} {
+ if strings.Contains(blockDOM, unexpected) {
+ t.Errorf("link appearance survived %q: %s", unexpected, blockDOM)
+ }
+ }
+ if again := normalizeBrowserClipboardStyle(output, false); again != output {
+ t.Fatal("link cleanup is not idempotent")
+ }
+ }
+}
+
+func TestBrowserClipboardLinkExplicitContent(t *testing.T) {
+ input := `<a href="https://example.com" style="color:blue;text-decoration:underline line-through"><strong>bold</strong><u>underline</u><span style="color:red;background:yellow;font-size:20px;font-family:serif">styled</span></a>`
+ lute := util.NewLute()
+ lute.SetHTMLTag2TextMark(true)
+ output := lute.HTML2BlockDOM(normalizeBrowserClipboardStyle(input, false))
+ for _, want := range []string{"strong", "u", "s", "color: red", "background-color: yellow", "font-size: 20.000000px", "font-family: serif"} {
+ if !strings.Contains(output, want) {
+ t.Errorf("missing %s: %s", want, output)
+ }
+ }
+ for _, input := range []string{input, `<div data-lark-html-role="root">` + input + `</div>`, `<p class=MsoNormal>` + input + `</p>`} {
+ if output := normalizeBrowserClipboardStyle(input, true); output != input {
+ t.Errorf("document link changed: %s", output)
+ }
+ }
+}
+
+func TestBrowserClipboardTransparentBackground(t *testing.T) {
+ for _, color := range []string{"transparent", "rgba(0, 0, 0, 0)", "rgb(0 0 0 / 0%)", "#1230", "#12345600"} {
+ input := `<span style="background-color:` + color + `">plain</span>`
+ output := normalizeBrowserClipboardStyle(input, false)
+ if strings.Contains(output, "background-color") {
+ t.Error(output)
+ }
+ }
+ for _, color := range []string{"#abcd00", "rgba(0,0,0,0.5)", "yellow"} {
+ input := `<span style="background-color:` + color + `">highlight</span>`
+ if output := normalizeBrowserClipboardStyle(input, false); output != input {
+ t.Errorf("visible background changed: %s", output)
+ }
+ }
+ input := `<div style="background-color:yellow"><span style="background-color:rgba(0,0,0,0)">plain</span></div>`
+ lute := util.NewLute()
+ lute.SetHTMLTag2TextMark(true)
+ output := lute.HTML2BlockDOM(normalizeBrowserClipboardStyle(input, false))
+ if strings.Contains(output, "background-color:") {
+ t.Errorf("transparent text inherited a background: %s", output)
+ }
+}