From ca5a5bf120eef4ce54e5763b39c9d80f251cc0f7 Mon Sep 17 00:00:00 2001 From: 0ko <0ko@noreply.codeberg.org> Date: Wed, 31 Jul 2024 18:48:46 +0000 Subject: [PATCH] feat: allow color and background-color style properties for table cells (#4766) * Allow adding text color and background color to HTML table headers and cells in markdown. * Added a few test cases. Preview and example: https://codeberg.org/attachments/98634f30-4fa2-4a76-adb3-6086af73744f Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4766 Reviewed-by: Gusted Reviewed-by: Earl Warren --- modules/markup/sanitizer.go | 4 ++-- modules/markup/sanitizer_test.go | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/modules/markup/sanitizer.go b/modules/markup/sanitizer.go index c0b449ea5..c40a3c6b6 100644 --- a/modules/markup/sanitizer.go +++ b/modules/markup/sanitizer.go @@ -111,8 +111,8 @@ func createDefaultPolicy() *bluemonday.Policy { // Allow icons, emojis, chroma syntax and keyword markup on span policy.AllowAttrs("class").Matching(regexp.MustCompile(`^((icon(\s+[\p{L}\p{N}_-]+)+)|(emoji)|(language-math display)|(language-math inline))$|^([a-z][a-z0-9]{0,2})$|^` + keywordClass + `$`)).OnElements("span") - // Allow 'color' and 'background-color' properties for the style attribute on text elements. - policy.AllowStyles("color", "background-color").OnElements("span", "p") + // Allow 'color' and 'background-color' properties for the style attribute on text elements and table cells. + policy.AllowStyles("color", "background-color").OnElements("span", "p", "th", "td") // Allow classes for file preview links... policy.AllowAttrs("class").Matching(regexp.MustCompile("^(lines-num|lines-code chroma)$")).OnElements("td") diff --git a/modules/markup/sanitizer_test.go b/modules/markup/sanitizer_test.go index b7b8792bd..56b2fcf47 100644 --- a/modules/markup/sanitizer_test.go +++ b/modules/markup/sanitizer_test.go @@ -47,8 +47,10 @@ func Test_Sanitizer(t *testing.T) { // Color property `Hello World`, `Hello World`, - `

Hello World

`, `

Hello World

`, + `

Hello World

`, `

Hello World

`, + `
TH1TH2TH3
TD1TD2TD3
`, `
TH1TH2TH3
TD1TD2TD3
`, `Hello World`, `Hello World`, + `Hello World`, `Hello World`, `Hello World`, `Hello World`, `

Hello World

`, `

Hello World

`, `Hello World`, `Hello World`,