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">`) | 	removedCodePrefix = []byte(`<span class="removed-code">`) | ||||||
| 	codeTagSuffix     = []byte(`</span>`) | 	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 { | func diffToHTML(fileName string, diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTML { | ||||||
| 	buf := bytes.NewBuffer(nil) | 	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) | 				diffs[i].Text = strings.TrimSuffix(diffs[i].Text, addSpan) | ||||||
| 			} | 			} | ||||||
| 			buf.Write(addedCodePrefix) | 			buf.Write(addedCodePrefix) | ||||||
| 			buf.WriteString(getLineContent(diffs[i].Text)) | 			buf.WriteString(diffs[i].Text) | ||||||
| 			buf.Write(codeTagSuffix) | 			buf.Write(codeTagSuffix) | ||||||
| 		case diffs[i].Type == diffmatchpatch.DiffDelete && lineType == DiffLineDel: | 		case diffs[i].Type == diffmatchpatch.DiffDelete && lineType == DiffLineDel: | ||||||
| 			if len(addSpan) > 0 { | 			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) | 				diffs[i].Text = strings.TrimSuffix(diffs[i].Text, addSpan) | ||||||
| 			} | 			} | ||||||
| 			buf.Write(removedCodePrefix) | 			buf.Write(removedCodePrefix) | ||||||
| 			buf.WriteString(getLineContent(diffs[i].Text)) | 			buf.WriteString(diffs[i].Text) | ||||||
| 			buf.Write(codeTagSuffix) | 			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.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>"}, | 		{Type: dmp.DiffEqual, Text: "</span><span class=\"p\">,</span> <span class=\"kc\">false</span><span class=\"p\">)</span>"}, | ||||||
| 	}, DiffLineAdd)) | 	}, 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) { | func TestParsePatch_singlefile(t *testing.T) { | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue