diff options
author | zeripath <art27@cantab.net> | 2019-06-24 21:23:52 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-24 21:23:52 +0100 |
commit | 5908bb103003912bd0ba3cefa66f2ff815ee8d8e (patch) | |
tree | ec0f4ff99314aa4e38334b436e1aea287b27cb7b /models | |
parent | e07ff2f89064db8fa5c78a2a27a69d93183d10a4 (diff) | |
download | gitea-5908bb103003912bd0ba3cefa66f2ff815ee8d8e.tar.gz gitea-5908bb103003912bd0ba3cefa66f2ff815ee8d8e.zip |
Make diff line-marker non-selectable (#7279)
* Make diff line-marker non-selectable
* Move to use data-* as per @mrsdizzie
* fix missing line nums
* Add a minimum-width to force right-align of the line num
* Move line-type-marker into separate column
Diffstat (limited to 'models')
-rw-r--r-- | models/git_diff.go | 29 | ||||
-rw-r--r-- | models/git_diff_test.go | 4 |
2 files changed, 18 insertions, 15 deletions
diff --git a/models/git_diff.go b/models/git_diff.go index a6ea7306d4..29c424c11c 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -80,6 +80,14 @@ func (d *DiffLine) GetCommentSide() string { return d.Comments[0].DiffSide() } +// GetLineTypeMarker returns the line type marker +func (d *DiffLine) GetLineTypeMarker() string { + if strings.IndexByte(" +-", d.Content[0]) > -1 { + return d.Content[0:1] + } + return "" +} + // DiffSection represents a section of a DiffFile. type DiffSection struct { Name string @@ -87,22 +95,14 @@ type DiffSection struct { } var ( - addedCodePrefix = []byte("<span class=\"added-code\">") - removedCodePrefix = []byte("<span class=\"removed-code\">") - codeTagSuffix = []byte("</span>") + addedCodePrefix = []byte(`<span class="added-code">`) + removedCodePrefix = []byte(`<span class="removed-code">`) + codeTagSuffix = []byte(`</span>`) ) func diffToHTML(diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTML { buf := bytes.NewBuffer(nil) - // Reproduce signs which are cut for inline diff before. - switch lineType { - case DiffLineAdd: - buf.WriteByte('+') - case DiffLineDel: - buf.WriteByte('-') - } - for i := range diffs { switch { case diffs[i].Type == diffmatchpatch.DiffInsert && lineType == DiffLineAdd: @@ -186,18 +186,21 @@ 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)) + return template.HTML(html.EscapeString(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)) + return template.HTML(html.EscapeString(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(html.EscapeString(diffLine.Content)) } diff --git a/models/git_diff_test.go b/models/git_diff_test.go index deca7c8d4a..bf52095acf 100644 --- a/models/git_diff_test.go +++ b/models/git_diff_test.go @@ -18,14 +18,14 @@ func assertEqual(t *testing.T, s1 string, s2 template.HTML) { } func TestDiffToHTML(t *testing.T) { - assertEqual(t, "+foo <span class=\"added-code\">bar</span> biz", diffToHTML([]dmp.Diff{ + assertEqual(t, "foo <span class=\"added-code\">bar</span> biz", diffToHTML([]dmp.Diff{ {Type: dmp.DiffEqual, Text: "foo "}, {Type: dmp.DiffInsert, Text: "bar"}, {Type: dmp.DiffDelete, Text: " baz"}, {Type: dmp.DiffEqual, Text: " biz"}, }, DiffLineAdd)) - assertEqual(t, "-foo <span class=\"removed-code\">bar</span> biz", diffToHTML([]dmp.Diff{ + assertEqual(t, "foo <span class=\"removed-code\">bar</span> biz", diffToHTML([]dmp.Diff{ {Type: dmp.DiffEqual, Text: "foo "}, {Type: dmp.DiffDelete, Text: "bar"}, {Type: dmp.DiffInsert, Text: " baz"}, |