forked from forgejo/forgejo
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>
This commit is contained in:
parent
5e34d3d2c0
commit
9fe4b7b692
2 changed files with 12 additions and 3 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue