From e041a77cc9cf60fe7bce435cd19d7e2b1c662263 Mon Sep 17 00:00:00 2001 From: Branko Majic Date: Wed, 21 Feb 2018 19:59:04 +0100 Subject: [PATCH] Enforce the internal urlize markdown extensions works only with links stand as "separate words": - Introduced additional tests to ensure links are rendered correctly when in middle of text. - Introduced additional tests to ensure links are not rendered in case of invalid patterns. - Ensure the link pattern is matched only for standalone words (with "spacing" around the links). --- src/wiki/plugins/links/mdx/urlize.py | 3 ++- tests/plugins/links/test_urlize.py | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/wiki/plugins/links/mdx/urlize.py b/src/wiki/plugins/links/mdx/urlize.py index 3dc630c6..901ce1c2 100644 --- a/src/wiki/plugins/links/mdx/urlize.py +++ b/src/wiki/plugins/links/mdx/urlize.py @@ -91,7 +91,8 @@ class UrlizePattern(markdown.inlinepatterns.Pattern): to standard matching flags added by parent class. """ - return re.compile(r'^(.*?)%s(.*?)$' % URLIZE_RE, re.DOTALL | re.UNICODE | re.IGNORECASE) + # Ensure links are matched only if they stand on their own to avoid bad matches etc. + return re.compile(r'^(|.*?\s)%s(\s.*?|)$' % URLIZE_RE, re.DOTALL | re.UNICODE | re.IGNORECASE) def handleMatch(self, m): """ diff --git a/tests/plugins/links/test_urlize.py b/tests/plugins/links/test_urlize.py index 079c123a..37750eef 100644 --- a/tests/plugins/links/test_urlize.py +++ b/tests/plugins/links/test_urlize.py @@ -120,6 +120,24 @@ FIXTURE_POSITIVE_MATCHES = [ 'http://example.com/my/path?param1=value1¶m2=value2', EXPECTED_PARAGRAPH_TEMPLATE % ('http://example.com/my/path?param1=value1&param2=value2', 'http://example.com/my/path?param1=value1&param2=value2') ), + + # Link positioned somewhere within the text, but around whitespace boundary. + ( + 'This is link myhost.example.com', + "

This is link " + EXPECTED_LINK_TEMPLATE % ('http://myhost.example.com', 'myhost.example.com') + "

" + ), + ( + 'myhost.example.com is the link', + "

" + EXPECTED_LINK_TEMPLATE % ('http://myhost.example.com', 'myhost.example.com') + " is the link

" + ), + ( + 'I have best myhost.example.com link ever', + "

I have best " + EXPECTED_LINK_TEMPLATE % ('http://myhost.example.com', 'myhost.example.com') + " link ever

" + ), + ( + 'I have best\nmyhost.example.com link ever', + "

I have best\n" + EXPECTED_LINK_TEMPLATE % ('http://myhost.example.com', 'myhost.example.com') + " link ever

" + ), ] @@ -139,6 +157,14 @@ FIXTURE_NEGATIVE_MATCHES = [ 'example-.com', '

example-.com

' ), + ( + '-example.com', + '

-example.com

' + ), + ( + 'my.-example.com', + '

my.-example.com

' + ), ] -- 2.45.2