From 9fe4b7b6920c9760262ad8e77b7ed50ee8f7d464 Mon Sep 17 00:00:00 2001 From: mrsdizzie <info@mrsdizzie.com> Date: Sun, 18 Oct 2020 12:51:38 -0500 Subject: [PATCH] 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> --- services/gitdiff/gitdiff.go | 6 +++--- services/gitdiff/gitdiff_test.go | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 91105399db..3aacc830f0 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -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) } } diff --git a/services/gitdiff/gitdiff_test.go b/services/gitdiff/gitdiff_test.go index 64cd4f1c21..bb7cb40313 100644 --- a/services/gitdiff/gitdiff_test.go +++ b/services/gitdiff/gitdiff_test.go @@ -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\">"</span><span class=\"s2\">// </span><span class=\"s2\">"</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\">"</span><span class=\"s2\">// </span><span class=\"s2\">"</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) {