From cd92c6a008b5a6ec60763cc892ae4fc7739ddfe6 Mon Sep 17 00:00:00 2001 From: silverwind Date: Sun, 8 Nov 2026 09:01:50 +0000 Subject: [PATCH] fix: paste copied code as text, not as a markdown table (#40053) Copying lines from a diff, file view, blame, code search or code preview and pasting them into a comment on Gitea or GitHub produced a markdown table with escaped pipes and stripped indentation. Both use `@github/paste-markdown`, which converts copied HTML tables unless they carry `data-paste-markdown-skip`, as GitHub's own code tables do. Gitea's code tables now set it too. The test is a bit more complicated because of a upstream bug which causes `paste-markdown` to insert pasted content twice, once https://github.com/github/paste-markdown/pull/128 is merged and released, the negative assertion could be removed. --------- Signed-off-by: silverwind --- modules/markup/sanitizer_default.go | 3 ++- modules/markup/sanitizer_default_test.go | 2 ++ routers/web/repo/setting/lfs.go | 2 +- .../markup/renderhelper_codepreview_test.go | 4 +-- templates/base/markup_codepreview.tmpl | 2 +- templates/repo/blame.tmpl | 2 +- templates/repo/diff/box.tmpl | 2 +- templates/repo/editor/diff_preview.tmpl | 2 +- .../repo/issue/view_content/conversation.tmpl | 2 +- templates/repo/view_file.tmpl | 2 +- templates/shared/searchfile.tmpl | 2 +- tests/e2e/pr-diff-copy.test.ts | 25 +++++++++++++++++++ 12 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 tests/e2e/pr-diff-copy.test.ts diff --git a/modules/markup/sanitizer_default.go b/modules/markup/sanitizer_default.go index 657dfd130e5..1d4a062059f 100644 --- a/modules/markup/sanitizer_default.go +++ b/modules/markup/sanitizer_default.go @@ -31,8 +31,9 @@ func (st *Sanitizer) createDefaultPolicy() *bluemonday.Policy { // Chroma always uses 1-2 letters for style names, we could tolerate it at the moment policy.AllowAttrs("class").Matching(regexp.MustCompile(`^\w{0,2}$`)).OnElements("span") - // Line numbers on codepreview + // Line numbers and paste behavior on codepreview policy.AllowAttrs("data-line-number").OnElements("span") + policy.AllowAttrs("data-paste-markdown-skip").OnElements("table") // emoji aliases for dark theme inversion policy.AllowAttrs("data-alias").OnElements("span") diff --git a/modules/markup/sanitizer_default_test.go b/modules/markup/sanitizer_default_test.go index 56d67bced77..3df87c8d73e 100644 --- a/modules/markup/sanitizer_default_test.go +++ b/modules/markup/sanitizer_default_test.go @@ -49,6 +49,8 @@ func TestSanitizer(t *testing.T) { `

Hello World

`, `

Hello World

`, `Hello World`, `Hello World`, + `
1
`, `
1
`, + // Org mode status of list items. `
  • `, `
  • `, `
  • `, `
  • `, diff --git a/routers/web/repo/setting/lfs.go b/routers/web/repo/setting/lfs.go index 98f0f7530a7..e72aef67f1a 100644 --- a/routers/web/repo/setting/lfs.go +++ b/routers/web/repo/setting/lfs.go @@ -290,7 +290,7 @@ func LFSFileGet(ctx *context.Context) { escapeStatus, fileContentHTML := charset.EscapeControlHTML(fileContentHTML, ctx.Locale) output := &htmlutil.HTMLBuilder{} - output.WriteHTML(``) + output.WriteHTML(`
    `) writeLine := func(lineNum int, line template.HTML) { output.WriteFormatf(``, lineNum, line) } diff --git a/services/markup/renderhelper_codepreview_test.go b/services/markup/renderhelper_codepreview_test.go index 954b403654d..114314b2573 100644 --- a/services/markup/renderhelper_codepreview_test.go +++ b/services/markup/renderhelper_codepreview_test.go @@ -34,7 +34,7 @@ func TestRenderHelperCodePreview(t *testing.T) { repo1/README.md repo.code_preview_line_from_to:1,2,65f1bf27bc -
    %d%s
    +
    # repo1 @@ -63,7 +63,7 @@ func TestRenderHelperCodePreview(t *testing.T) { repo1/README.md repo.code_preview_line_in:1,65f1bf27bc
    - +
    # repo1 diff --git a/templates/base/markup_codepreview.tmpl b/templates/base/markup_codepreview.tmpl index 4c04b5092c7..df21cd75446 100644 --- a/templates/base/markup_codepreview.tmpl +++ b/templates/base/markup_codepreview.tmpl @@ -8,7 +8,7 @@ {{ctx.Locale.Tr "repo.code_preview_line_from_to" .LineStart .LineStop $link}} {{- end}}
    - +
    {{- range $idx, $line := .HighlightLines -}} diff --git a/templates/repo/blame.tmpl b/templates/repo/blame.tmpl index a8cc5dce4eb..7bf8595763e 100644 --- a/templates/repo/blame.tmpl +++ b/templates/repo/blame.tmpl @@ -35,7 +35,7 @@ {{else if not .FileSize}} {{template "shared/fileisempty"}} {{else}} -
    +
    {{range $row := .BlameRows}} diff --git a/templates/repo/diff/box.tmpl b/templates/repo/diff/box.tmpl index 9879754bef6..729d60b8c13 100644 --- a/templates/repo/diff/box.tmpl +++ b/templates/repo/diff/box.tmpl @@ -190,7 +190,7 @@ {{end}} {{else if $file.Sections}} -
    +
    {{if $.IsSplitStyle}} {{template "repo/diff/section_split" dict "file" $file}} {{else}} diff --git a/templates/repo/editor/diff_preview.tmpl b/templates/repo/editor/diff_preview.tmpl index 60fc730e8ca..dd352500edb 100644 --- a/templates/repo/editor/diff_preview.tmpl +++ b/templates/repo/editor/diff_preview.tmpl @@ -2,7 +2,7 @@
    -
    +
    {{template "repo/diff/section_unified" dict "file" .File}} diff --git a/templates/repo/issue/view_content/conversation.tmpl b/templates/repo/issue/view_content/conversation.tmpl index e7768b92b55..723e7ca0f4f 100644 --- a/templates/repo/issue/view_content/conversation.tmpl +++ b/templates/repo/issue/view_content/conversation.tmpl @@ -54,7 +54,7 @@ The variables in "ctx.Data" are different in each case, making this template fra
    -
    +
    {{template "repo/diff/section_unified" dict "file" $file}} diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index 55e5184b72d..2e69135730a 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -113,7 +113,7 @@ {{else if .IsPlainText}}
    {{if .FileContent}}{{.FileContent}}{{end}}
    {{else if .FileContent}} -
    +
    {{range $idx, $code := .FileContent}} {{$line := Eval $idx "+" 1}} diff --git a/templates/shared/searchfile.tmpl b/templates/shared/searchfile.tmpl index 280584e4d1f..241e6a95caa 100644 --- a/templates/shared/searchfile.tmpl +++ b/templates/shared/searchfile.tmpl @@ -1,5 +1,5 @@
    -
    +
    {{range .SearchResult.Lines}} diff --git a/tests/e2e/pr-diff-copy.test.ts b/tests/e2e/pr-diff-copy.test.ts new file mode 100644 index 00000000000..443e2d7d86b --- /dev/null +++ b/tests/e2e/pr-diff-copy.test.ts @@ -0,0 +1,25 @@ +import {env} from 'node:process'; +import {test, expect} from '@playwright/test'; +import {apiCreateFiles, apiCreatePR, apiCreateRepo, login, randomString} from './utils.ts'; + +test('a copied pull request diff pastes as text', async ({page, request}) => { + const repoName = `e2e-pr-diff-copy-${randomString(8)}`; + const owner = env.GITEA_TEST_E2E_USER; + const [index] = await Promise.all([ + (async () => { + await apiCreateRepo(request, {name: repoName}); + await apiCreateFiles(request, owner, repoName, [{path: 'copy.txt', content: 'one | two\n'}], {branch: 'main', newBranch: 'feat'}); + return apiCreatePR(request, owner, repoName, 'feat', 'main', 'title'); + })(), + login(page), + ]); + await page.goto(`/${owner}/${repoName}/pulls/${index}/files`); + + await page.getByRole('main').selectText(); + await page.keyboard.press('ControlOrMeta+KeyC'); + await page.getByRole('button', {name: 'Review', exact: true}).click(); + const comment = page.getByRole('textbox', {name: 'Review comment'}); + await comment.press('ControlOrMeta+KeyV'); + await expect(comment).toHaveValue(/one \| two/); + await expect(comment).not.toHaveValue(/one \\\| two/); // paste-markdown also inserts the table until https://github.com/github/paste-markdown/pull/128 +});