mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-31 11:28:24 +00:00 
			
		
		
		
	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:
		| @@ -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) | ||||
| 		} | ||||
| 	} | ||||
|   | ||||
| @@ -74,6 +74,15 @@ func TestDiffToHTML(t *testing.T) { | ||||
| 		{Type: dmp.DiffInsert, Text: "lass=\"p\">,</span> <span class=\"kc\">true</span><span class=\"p\">,</span> <span class=\"nx\">attrs"}, | ||||
| 		{Type: dmp.DiffEqual, Text: "</span><span class=\"p\">,</span> <span class=\"kc\">false</span><span class=\"p\">)</span>"}, | ||||
| 	}, DiffLineAdd)) | ||||
|  | ||||
| 	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\">"</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><span class=\"added-code\"><span class=\"p\">)</span></span>", diffToHTML("", []dmp.Diff{ | ||||
| 		{Type: dmp.DiffEqual, Text: "<span class=\"k\">print</span>"}, | ||||
| 		{Type: dmp.DiffInsert, Text: "<span"}, | ||||
| 		{Type: dmp.DiffEqual, Text: " "}, | ||||
| 		{Type: dmp.DiffInsert, Text: "class=\"p\">(</span>"}, | ||||
| 		{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)) | ||||
| } | ||||
|  | ||||
| func TestParsePatch_singlefile(t *testing.T) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user