From a6168fa25d0a7dfee689e1bce940557fff70d392 Mon Sep 17 00:00:00 2001 From: mrsdizzie Date: Wed, 8 Jul 2020 17:02:38 -0400 Subject: [PATCH] Make copy/paste work for source code (#12191) * Make copy/paste work for source code Fix regression casued by #12047 so copy/paste works properly in all browsers. Fixes #12184 Also while looking at this I saw a small display issue for blame view. I think #12023 was merged into original PR through an update branch before #12047 was merged and made one of the css ruules not apply anymore. * use pseudo-element to prevent copying of comment + symbol even when not visually selected * remove added newline here should not be necessary anymore * make sure empty line is newline so there is something to select and copy --- modules/highlight/highlight.go | 17 +++++++++++++++-- services/gitdiff/gitdiff.go | 6 +++--- templates/repo/diff/box.tmpl | 4 ++-- templates/repo/diff/section_unified.tmpl | 2 +- templates/repo/home.tmpl | 2 +- templates/repo/view_file.tmpl | 2 +- web_src/less/_base.less | 4 ++++ 7 files changed, 27 insertions(+), 10 deletions(-) diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go index 5d66e4c32d..31448ccd63 100644 --- a/modules/highlight/highlight.go +++ b/modules/highlight/highlight.go @@ -44,6 +44,9 @@ func NewContext() { func Code(fileName, code string) string { NewContext() + if code == "" { + return "\n" + } if len(code) > sizeLimit { return code } @@ -133,7 +136,12 @@ func File(numLines int, fileName string, code []byte) map[int]string { m := make(map[int]string, numLines) for k, v := range strings.SplitN(htmlbuf.String(), "\n", numLines) { line := k + 1 - m[line] = string(v) + content := string(v) + //need to keep lines that are only \n so copy/paste works properly in browser + if content == "" { + content = "\n" + } + m[line] = content } return m } @@ -143,7 +151,12 @@ func plainText(code string, numLines int) map[int]string { m := make(map[int]string, numLines) for k, v := range strings.SplitN(string(code), "\n", numLines) { line := k + 1 - m[line] = string(v) + content := string(v) + //need to keep lines that are only \n so copy/paste works properly in browser + if content == "" { + content = "\n" + } + m[line] = content } return m } diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 80e6eb1ecd..d335661ffb 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -269,20 +269,20 @@ func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) tem case DiffLineAdd: compareDiffLine = diffSection.GetLine(DiffLineDel, diffLine.RightIdx) if compareDiffLine == nil { - return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:]+"\n")) + return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:])) } diff1 = compareDiffLine.Content diff2 = diffLine.Content case DiffLineDel: compareDiffLine = diffSection.GetLine(DiffLineAdd, diffLine.LeftIdx) if compareDiffLine == nil { - return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:]+"\n")) + return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:])) } diff1 = diffLine.Content diff2 = compareDiffLine.Content default: if strings.IndexByte(" +-", diffLine.Content[0]) > -1 { - return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:]+"\n")) + return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:])) } return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content)) } diff --git a/templates/repo/diff/box.tmpl b/templates/repo/diff/box.tmpl index 960710a987..adb9184131 100644 --- a/templates/repo/diff/box.tmpl +++ b/templates/repo/diff/box.tmpl @@ -139,10 +139,10 @@ {{else}} {{if $line.LeftIdx}}{{end}} - {{if and $.SignedUserID $line.CanComment $.PageIsPullFiles (not (eq .GetType 2))}}+{{end}}{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}} + {{if and $.SignedUserID $line.CanComment $.PageIsPullFiles (not (eq .GetType 2))}}{{end}}{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}} {{if $line.RightIdx}}{{end}} - {{if and $.SignedUserID $line.CanComment $.PageIsPullFiles (not (eq .GetType 3))}}+{{end}}{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}} + {{if and $.SignedUserID $line.CanComment $.PageIsPullFiles (not (eq .GetType 3))}}{{end}}{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}} {{end}} {{if gt (len $line.Comments) 0}} diff --git a/templates/repo/diff/section_unified.tmpl b/templates/repo/diff/section_unified.tmpl index 495923ec85..040545069a 100644 --- a/templates/repo/diff/section_unified.tmpl +++ b/templates/repo/diff/section_unified.tmpl @@ -22,7 +22,7 @@ {{if eq .GetType 4}} {{$section.GetComputedInlineDiffFor $line}} {{else}} - {{if and $.root.SignedUserID $line.CanComment $.root.PageIsPullFiles}}+{{end}}{{$section.GetComputedInlineDiffFor $line}} + {{if and $.root.SignedUserID $line.CanComment $.root.PageIsPullFiles}}{{end}}{{$section.GetComputedInlineDiffFor $line}} {{end}} {{if gt (len $line.Comments) 0}} diff --git a/templates/repo/home.tmpl b/templates/repo/home.tmpl index f3e07b686e..0e017f532b 100644 --- a/templates/repo/home.tmpl +++ b/templates/repo/home.tmpl @@ -1,5 +1,5 @@ {{template "base/head" .}} -
+
{{template "repo/header" .}}
{{template "base/alert" .}} diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index 5e519354b7..782331aad7 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -105,7 +105,7 @@ -
{{$code | Safe}}
+ {{$code | Safe}} {{end}} diff --git a/web_src/less/_base.less b/web_src/less/_base.less index 93e8af806c..ee02601676 100644 --- a/web_src/less/_base.less +++ b/web_src/less/_base.less @@ -1039,6 +1039,10 @@ i.icon.centerlock { } } +.lines-code code { + white-space: pre; +} + .blame .lines-num { padding: 0 !important; background-color: #f5f5f5;