Skip to content

Commit f21f422

Browse files
committed
demo for why the tags are all closed
1 parent c9a1566 commit f21f422

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

services/gitdiff/gitdiff.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ type highlightCodeDiff struct {
295295
placeholderTagMap map[rune]string
296296
tagPlaceholderMap map[string]rune
297297

298+
placeholderOverflowCount int
299+
298300
lineWrapperTags []string
299301
}
300302

@@ -410,9 +412,11 @@ func (hcd *highlightCodeDiff) convertToPlaceholders(htmlCode string) string {
410412

411413
if placeholder != 0 {
412414
res.WriteRune(placeholder) // use the placeholder to replace the tag
415+
} else {
416+
// unfortunately, all private use runes has been exhausted, no more placeholder could be used, no more converting
417+
// usually, the exhausting won't occur in real cases, the magnitude of used placeholders is not larger than that of the CSS classes outputted by chroma.
418+
hcd.placeholderOverflowCount++
413419
}
414-
// else: unfortunately, all private use runes has been exhausted, no more placeholder could be used, no more converting
415-
// usually, the exhausting won't occur in real cases, the magnitude of used placeholders is not larger than that of the CSS classes outputted by chroma.
416420
}
417421
// write the remaining string
418422
res.WriteString(htmlCode)

services/gitdiff/gitdiff_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,29 @@ func TestDiffWithHighlightPlaceholder(t *testing.T) {
684684
expected = fmt.Sprintf(`<span class="nx">a</span><span class="o">=</span><span class="s1">&#39;</span><span class="added-code">%s</span>&#39;`, "\uF8FF")
685685
output = diffToHTML(nil, diffs, DiffLineAdd)
686686
assert.Equal(t, expected, output)
687+
688+
totalOverflow := 0
689+
for i := 0; i < 100; i++ {
690+
hcd = newHighlightCodeDiff()
691+
hcd.placeholderMaxCount = i
692+
diffs = hcd.diffWithHighlight(
693+
"main.js", "",
694+
"a='1'",
695+
"b='2'",
696+
)
697+
totalOverflow += hcd.placeholderOverflowCount
698+
699+
output = diffToHTML(nil, diffs, DiffLineDel)
700+
c1 := strings.Count(output, "<span")
701+
c2 := strings.Count(output, "</span")
702+
assert.Equal(t, c1, c2)
703+
704+
output = diffToHTML(nil, diffs, DiffLineAdd)
705+
c1 = strings.Count(output, "<span")
706+
c2 = strings.Count(output, "</span")
707+
assert.Equal(t, c1, c2)
708+
}
709+
assert.NotZero(t, totalOverflow)
687710
}
688711

689712
func TestNoCrashes(t *testing.T) {

0 commit comments

Comments
 (0)