History

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

mrsdizzie committed 6 years ago

Commit d7e09831 parent dd882f62 files · +45 −114Browse files

Showing the changes to services/gitdiff/gitdiff_test.go (1 of 2 files). Show all

Unified · Split

services/gitdiff/gitdiff_test.go+15−30
@@ -15,7 +15,6 @@ import ( · 14 unmodified lines
1515 "code.gitea.io/gitea/models"
1616 "code.gitea.io/gitea/modules/git"
1717 "code.gitea.io/gitea/modules/setting"
18− "github.com/sergi/go-diff/diffmatchpatch"
1918 dmp "github.com/sergi/go-diff/diffmatchpatch"
2019 "github.com/stretchr/testify/assert"
2120 "gopkg.in/ini.v1"
@@ -27,35 +26,6 @@ func assertEqual(t *testing.T, s1 string, s2 template.HTML) { · 5 unmodified lines
2726 }
2827 }
2928
30−func TestUnsplitEntities(t *testing.T) {
31− left := "sh &#34;useradd -u 111 jenkins&#34;"
32− right := "sh &#39;useradd -u $(stat -c &#34;%u&#34; .gitignore) jenkins&#39;"
33− diffRecord := diffMatchPatch.DiffMain(left, right, true)
34− diffRecord = diffMatchPatch.DiffCleanupEfficiency(diffRecord)
35−
36− // Now we need to clean up the split entities
37− diffRecord = unsplitEntities(diffRecord)
38− diffRecord = diffMatchPatch.DiffCleanupEfficiency(diffRecord)
39−
40− leftRecombined := ""
41− rightRecombined := ""
42− for _, record := range diffRecord {
43− assert.False(t, unterminatedEntityRE.MatchString(record.Text), "")
44− switch record.Type {
45− case diffmatchpatch.DiffDelete:
46− leftRecombined += record.Text
47− case diffmatchpatch.DiffInsert:
48− rightRecombined += record.Text
49− default:
50− leftRecombined += record.Text
51− rightRecombined += record.Text
52− }
53− }
54−
55− assert.EqualValues(t, left, leftRecombined)
56− assert.EqualValues(t, right, rightRecombined)
57−}
58−
5929 func TestDiffToHTML(t *testing.T) {
6030 setting.Cfg = ini.Empty()
6131 assertEqual(t, "foo <span class=\"added-code\">bar</span> biz", diffToHTML("", []dmp.Diff{
@@ -113,6 +83,21 @@ func TestDiffToHTML(t *testing.T) { · 51 unmodified lines
11383 {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>"},
11484 {Type: dmp.DiffInsert, Text: "<span class=\"p\">)</span>"},
11585 }, DiffLineAdd))
86+
87+ assertEqual(t, "sh <span class=\"added-code\">&#39;useradd -u $(stat -c &#34;%u&#34; .gitignore) jenkins</span>&#39;", diffToHTML("", []dmp.Diff{
88+ {Type: dmp.DiffEqual, Text: "sh &#3"},
89+ {Type: dmp.DiffDelete, Text: "4;useradd -u 111 jenkins&#34"},
90+ {Type: dmp.DiffInsert, Text: "9;useradd -u $(stat -c &#34;%u&#34; .gitignore) jenkins&#39"},
91+ {Type: dmp.DiffEqual, Text: ";"},
92+ }, DiffLineAdd))
93+
94+ assertEqual(t, "<span class=\"x\"> &lt;h<span class=\"added-code\">4 class=</span><span class=\"added-code\">&#34;release-list-title df ac&#34;</span>&gt;</span>", diffToHTML("", []dmp.Diff{
95+ {Type: dmp.DiffEqual, Text: "<span class=\"x\"> &lt;h"},
96+ {Type: dmp.DiffInsert, Text: "4 class=&#"},
97+ {Type: dmp.DiffEqual, Text: "3"},
98+ {Type: dmp.DiffInsert, Text: "4;release-list-title df ac&#34;"},
99+ {Type: dmp.DiffEqual, Text: "&gt;</span>"},
100+ }, DiffLineAdd))
116101 }
117102
118103 func TestParsePatch_singlefile(t *testing.T) {