Skip to content

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

:bug: Preserve custom inline styles in rich table cells https://github.com/siyuan-note/siyuan/issues/19840

Daniel6a8eb2a98dfb
diff --git a/app/src/protyle/render/av/richText.ts b/app/src/protyle/render/av/richText.ts
index cffcdeb358..42967d3c0a 100644
--- a/app/src/protyle/render/av/richText.ts
+++ b/app/src/protyle/render/av/richText.ts
@@ -122,6 +122,10 @@ const sanitizeTableCellCustomStyle = (style: string) => {
declarations.push(`${property}: ${value};`);
return;
}
+ if (/^var\(--b3-font-(?:color|background)(?:[1-9]|1[0-3])\)$/.test(value)) {
+ declarations.push(`${property}: ${value};`);
+ return;
+ }
const rgb = value.match(/^rgba?\(\s*(\d{1,3})(?:,\s*|\s+)(\d{1,3})(?:,\s*|\s+)(\d{1,3})(?:\s*[,/]\s*(0(?:\.\d+)?|1(?:\.0+)?))?\s*\)$/i);
if (rgb && [rgb[1], rgb[2], rgb[3]].every(channel => Number(channel) <= 255)) {
declarations.push(`${property}: ${value};`);
diff --git a/app/src/protyle/util/tableCellRichLute.test.ts b/app/src/protyle/util/tableCellRichLute.test.ts
index 830f91f838..35f6bdd3b3 100644
--- a/app/src/protyle/util/tableCellRichLute.test.ts
+++ b/app/src/protyle/util/tableCellRichLute.test.ts
@@ -34,12 +34,20 @@ const browserCases = async (source: string, enterSource: string, hintSource: str
check.match(reopenedCustom, /--custom-symble-strong: rgb\(0 166 240 \/ 1\)/);
check.match(reopenedCustom, /second/);
check.doesNotMatch(api.sanitizeAVRichTextBlockDOM(customBlockDOM, true), /custom_symble/);
+ const variableHTML = '<span data-type="custom_symble_strong_CJK_rectangle_yin" ' +
+ 'style="--custom-symble-strong: var(--b3-font-color12);">colored</span>';
+ const variableValue = api.serializeTableCellRich(base.Md2BlockDOM("before " + variableHTML));
+ const variableCell = document.createElement("td");
+ api.updateTableCellEditingValue(variableCell, variableValue);
+ check.match(variableValue.markdown, /--custom-symble-strong: var\(--b3-font-color12\)/);
+ check.match(api.getTableCellRichBlockDOM(variableCell), /--custom-symble-strong: var\(--b3-font-color12\)/);
+ check.match(api.getTableCellRichBlockDOM(variableCell), /before/);
const unsafeCustom = api.sanitizeAVRichTextBlockDOM(
'<div data-type="NodeParagraph"><div contenteditable="true">' +
'<span data-type="custom_bad" style="--custom-bad: url(javascript:alert(1)); ' +
- '--custom-safe: #abc;" onclick="alert(1)">safe</span></div></div>', true, true);
+ '--custom-unknown: var(--theme-color); --custom-safe: #abc;" onclick="alert(1)">safe</span></div></div>', true, true);
check.match(unsafeCustom, /--custom-safe: #abc;/);
- check.doesNotMatch(unsafeCustom, /url\(|onclick|javascript:/);
+ check.doesNotMatch(unsafeCustom, /url\(|onclick|javascript:|--custom-unknown/);
const externalCustom = api.serializeTableCellRich(base.Md2BlockDOM(customHTML));
check.match(externalCustom.blockDOM, /data-type="custom_symble_strong_CJK_rectangle_yin"/);
check.match(externalCustom.blockDOM, /--custom-symble-strong: rgb\(0 166 240 \/ 1\)/);
diff --git a/kernel/av/table_cell_rich.go b/kernel/av/table_cell_rich.go
index b4e4851b39..8ede19ee10 100644
--- a/kernel/av/table_cell_rich.go
+++ b/kernel/av/table_cell_rich.go
@@ -2,12 +2,22 @@ package av
import (
"fmt"
+ "regexp"
+ "sort"
+ "strconv"
"strings"
"github.com/88250/lute/ast"
"github.com/88250/lute/parse"
)
+var (
+ tableCellRichCustomTypePattern = regexp.MustCompile(`^custom_[A-Za-z0-9_-]{1,128}$`)
+ tableCellRichCustomPropertyPattern = regexp.MustCompile(`^--custom-[a-z0-9-]{1,128}$`)
+ tableCellRichCustomHexPattern = regexp.MustCompile(`^#(?:[0-9A-Fa-f]{3}|[0-9A-Fa-f]{4}|[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$`)
+ tableCellRichCustomRGBPattern = regexp.MustCompile(`(?i)^rgba?\(\s*(\d{1,3})(?:,\s*|\s+)(\d{1,3})(?:,\s*|\s+)(\d{1,3})(?:\s*[,/]\s*(?:0(?:\.\d+)?|1(?:\.0+)?))?\s*\)$`)
+)
+
// ParseTableCellRich 复用数据库片段的格式校验,并保留普通表格已有的图片能力。
func ParseTableCellRich(rich *ast.TableCellRich) (*parse.Tree, error) {
if nil == rich {
@@ -31,6 +41,107 @@ func RenderTableCellRich(tree *parse.Tree) (string, error) {
return content, err
}
+func isTableCellRichCustomTextMark(node *ast.Node) bool {
+ if nil == node || ast.NodeTextMark != node.Type {
+ return false
+ }
+ for _, typ := range strings.Fields(node.TextMarkType) {
+ if tableCellRichCustomTypePattern.MatchString(typ) {
+ return true
+ }
+ }
+ return false
+}
+
+func isAllowedTableCellRichCustomNode(node *ast.Node) bool {
+ if nil == node {
+ return false
+ }
+ if ast.NodeKramdownSpanIAL == node.Type {
+ return isAllowedTableCellRichCustomSpanIAL(node)
+ }
+ if ast.NodeTextMark != node.Type || !isTableCellRichCustomTextMark(node) ||
+ "" != node.TextMarkFlashcardOcclusionID || !isAllowedValueTextMarkReferenceData(node) {
+ return false
+ }
+ for _, typ := range strings.Fields(node.TextMarkType) {
+ if tableCellRichCustomTypePattern.MatchString(typ) {
+ continue
+ }
+ standard := *node
+ standard.TextMarkType = typ
+ if !isAllowedValueTextMarkType(&standard) {
+ return false
+ }
+ }
+ if 1 > len(node.KramdownIAL) {
+ return nil == node.Next || ast.NodeKramdownSpanIAL != node.Next.Type
+ }
+ return nil != node.Next && isAllowedTableCellRichCustomSpanIAL(node.Next)
+}
+
+func isAllowedTableCellRichCustomSpanIAL(node *ast.Node) bool {
+ if nil == node || nil == node.Previous || !isTableCellRichCustomTextMark(node.Previous) {
+ return false
+ }
+ markIAL := node.Previous.KramdownIAL
+ spanIAL := parse.Tokens2IAL(node.Tokens)
+ if 1 != len(markIAL) || 1 != len(spanIAL) || 2 != len(markIAL[0]) || 2 != len(spanIAL[0]) ||
+ "style" != markIAL[0][0] || "style" != spanIAL[0][0] {
+ return false
+ }
+ style := node.Previous.IALAttr("style")
+ if "" == style || style != parse.IALVal(node, "style") {
+ return false
+ }
+ _, ok := normalizeTableCellRichCustomStyle(style)
+ return ok
+}
+
+func normalizeTableCellRichCustomStyle(style string) (string, bool) {
+ if 2048 < len(style) {
+ return "", false
+ }
+ declarations, ok := parseValueTextRichStyleDeclarations(style)
+ if !ok {
+ return "", false
+ }
+ var standard, custom []string
+ for property, value := range declarations {
+ if !tableCellRichCustomPropertyPattern.MatchString(property) {
+ standard = append(standard, property+": "+value+";")
+ continue
+ }
+ if !tableCellRichCustomHexPattern.MatchString(value) {
+ rgb := tableCellRichCustomRGBPattern.FindStringSubmatch(value)
+ if 4 == len(rgb) {
+ for _, channel := range rgb[1:] {
+ number, err := strconv.Atoi(channel)
+ if nil != err || 255 < number {
+ return "", false
+ }
+ }
+ } else if normalized, valid := normalizeValueTextRichBuiltinPaletteValue("color", value); valid {
+ value = normalized
+ } else if normalized, valid = normalizeValueTextRichBuiltinPaletteValue("background-color", value); valid {
+ value = normalized
+ } else {
+ return "", false
+ }
+ }
+ custom = append(custom, property+": "+value+";")
+ }
+ sort.Strings(custom)
+ if 0 < len(standard) {
+ normalized, valid := normalizeValueTextRichStyle(strings.Join(standard, " "))
+ if !valid {
+ return "", false
+ }
+ standard = []string{normalized}
+ }
+ return strings.Join(append(standard, custom...), " "), true
+}
+
func isAllowedTableCellRichImageNode(node *ast.Node) bool {
if ast.NodeBang == node.Type {
return node.ParentIs(ast.NodeImage)
diff --git a/kernel/av/table_cell_rich_test.go b/kernel/av/table_cell_rich_test.go
index e125526991..ef60de90bd 100644
--- a/kernel/av/table_cell_rich_test.go
+++ b/kernel/av/table_cell_rich_test.go
@@ -67,3 +67,51 @@ func TestTableCellRichRejectsInvalidCodeSettings(t *testing.T) {
}
}
}
+
+func TestTableCellRichCustomInlineStyle(t *testing.T) {
+ for _, style := range []string{
+ "--custom-symble-strong: rgb(0 166 240 / 1);",
+ "--custom-symble-strong: var(--b3-font-color12);",
+ } {
+ source := `before <span data-type="custom_symble_strong_CJK_rectangle_yin" style="` + style + `">after</span>`
+ rich := &ast.TableCellRich{Spec: 1, Format: "kramdown", Content: source}
+ for pass := 0; pass < 3; pass++ {
+ tree, err := ParseTableCellRich(rich)
+ if err != nil {
+ t.Fatal(err)
+ }
+ content, err := RenderTableCellRich(tree)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !strings.Contains(content, style) || !strings.Contains(content, "before") ||
+ !strings.Contains(content, "after") || pass > 0 && content != rich.Content {
+ t.Fatalf("table custom inline source changed: %q != %q", content, rich.Content)
+ }
+ rich.Content = content
+ }
+ if _, err := ParseValueTextRich(&ValueTextRich{Spec: ValueTextRichSpec,
+ Format: ValueTextRichFormatKramdown, Content: source}); err == nil {
+ t.Fatal("table custom inline must not expand the database rich text whitelist")
+ }
+ }
+}
+
+func TestTableCellRichRejectsUnsafeCustomInlineStyle(t *testing.T) {
+ for _, style := range []string{
+ "--custom-symble-strong: url(javascript:alert(1));",
+ "--custom-symble-strong: var(--theme-color);",
+ "--custom-symble-strong: var(--b3-font-color14);",
+ "--custom-symble-strong: rgb(256 0 0 / 1);",
+ "--custom-symble-strong: #abc; background-image: url(javascript:alert(1));",
+ } {
+ source := `<span data-type="custom_symble_strong_CJK_rectangle_yin" style="` + style + `">unsafe</span>`
+ rich := &ast.TableCellRich{Spec: 1, Format: "kramdown", Content: source}
+ if _, err := ParseTableCellRich(rich); nil == err {
+ t.Fatalf("accepted unsafe table custom inline style: %q", style)
+ }
+ if rich.Content != source {
+ t.Fatal("rejected source must remain unchanged")
+ }
+ }
+}
diff --git a/kernel/av/value.go b/kernel/av/value.go
index f42de3fb15..a13c46f871 100644
--- a/kernel/av/value.go
+++ b/kernel/av/value.go
@@ -607,6 +607,9 @@ func validateValueTextRichTreeWithImages(tree *parse.Tree, images bool) (err err
if images && ast.NodeKramdownBlockIAL == node.Type {
allowed = isAllowedValueTextRichBlockIAL(node, true)
}
+ if images && !allowed {
+ allowed = isAllowedTableCellRichCustomNode(node)
+ }
if !allowed && !(images && isAllowedTableCellRichImageNode(node)) {
err = fmt.Errorf("unsupported attribute view rich text node [%s]", node.Type.String())
return ast.WalkStop
@@ -1285,7 +1288,7 @@ func normalizeValueTextRichBuiltinStyleValue(property, value string) (ret string
match[1], valueSuffix, match[1], legacySuffix), true
}
-func normalizeValueTextRichTreeStyles(tree *parse.Tree) (err error) {
+func normalizeValueTextRichTreeStyles(tree *parse.Tree, images bool) (err error) {
ast.Walk(tree.Root, func(node *ast.Node, entering bool) ast.WalkStatus {
if !entering || ast.NodeTextMark != node.Type || 1 > len(node.KramdownIAL) {
return ast.WalkContinue
@@ -1293,7 +1296,12 @@ func normalizeValueTextRichTreeStyles(tree *parse.Tree) (err error) {
style := node.IALAttr("style")
var normalized string
var ok bool
- if normalized, ok = normalizeValueTextRichStyle(style); !ok {
+ if images && isTableCellRichCustomTextMark(node) {
+ normalized, ok = normalizeTableCellRichCustomStyle(style)
+ } else {
+ normalized, ok = normalizeValueTextRichStyle(style)
+ }
+ if !ok {
err = fmt.Errorf("unsupported attribute view rich text style")
return ast.WalkStop
}
@@ -2175,7 +2183,7 @@ func normalizeValueTextRichTreeSourceWithImages(tree *parse.Tree, images bool) (
normalizedTree = tree
previous := ""
for iteration := 0; iteration < 4; iteration++ {
- if err = normalizeValueTextRichTreeStyles(normalizedTree); nil != err {
+ if err = normalizeValueTextRichTreeStyles(normalizedTree, images); nil != err {
return "", nil, err
}
luteEngine := newValueTextRichLute()
diff --git a/kernel/treenode/table_cell_rich_test.go b/kernel/treenode/table_cell_rich_test.go
index 170040427e..73e74c41f9 100644
--- a/kernel/treenode/table_cell_rich_test.go
+++ b/kernel/treenode/table_cell_rich_test.go
@@ -30,6 +30,7 @@ func TestTableCellRichSourceProjectionAndSpec(t *testing.T) {
"```go\na | b\n```\n" + `{: id="20260921000000-code001" linewrap="false" linenumber="true" ligatures="false"}`,
"- [ ] open\n- [x] done\n\n![image](assets/image.png) ((20240101000000-abcdefg 'reference'))",
"<span data-type=\"text\" style=\"color: var(--b3-font-color1);\">styled</span>",
+ `before <span data-type="custom_symble_strong_CJK_rectangle_yin" style="--custom-symble-strong: var(--b3-font-color12);">after</span>`,
"",
}
for _, source := range sources {