Skip to content

Commit 447236e

Browse files
committed
- readding call to highligh.Code in diffWithHighlight
- reverting tests to be closer to original
1 parent 5b0adf2 commit 447236e

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

services/gitdiff/highlightdiff.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package gitdiff
66
import (
77
"strings"
88

9+
"code.gitea.io/gitea/modules/highlight"
910
"github.com/sergi/go-diff/diffmatchpatch"
1011
)
1112

@@ -88,8 +89,11 @@ func (hcd *highlightCodeDiff) diffWithHighlight(codeA, codeB string) []diffmatch
8889
hcd.collectUsedRunes(codeA)
8990
hcd.collectUsedRunes(codeB)
9091

91-
convertedCodeA := hcd.convertToPlaceholders(codeA)
92-
convertedCodeB := hcd.convertToPlaceholders(codeB)
92+
highlightCodeA, _ := highlight.Code("", "", codeA)
93+
highlightCodeB, _ := highlight.Code("", "", codeB)
94+
95+
convertedCodeA := hcd.convertToPlaceholders(string(highlightCodeA))
96+
convertedCodeB := hcd.convertToPlaceholders(string(highlightCodeB))
9397

9498
diffs := diffMatchPatch.DiffMain(convertedCodeA, convertedCodeB, true)
9599
diffs = diffMatchPatch.DiffCleanupEfficiency(diffs)

services/gitdiff/highlightdiff_test.go

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package gitdiff
55

66
import (
77
"fmt"
8+
"strings"
89
"testing"
910

1011
"github.com/sergi/go-diff/diffmatchpatch"
@@ -18,13 +19,11 @@ func TestDiffWithHighlight(t *testing.T) {
1819
" run(db)\n",
1920
)
2021

21-
expected := "\t\trun(<span class=\"removed-code\">'<>'</></span>)\n"
22-
22+
expected := ` run(<span class="removed-code">&#39;&lt;&gt;&#39;</span>)`
2323
output := diffToHTML(nil, diffs, DiffLineDel)
2424
assert.Equal(t, expected, output)
2525

26-
expected = ` run(<span class="added-code">db</span>)
27-
`
26+
expected = ` run(<span class="added-code">db</span>)`
2827
output = diffToHTML(nil, diffs, DiffLineAdd)
2928
assert.Equal(t, expected, output)
3029

@@ -33,6 +32,14 @@ func TestDiffWithHighlight(t *testing.T) {
3332
hcd.placeholderTokenMap['C'] = "</span>"
3433
diff := diffmatchpatch.Diff{}
3534

35+
diff.Text = "OC"
36+
hcd.recoverOneDiff(&diff)
37+
assert.Equal(t, "<span></span>", diff.Text)
38+
39+
diff.Text = "O"
40+
hcd.recoverOneDiff(&diff)
41+
assert.Equal(t, "<span></span>", diff.Text)
42+
3643
diff.Text = "C"
3744
hcd.recoverOneDiff(&diff)
3845
assert.Equal(t, "", diff.Text)
@@ -47,7 +54,7 @@ func TestDiffWithHighlightPlaceholder(t *testing.T) {
4754
assert.Equal(t, "", hcd.placeholderTokenMap[0x00100000])
4855
assert.Equal(t, "", hcd.placeholderTokenMap[0x0010FFFD])
4956

50-
expected := fmt.Sprintf(`a='<span class="removed-code">%s</span>'`, "\U00100000")
57+
expected := fmt.Sprintf(`a=&#39;<span class="removed-code">%s</span>&#39;`, "\U00100000")
5158
output := diffToHTML(hcd.lineWrapperTags, diffs, DiffLineDel)
5259
assert.Equal(t, expected, output)
5360

@@ -56,7 +63,7 @@ func TestDiffWithHighlightPlaceholder(t *testing.T) {
5663
"a='\U00100000'",
5764
"a='\U0010FFFD'",
5865
)
59-
expected = fmt.Sprintf(`a='<span class="added-code">%s</span>'`, "\U0010FFFD")
66+
expected = fmt.Sprintf(`a=&#39;<span class="added-code">%s</span>&#39;`, "\U0010FFFD")
6067
output = diffToHTML(nil, diffs, DiffLineAdd)
6168
assert.Equal(t, expected, output)
6269
}
@@ -69,20 +76,44 @@ func TestDiffWithHighlightPlaceholderExhausted(t *testing.T) {
6976
``,
7077
)
7178
output := diffToHTML(nil, diffs, DiffLineDel)
72-
expected := `<span class="removed-code">'</span>`
79+
expected := fmt.Sprintf(`<span class="removed-code">%s#39;</span>`, "\uFFFD")
7380
assert.Equal(t, expected, output)
7481

7582
hcd = newHighlightCodeDiff()
7683
hcd.placeholderMaxCount = 0
7784
diffs = hcd.diffWithHighlight(
78-
"a this_is_not_html_at_this_point b",
79-
"a this_is_is_still_not_html_at_this_point_its_just_a_string b",
85+
"a < b",
86+
"a > b",
8087
)
8188
output = diffToHTML(nil, diffs, DiffLineDel)
82-
expected = "a this_is_not_html_at_this_point b"
89+
expected = fmt.Sprintf(`a %s<span class="removed-code">l</span>t; b`, "\uFFFD")
8390
assert.Equal(t, expected, output)
8491

8592
output = diffToHTML(nil, diffs, DiffLineAdd)
86-
expected = "a this_is_<span class=\"added-code\">is_still_</span>not_html_at_this_point<span class=\"added-code\">_its_just_a_string</span> b"
93+
expected = fmt.Sprintf(`a %s<span class="added-code">g</span>t; b`, "\uFFFD")
8794
assert.Equal(t, expected, output)
8895
}
96+
97+
func TestDiffWithHighlightTagMatch(t *testing.T) {
98+
totalOverflow := 0
99+
for i := 0; i < 100; i++ {
100+
hcd := newHighlightCodeDiff()
101+
hcd.placeholderMaxCount = i
102+
diffs := hcd.diffWithHighlight(
103+
"a='1'",
104+
"b='2'",
105+
)
106+
totalOverflow += hcd.placeholderOverflowCount
107+
108+
output := diffToHTML(nil, diffs, DiffLineDel)
109+
c1 := strings.Count(output, "<span")
110+
c2 := strings.Count(output, "</span")
111+
assert.Equal(t, c1, c2)
112+
113+
output = diffToHTML(nil, diffs, DiffLineAdd)
114+
c1 = strings.Count(output, "<span")
115+
c2 = strings.Count(output, "</span")
116+
assert.Equal(t, c1, c2)
117+
}
118+
assert.NotZero(t, totalOverflow)
119+
}

0 commit comments

Comments
 (0)