Alternative fix for HTML diff entity split (#13425)
* Alternative fix for HTML diff entity split This commit both reverts PR #13357 and uses the exiting implementation alredy used for spans to fix the same issue. That PR duplicates most of logic that is already present elsewhere and still was failing for some cases. This should be simpler as it uses the existing logic that already works for <span>s being split apart. Added both test cases as well. * Update gitdiff_test.go * fmt * entity can have uppercase letter, also add detailed comment per @zeripath
This commit is contained in:
parent
dd882f6a69
commit
d7e0983780
2 changed files with 45 additions and 114 deletions
|
@ -15,7 +15,6 @@ import (
|
|||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"github.com/sergi/go-diff/diffmatchpatch"
|
||||
dmp "github.com/sergi/go-diff/diffmatchpatch"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/ini.v1"
|
||||
|
@ -27,35 +26,6 @@ func assertEqual(t *testing.T, s1 string, s2 template.HTML) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestUnsplitEntities(t *testing.T) {
|
||||
left := "sh "useradd -u 111 jenkins""
|
||||
right := "sh 'useradd -u $(stat -c "%u" .gitignore) jenkins'"
|
||||
diffRecord := diffMatchPatch.DiffMain(left, right, true)
|
||||
diffRecord = diffMatchPatch.DiffCleanupEfficiency(diffRecord)
|
||||
|
||||
// Now we need to clean up the split entities
|
||||
diffRecord = unsplitEntities(diffRecord)
|
||||
diffRecord = diffMatchPatch.DiffCleanupEfficiency(diffRecord)
|
||||
|
||||
leftRecombined := ""
|
||||
rightRecombined := ""
|
||||
for _, record := range diffRecord {
|
||||
assert.False(t, unterminatedEntityRE.MatchString(record.Text), "")
|
||||
switch record.Type {
|
||||
case diffmatchpatch.DiffDelete:
|
||||
leftRecombined += record.Text
|
||||
case diffmatchpatch.DiffInsert:
|
||||
rightRecombined += record.Text
|
||||
default:
|
||||
leftRecombined += record.Text
|
||||
rightRecombined += record.Text
|
||||
}
|
||||
}
|
||||
|
||||
assert.EqualValues(t, left, leftRecombined)
|
||||
assert.EqualValues(t, right, rightRecombined)
|
||||
}
|
||||
|
||||
func TestDiffToHTML(t *testing.T) {
|
||||
setting.Cfg = ini.Empty()
|
||||
assertEqual(t, "foo <span class=\"added-code\">bar</span> biz", diffToHTML("", []dmp.Diff{
|
||||
|
@ -113,6 +83,21 @@ func TestDiffToHTML(t *testing.T) {
|
|||
{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))
|
||||
|
||||
assertEqual(t, "sh <span class=\"added-code\">'useradd -u $(stat -c "%u" .gitignore) jenkins</span>'", diffToHTML("", []dmp.Diff{
|
||||
{Type: dmp.DiffEqual, Text: "sh "},
|
||||
{Type: dmp.DiffDelete, Text: "4;useradd -u 111 jenkins""},
|
||||
{Type: dmp.DiffInsert, Text: "9;useradd -u $(stat -c "%u" .gitignore) jenkins'"},
|
||||
{Type: dmp.DiffEqual, Text: ";"},
|
||||
}, DiffLineAdd))
|
||||
|
||||
assertEqual(t, "<span class=\"x\"> <h<span class=\"added-code\">4 class=</span><span class=\"added-code\">"release-list-title df ac"</span>></span>", diffToHTML("", []dmp.Diff{
|
||||
{Type: dmp.DiffEqual, Text: "<span class=\"x\"> <h"},
|
||||
{Type: dmp.DiffInsert, Text: "4 class=&#"},
|
||||
{Type: dmp.DiffEqual, Text: "3"},
|
||||
{Type: dmp.DiffInsert, Text: "4;release-list-title df ac""},
|
||||
{Type: dmp.DiffEqual, Text: "></span>"},
|
||||
}, DiffLineAdd))
|
||||
}
|
||||
|
||||
func TestParsePatch_singlefile(t *testing.T) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue