mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 19:06:18 +01:00 
			
		
		
		
	When creating line diffs do not split within an html entity (#13357)
* When creating line diffs do not split within an html entity Fix #13342 Signed-off-by: Andrew Thornton <art27@cantab.net> * Add test case Signed-off-by: Andrew Thornton <art27@cantab.net> * improve test Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
		| @@ -15,6 +15,7 @@ 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" | ||||
| @@ -26,6 +27,35 @@ 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{ | ||||
|   | ||||
		Reference in New Issue
	
	Block a user