Skip to content

Commit 9fe4b7b

Browse files
mrsdizziezeripath
andauthoredOct 18, 2020
Fix error in diff html rendering (go-gitea#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 go-gitea#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>
1 parent 5e34d3d commit 9fe4b7b

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed
 

‎services/gitdiff/gitdiff.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ var (
181181
removedCodePrefix = []byte(`<span class="removed-code">`)
182182
codeTagSuffix = []byte(`</span>`)
183183
)
184-
var addSpanRegex = regexp.MustCompile(`<span [class="[a-z]*]*$`)
184+
var addSpanRegex = regexp.MustCompile(`<span\s*[a-z="]*$`)
185185

186186
func diffToHTML(fileName string, diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTML {
187187
buf := bytes.NewBuffer(nil)
@@ -221,7 +221,7 @@ func diffToHTML(fileName string, diffs []diffmatchpatch.Diff, lineType DiffLineT
221221
diffs[i].Text = strings.TrimSuffix(diffs[i].Text, addSpan)
222222
}
223223
buf.Write(addedCodePrefix)
224-
buf.WriteString(getLineContent(diffs[i].Text))
224+
buf.WriteString(diffs[i].Text)
225225
buf.Write(codeTagSuffix)
226226
case diffs[i].Type == diffmatchpatch.DiffDelete && lineType == DiffLineDel:
227227
if len(addSpan) > 0 {
@@ -238,7 +238,7 @@ func diffToHTML(fileName string, diffs []diffmatchpatch.Diff, lineType DiffLineT
238238
diffs[i].Text = strings.TrimSuffix(diffs[i].Text, addSpan)
239239
}
240240
buf.Write(removedCodePrefix)
241-
buf.WriteString(getLineContent(diffs[i].Text))
241+
buf.WriteString(diffs[i].Text)
242242
buf.Write(codeTagSuffix)
243243
}
244244
}

‎services/gitdiff/gitdiff_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ func TestDiffToHTML(t *testing.T) {
7474
{Type: dmp.DiffInsert, Text: "lass=\"p\">,</span> <span class=\"kc\">true</span><span class=\"p\">,</span> <span class=\"nx\">attrs"},
7575
{Type: dmp.DiffEqual, Text: "</span><span class=\"p\">,</span> <span class=\"kc\">false</span><span class=\"p\">)</span>"},
7676
}, DiffLineAdd))
77+
78+
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{
79+
{Type: dmp.DiffEqual, Text: "<span class=\"k\">print</span>"},
80+
{Type: dmp.DiffInsert, Text: "<span"},
81+
{Type: dmp.DiffEqual, Text: " "},
82+
{Type: dmp.DiffInsert, Text: "class=\"p\">(</span>"},
83+
{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>"},
84+
{Type: dmp.DiffInsert, Text: "<span class=\"p\">)</span>"},
85+
}, DiffLineAdd))
7786
}
7887

7988
func TestParsePatch_singlefile(t *testing.T) {

0 commit comments

Comments
 (0)
Please sign in to comment.