Skip to content

Commit ffca29f

Browse files
committed
fix
1 parent 5feb1a6 commit ffca29f

File tree

5 files changed

+37
-17
lines changed

5 files changed

+37
-17
lines changed

modules/markup/html_link.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
"code.gitea.io/gitea/modules/markup/common"
12+
"code.gitea.io/gitea/modules/util"
1213

1314
"golang.org/x/net/html"
1415
"golang.org/x/net/html/atom"
@@ -171,6 +172,10 @@ func linkProcessor(ctx *RenderContext, node *html.Node) {
171172
}
172173

173174
uri := node.Data[m[0]:m[1]]
175+
remaining := node.Data[m[1]:]
176+
if util.IsLikelySplitLeftEnding(remaining) {
177+
return
178+
}
174179
replaceContent(node, m[0], m[1], createLink(ctx, uri, uri, "" /*link*/))
175180
node = node.NextSibling.NextSibling
176181
}

modules/markup/html_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ func TestRender_links(t *testing.T) {
206206
test(
207207
"ftps://gitea.com",
208208
`<p>ftps://gitea.com</p>`)
209+
210+
t.Run("LinkSplit", func(t *testing.T) {
211+
input := "http://10.1.2.3"
212+
input, _ = util.SplitStringAtByteN(input, 12)
213+
assert.Equal(t, "http://10…", input)
214+
test(input, "<p>http://10…</p>")
215+
})
209216
}
210217

211218
func TestRender_email(t *testing.T) {

modules/util/string.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
package util
55

6-
import "unsafe"
6+
import (
7+
"strings"
8+
"unsafe"
9+
)
710

811
func isSnakeCaseUpper(c byte) bool {
912
return 'A' <= c && c <= 'Z'
@@ -95,3 +98,15 @@ func UnsafeBytesToString(b []byte) string {
9598
func UnsafeStringToBytes(s string) []byte {
9699
return unsafe.Slice(unsafe.StringData(s), len(s))
97100
}
101+
102+
// SplitTrimSpace splits the string at given separator and trims leading and trailing space
103+
func SplitTrimSpace(input, sep string) []string {
104+
input = strings.TrimSpace(input)
105+
var stringList []string
106+
for _, s := range strings.Split(input, sep) {
107+
if s = strings.TrimSpace(s); s != "" {
108+
stringList = append(stringList, s)
109+
}
110+
}
111+
return stringList
112+
}

modules/util/string_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,8 @@ func TestToSnakeCase(t *testing.T) {
4545
assert.Equal(t, expected, ToSnakeCase(input))
4646
}
4747
}
48+
49+
func TestSplitTrimSpace(t *testing.T) {
50+
assert.Equal(t, []string{"a", "b", "c"}, SplitTrimSpace("a\nb\nc", "\n"))
51+
assert.Equal(t, []string{"a", "b"}, SplitTrimSpace("\r\na\n\r\nb\n\n", "\n"))
52+
}

modules/util/truncate.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ const (
1414
asciiEllipsis = "..."
1515
)
1616

17+
func IsLikelySplitLeftEnding(s string) bool {
18+
return strings.HasPrefix(s, utf8Ellipsis) || strings.HasPrefix(s, asciiEllipsis)
19+
}
20+
1721
// SplitStringAtByteN splits a string at byte n accounting for rune boundaries. (Combining characters are not accounted for.)
1822
func SplitStringAtByteN(input string, n int) (left, right string) {
1923
if len(input) <= n {
@@ -38,19 +42,3 @@ func SplitStringAtByteN(input string, n int) (left, right string) {
3842

3943
return input[:end] + utf8Ellipsis, utf8Ellipsis + input[end:]
4044
}
41-
42-
// SplitTrimSpace splits the string at given separator and trims leading and trailing space
43-
func SplitTrimSpace(input, sep string) []string {
44-
// Trim initial leading & trailing space
45-
input = strings.TrimSpace(input)
46-
// replace CRLF with LF
47-
input = strings.ReplaceAll(input, "\r\n", "\n")
48-
49-
var stringList []string
50-
for _, s := range strings.Split(input, sep) {
51-
// trim leading and trailing space
52-
stringList = append(stringList, strings.TrimSpace(s))
53-
}
54-
55-
return stringList
56-
}

0 commit comments

Comments
 (0)