diff options
author | mrsdizzie <info@mrsdizzie.com> | 2019-04-07 07:18:16 -0400 |
---|---|---|
committer | zeripath <art27@cantab.net> | 2019-04-07 12:18:16 +0100 |
commit | 6293736d02992ef317c0f1ffc875cdccf0fd5837 (patch) | |
tree | 00fbf299043df33893f1c173a824c4f45d13eb30 /modules/markup/html_test.go | |
parent | 5422f23ed8174661b6e658250e4007b7fdf0d603 (diff) | |
download | gitea-6293736d02992ef317c0f1ffc875cdccf0fd5837.tar.gz gitea-6293736d02992ef317c0f1ffc875cdccf0fd5837.zip |
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.
Diffstat (limited to 'modules/markup/html_test.go')
-rw-r--r-- | modules/markup/html_test.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go index 8d113b18a1..6bd9a465b5 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -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( + "info@gitea.com", + `<p><a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a></p>`) + test( + "(info@gitea.com)", + `<p>(<a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a>)</p>`) + test( + "[info@gitea.com]", + `<p>[<a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a>]</p>`) + test( + "info@gitea.com.", + `<p><a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a>.</p>`) + test( + "send email to info@gitea.co.uk.", + `<p>send email to <a href="mailto:info@gitea.co.uk" rel="nofollow">info@gitea.co.uk</a>.</p>`) + + // Test that should *not* be turned into email links + test( + "\"info@gitea.com\"", + `<p>“info@gitea.com”</p>`) + test( + "/home/gitea/mailstore/info@gitea/com", + `<p>/home/gitea/mailstore/info@gitea/com</p>`) + test( + "git@try.gitea.io:go-gitea/gitea.git", + `<p>git@try.gitea.io:go-gitea/gitea.git</p>`) +} + func TestRender_ShortLinks(t *testing.T) { setting.AppURL = AppURL setting.AppSubURL = AppSubURL |