diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go index 5d66e4c32d40c..31448ccd632f2 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 80e6eb1ecdee0..d335661ffbf13 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 960710a9871b8..adb9184131ab3 100644 --- a/templates/repo/diff/box.tmpl +++ b/templates/repo/diff/box.tmpl @@ -139,10 +139,10 @@ {{else}}
{{$code | Safe}}
+ {{$code | Safe}}