-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
3 changed files
with
70 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,8 @@ func TestRender_Commits(t *testing.T) { | |
test(commit, `<p><a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`) | ||
test(tree, `<p><a href="`+tree+`" rel="nofollow">b6dd6210ea/src</a></p>`) | ||
test("commit "+sha, `<p>commit <a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`) | ||
test("/home/gitea/"+sha, "<p>/home/gitea/"+sha+"</p>") | ||
|
||
} | ||
|
||
func TestRender_CrossReferences(t *testing.T) { | ||
|
@@ -53,6 +55,9 @@ func TestRender_CrossReferences(t *testing.T) { | |
test( | ||
"go-gitea/gitea#12345", | ||
`<p><a href="`+util.URLJoin(AppURL, "go-gitea", "gitea", "issues", "12345")+`" rel="nofollow">go-gitea/gitea#12345</a></p>`) | ||
test( | ||
"/home/gitea/go-gitea/gitea#12345", | ||
`<p>/home/gitea/go-gitea/gitea#12345</p>`) | ||
} | ||
|
||
func TestMisc_IsSameDomain(t *testing.T) { | ||
|
@@ -144,6 +149,44 @@ func TestRender_links(t *testing.T) { | |
`<p>www</p>`) | ||
} | ||
|
||
func TestRender_email(t *testing.T) { | ||
setting.AppURL = AppURL | ||
setting.AppSubURL = AppSubURL | ||
|
||
test := func(input, expected string) { | ||
buffer := RenderString("a.md", input, setting.AppSubURL, nil) | ||
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer))) | ||
} | ||
// Text that should be turned into email link | ||
|
||
test( | ||
"[email protected]", | ||
`<p><a href="mailto:[email protected]" rel="nofollow">[email protected]</a></p>`) | ||
test( | ||
"([email protected])", | ||
`<p>(<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>)</p>`) | ||
test( | ||
"[[email protected]]", | ||
`<p>[<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>]</p>`) | ||
test( | ||
"[email protected].", | ||
`<p><a href="mailto:[email protected]" rel="nofollow">[email protected]</a>.</p>`) | ||
test( | ||
"send email to [email protected].", | ||
`<p>send email to <a href="mailto:[email protected]" rel="nofollow">[email protected]</a>.</p>`) | ||
|
||
// Test that should *not* be turned into email links | ||
test( | ||
"\"[email protected]\"", | ||
`<p>“[email protected]”</p>`) | ||
test( | ||
"/home/gitea/mailstore/info@gitea/com", | ||
`<p>/home/gitea/mailstore/info@gitea/com</p>`) | ||
test( | ||
"[email protected]:go-gitea/gitea.git", | ||
`<p>[email protected]:go-gitea/gitea.git</p>`) | ||
} | ||
|
||
func TestRender_ShortLinks(t *testing.T) { | ||
setting.AppURL = AppURL | ||
setting.AppSubURL = AppSubURL | ||
|