Fix error in diff html rendering (#13191)

* Fix error in diff html rendering

Was missing an optional whitespace check in regex. Also noticed a rare case where diff.Type == Equal would be empty and thus get a newline attached. Fixed that too.

Fixes #13177

* Update services/gitdiff/gitdiff.go

Co-authored-by: zeripath <art27@cantab.net>

* Update gitdiff_test.go

* fmt

Co-authored-by: zeripath <art27@cantab.net>
mj-v1.14.3
mrsdizzie 4 years ago committed by GitHub
parent 5e34d3d2c0
commit 9fe4b7b692
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -181,7 +181,7 @@ var (
removedCodePrefix = []byte(`<span class="removed-code">`)
codeTagSuffix = []byte(`</span>`)
)
var addSpanRegex = regexp.MustCompile(`<span [class="[a-z]*]*$`)
var addSpanRegex = regexp.MustCompile(`<span\s*[a-z="]*$`)
func diffToHTML(fileName string, diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTML {
buf := bytes.NewBuffer(nil)
@ -221,7 +221,7 @@ func diffToHTML(fileName string, diffs []diffmatchpatch.Diff, lineType DiffLineT
diffs[i].Text = strings.TrimSuffix(diffs[i].Text, addSpan)
}
buf.Write(addedCodePrefix)
buf.WriteString(getLineContent(diffs[i].Text))
buf.WriteString(diffs[i].Text)
buf.Write(codeTagSuffix)
case diffs[i].Type == diffmatchpatch.DiffDelete && lineType == DiffLineDel:
if len(addSpan) > 0 {
@ -238,7 +238,7 @@ func diffToHTML(fileName string, diffs []diffmatchpatch.Diff, lineType DiffLineT
diffs[i].Text = strings.TrimSuffix(diffs[i].Text, addSpan)
}
buf.Write(removedCodePrefix)
buf.WriteString(getLineContent(diffs[i].Text))
buf.WriteString(diffs[i].Text)
buf.Write(codeTagSuffix)
}
}

@ -74,6 +74,15 @@ func TestDiffToHTML(t *testing.T) {
{Type: dmp.DiffInsert, Text: "lass=\"p\">,</span> <span class=\"kc\">true</span><span class=\"p\">,</span> <span class=\"nx\">attrs"},
{Type: dmp.DiffEqual, Text: "</span><span class=\"p\">,</span> <span class=\"kc\">false</span><span class=\"p\">)</span>"},
}, DiffLineAdd))
assertEqual(t, "<span class=\"k\">print</span><span class=\"added-code\"></span><span class=\"added-code\"><span class=\"p\">(</span></span><span class=\"sa\"></span><span class=\"s2\">&#34;</span><span class=\"s2\">// </span><span class=\"s2\">&#34;</span><span class=\"p\">,</span> <span class=\"n\">sys</span><span class=\"o\">.</span><span class=\"n\">argv</span><span class=\"added-code\"><span class=\"p\">)</span></span>", diffToHTML("", []dmp.Diff{
{Type: dmp.DiffEqual, Text: "<span class=\"k\">print</span>"},
{Type: dmp.DiffInsert, Text: "<span"},
{Type: dmp.DiffEqual, Text: " "},
{Type: dmp.DiffInsert, Text: "class=\"p\">(</span>"},
{Type: dmp.DiffEqual, Text: "<span class=\"sa\"></span><span class=\"s2\">&#34;</span><span class=\"s2\">// </span><span class=\"s2\">&#34;</span><span class=\"p\">,</span> <span class=\"n\">sys</span><span class=\"o\">.</span><span class=\"n\">argv</span>"},
{Type: dmp.DiffInsert, Text: "<span class=\"p\">)</span>"},
}, DiffLineAdd))
}
func TestParsePatch_singlefile(t *testing.T) {

Loading…
Cancel
Save