diff options
author | silverwind <me@silverwind.io> | 2019-06-26 23:35:07 +0200 |
---|---|---|
committer | zeripath <art27@cantab.net> | 2019-06-26 22:35:07 +0100 |
commit | da230412574daa9697b4cef24c7be6209b8884dc (patch) | |
tree | 24c1a7ba637c04fe9512643079572afe63d89d39 /models | |
parent | edc94c70413048107ea728ff330f32ca3de6df88 (diff) | |
download | gitea-da230412574daa9697b4cef24c7be6209b8884dc.tar.gz gitea-da230412574daa9697b4cef24c7be6209b8884dc.zip |
fix extra newlines when copying from diff in Firefox (#7288)
* fix extra newlines when copying from diff
See https://bugzilla.mozilla.org/show_bug.cgi?id=1273836
Basically, the <pre><code> seems to add a forced newline that is not
possible to get rid of via CSS, so I replaced it with just a <code>.
Secondly, .lines-type-marker also forced a newline in the copied text,
but that was possible to get rid of via user-select.
Safari still has a extraneous newline in the copied text of unknown
origin, but this should not block stop this PR.
* simplify .line-type-marker
* fix selector
* remove erronous ^^^
* Fix empty split diff
* Fix arc-theme-green
* fix add comment
* ensure line-num is copied too
* Update templates/repo/diff/box.tmpl
Co-Authored-By: zeripath <art27@cantab.net>
* attempt to fix safari via removing <code>
* remove useless whitespace at the end of 'class'
* remove inter-tag whitespace for code <td>s
* more inter-tag removal
* final inter-tag removal
* attempt to fix empty line copy
* move and comment getLineContent
* fix golint
* make background grey for missing added code
Diffstat (limited to 'models')
-rw-r--r-- | models/git_diff.go | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/models/git_diff.go b/models/git_diff.go index bc79a73a4e..2f48f1b6fa 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -88,6 +88,14 @@ func (d *DiffLine) GetLineTypeMarker() string { return "" } +// escape a line's content or return <br> needed for copy/paste purposes +func getLineContent(content string) string { + if len(content) > 0 { + return html.EscapeString(content) + } + return "<br>" +} + // DiffSection represents a section of a DiffFile. type DiffSection struct { Name string @@ -107,14 +115,14 @@ func diffToHTML(diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTM switch { case diffs[i].Type == diffmatchpatch.DiffInsert && lineType == DiffLineAdd: buf.Write(addedCodePrefix) - buf.WriteString(html.EscapeString(diffs[i].Text)) + buf.WriteString(getLineContent(diffs[i].Text)) buf.Write(codeTagSuffix) case diffs[i].Type == diffmatchpatch.DiffDelete && lineType == DiffLineDel: buf.Write(removedCodePrefix) - buf.WriteString(html.EscapeString(diffs[i].Text)) + buf.WriteString(getLineContent(diffs[i].Text)) buf.Write(codeTagSuffix) case diffs[i].Type == diffmatchpatch.DiffEqual: - buf.WriteString(html.EscapeString(diffs[i].Text)) + buf.WriteString(getLineContent(diffs[i].Text)) } } @@ -173,7 +181,7 @@ func init() { // GetComputedInlineDiffFor computes inline diff for the given line. func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) template.HTML { if setting.Git.DisableDiffHighlight { - return template.HTML(html.EscapeString(diffLine.Content[1:])) + return template.HTML(getLineContent(diffLine.Content[1:])) } var ( compareDiffLine *DiffLine @@ -186,22 +194,22 @@ func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) tem case DiffLineAdd: compareDiffLine = diffSection.GetLine(DiffLineDel, diffLine.RightIdx) if compareDiffLine == nil { - return template.HTML(html.EscapeString(diffLine.Content[1:])) + return template.HTML(getLineContent(diffLine.Content[1:])) } diff1 = compareDiffLine.Content diff2 = diffLine.Content case DiffLineDel: compareDiffLine = diffSection.GetLine(DiffLineAdd, diffLine.LeftIdx) if compareDiffLine == nil { - return template.HTML(html.EscapeString(diffLine.Content[1:])) + return template.HTML(getLineContent(diffLine.Content[1:])) } diff1 = diffLine.Content diff2 = compareDiffLine.Content default: if strings.IndexByte(" +-", diffLine.Content[0]) > -1 { - return template.HTML(html.EscapeString(diffLine.Content[1:])) + return template.HTML(getLineContent(diffLine.Content[1:])) } - return template.HTML(html.EscapeString(diffLine.Content)) + return template.HTML(getLineContent(diffLine.Content)) } diffRecord := diffMatchPatch.DiffMain(diff1[1:], diff2[1:], true) |