forked from forgejo/forgejo
Use stricter boundaries for auto-link detection (#6522)
* Use stricter boundaries for auto-link detection Currently autolinks use \W for boundary detection which creates many situations of inserting links into places they don't belong (paths, URLs, UUIDs, etc...) This fixes that by replacing \W and only allowing these matches to touch an open paren or bracket (matching what seems to be Github behavior) in addition to whitespace and start of line. Similar for ending boundary as well. Fixes #6149 (and probably others) * Update test Replace incorrect test with a value that is a valid username, based on: "Username should contain only alphanumeric, dash ('-'), underscore ('_') and dot ('.') characters." * Also allow for period at the end Matching Github behavior * Fix email regex to work properly with specificed boundaries Create a specific capture group for email address and then use FindStringSubmatchIndex to allow for non-matching patterns as boundaries. * Add Tests Add tests for new behavior -- including tests for email addresses which were absent before.
This commit is contained in:
parent
5422f23ed8
commit
6293736d02
3 changed files with 70 additions and 11 deletions
|
@ -71,6 +71,7 @@ func TestRender_IssueIndexPattern(t *testing.T) {
|
|||
test("test#1234")
|
||||
test("#1234test")
|
||||
test(" test #1234test")
|
||||
test("/home/gitea/#1234")
|
||||
|
||||
// should not render issue mention without leading space
|
||||
test("test#54321 issue")
|
||||
|
@ -103,9 +104,11 @@ func TestRender_IssueIndexPattern2(t *testing.T) {
|
|||
test("#1234 test", "%s test", 1234)
|
||||
test("test #8 issue", "test %s issue", 8)
|
||||
test("test issue #1234", "test issue %s", 1234)
|
||||
test("fixes issue #1234.", "fixes issue %s.", 1234)
|
||||
|
||||
// should render mentions in parentheses
|
||||
// should render mentions in parentheses / brackets
|
||||
test("(#54321 issue)", "(%s issue)", 54321)
|
||||
test("[#54321 issue]", "[%s issue]", 54321)
|
||||
test("test (#9801 extra) issue", "test (%s extra) issue", 9801)
|
||||
test("test (#1)", "test (%s)", 1)
|
||||
|
||||
|
@ -253,10 +256,14 @@ func TestRegExp_sha1CurrentPattern(t *testing.T) {
|
|||
trueTestCases := []string{
|
||||
"d8a994ef243349f321568f9e36d5c3f444b99cae",
|
||||
"abcdefabcdefabcdefabcdefabcdefabcdefabcd",
|
||||
"(abcdefabcdefabcdefabcdefabcdefabcdefabcd)",
|
||||
"[abcdefabcdefabcdefabcdefabcdefabcdefabcd]",
|
||||
"abcdefabcdefabcdefabcdefabcdefabcdefabcd.",
|
||||
}
|
||||
falseTestCases := []string{
|
||||
"test",
|
||||
"abcdefg",
|
||||
"e59ff077-2d03-4e6b-964d-63fbaea81f",
|
||||
"abcdefghijklmnopqrstuvwxyzabcdefghijklmn",
|
||||
"abcdefghijklmnopqrstuvwxyzabcdefghijklmO",
|
||||
}
|
||||
|
@ -309,7 +316,9 @@ func TestRegExp_mentionPattern(t *testing.T) {
|
|||
"@ANT_123",
|
||||
"@xxx-DiN0-z-A..uru..s-xxx",
|
||||
" @lol ",
|
||||
" @Te/st",
|
||||
" @Te-st",
|
||||
"(@gitea)",
|
||||
"[@gitea]",
|
||||
}
|
||||
falseTestCases := []string{
|
||||
"@ 0",
|
||||
|
@ -317,6 +326,8 @@ func TestRegExp_mentionPattern(t *testing.T) {
|
|||
"@",
|
||||
"",
|
||||
"ABC",
|
||||
"/home/gitea/@gitea",
|
||||
"\"@gitea\"",
|
||||
}
|
||||
|
||||
for _, testCase := range trueTestCases {
|
||||
|
@ -335,6 +346,9 @@ func TestRegExp_issueAlphanumericPattern(t *testing.T) {
|
|||
"A-1",
|
||||
"RC-80",
|
||||
"ABCDEFGHIJ-1234567890987654321234567890",
|
||||
"ABC-123.",
|
||||
"(ABC-123)",
|
||||
"[ABC-123]",
|
||||
}
|
||||
falseTestCases := []string{
|
||||
"RC-08",
|
||||
|
@ -347,6 +361,8 @@ func TestRegExp_issueAlphanumericPattern(t *testing.T) {
|
|||
"ABC",
|
||||
"GG-",
|
||||
"rm-1",
|
||||
"/home/gitea/ABC-1234",
|
||||
"MY-STRING-ABC-123",
|
||||
}
|
||||
|
||||
for _, testCase := range trueTestCases {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue