Skip to content

Commit

Permalink
always output the code/name part of the html entities if the placehol…
Browse files Browse the repository at this point in the history
…ders are exhausted.
  • Loading branch information
wxiaoguang committed Jul 9, 2022
1 parent dc74baf commit 4afb1cc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion services/gitdiff/highlightdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ func (hcd *highlightCodeDiff) convertToPlaceholders(htmlCode string) string {
hcd.placeholderOverflowCount++
if strings.HasPrefix(token, "&") {
// when the token is a html entity, something must be outputted even if there is no placeholder.
res.WriteRune(0xFFFD) // replacement character TODO: how to handle this case more gracefully?
res.WriteRune(0xFFFD) // replacement character TODO: how to handle this case more gracefully?
res.WriteString(token[1:]) // still output the entity code part, otherwise there will be no diff result.
}
}
}
Expand Down
17 changes: 16 additions & 1 deletion services/gitdiff/highlightdiff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,22 @@ func TestDiffWithHighlightPlaceholderExhausted(t *testing.T) {
``,
)
output := diffToHTML(nil, diffs, DiffLineDel)
expected := fmt.Sprintf(`<span class="removed-code">%s</span>`, "\uFFFD")
expected := fmt.Sprintf(`<span class="removed-code">%s#39;</span>`, "\uFFFD")
assert.Equal(t, expected, output)

hcd = newHighlightCodeDiff()
hcd.placeholderMaxCount = 0
diffs = hcd.diffWithHighlight(
"main.js", "",
"a < b",
"a > b",
)
output = diffToHTML(nil, diffs, DiffLineDel)
expected = fmt.Sprintf(`a %s<span class="removed-code">l</span>t; b`, "\uFFFD")
assert.Equal(t, expected, output)

output = diffToHTML(nil, diffs, DiffLineAdd)
expected = fmt.Sprintf(`a %s<span class="added-code">g</span>t; b`, "\uFFFD")
assert.Equal(t, expected, output)
}

Expand Down

0 comments on commit 4afb1cc

Please sign in to comment.