Browse Source

fix #754: append http:// to protocol-less outgoing urls, fix [url] tests

Rafał Pitoń 8 years ago
parent
commit
245176c4b8
3 changed files with 24 additions and 10 deletions
  1. 4 6
      misago/markup/bbcode/inline.py
  2. 14 1
      misago/markup/parser.py
  3. 6 3
      misago/markup/tests/test_parser.py

+ 4 - 6
misago/markup/bbcode/inline.py

@@ -76,16 +76,14 @@ class BBCodeUrlPattern(BBcodePattern, LinkPattern):
     def handleMatch(self, m):
         el = util.etree.Element("a")
 
-        if m.group(8):
-            el.text = m.group(8).strip()
+        if m.group(6):
+            el.text = m.group(8)
             href = m.group(5)
         else:
-            el.text = m.group(3)
-            href = m.group(3)
+            el.text = m.group(8).strip()
+            href = m.group(8)
 
         if href:
-            if href[0] == "<":
-                href = href[1:-1]
             el.set("href", self.sanitize_url(self.unescape(href.strip())))
         else:
             el.set("href", "")

+ 14 - 1
misago/markup/parser.py

@@ -168,7 +168,8 @@ def clean_links(request, result, force_shva=False):
             result['internal_links'].append(link['href'])
             link['href'] = clean_attachment_link(link['href'], force_shva)
         else:
-            result['outgoing_links'].append(link['href'])
+            result['outgoing_links'].append(clean_link_prefix(link['href']))
+            link['href'] = assert_link_prefix(link['href'])
 
         if link.string:
             link.string = clean_link_prefix(link.string)
@@ -181,6 +182,7 @@ def clean_links(request, result, force_shva=False):
             img['src'] = clean_attachment_link(img['src'], force_shva)
         else:
             result['images'].append(clean_link_prefix(img['src']))
+            img['src'] = assert_link_prefix(img['src'])
 
     # [6:-7] trims <body></body> wrap
     result['parsed_text'] = six.text_type(soup.body)[6:-7]
@@ -204,6 +206,17 @@ def clean_link_prefix(link):
     return link
 
 
+def assert_link_prefix(link):
+    if link.lower().startswith('https:'):
+        return link
+    if link.lower().startswith('http:'):
+        return link
+    if link.startswith('//'):
+        return 'http:{}'.format(link)
+
+    return 'http://{}'.format(link)
+
+
 def clean_internal_link(link, host):
     link = clean_link_prefix(link)
 

+ 6 - 3
misago/markup/tests/test_parser.py

@@ -99,6 +99,8 @@ Lorem ipsum !(https://placekitten.com/g/1200/500)
     def test_url(self):
         """url bbcode is correctly parsed"""
         test_text = """
+Lorem ipsum [url]placekitten.com/g/300/300[/url]
+
 Lorem ipsum [url]https://placekitten.com/g/600/600[/url]
 
 Lorem ipsum [uRL=https://placekitten.com/g/400/400"]Label text![/UrL]
@@ -107,7 +109,8 @@ Lorem ipsum [Lorem ipsum](https://placekitten.com/g/1200/500)
 """.strip()
 
         expected_result = """
-<p>Lorem ipsum <a href="" rel="nofollow">placekitten.com/g/600/600</a></p>
+<p>Lorem ipsum <a href="http://placekitten.com/g/300/300" rel="nofollow">placekitten.com/g/300/300</a></p>
+<p>Lorem ipsum <a href="https://placekitten.com/g/600/600" rel="nofollow">placekitten.com/g/600/600</a></p>
 <p>Lorem ipsum <a href="https://placekitten.com/g/400/400" rel="nofollow">Label text!</a></p>
 <p>Lorem ipsum <a href="https://placekitten.com/g/1200/500" rel="nofollow">Lorem ipsum</a></p>
 """.strip()
@@ -224,7 +227,7 @@ Lorem ipsum: http://somewhere.com
 
         result = parse(test_text, MockRequest(), MockPoster(), minify=True)
         self.assertEqual(expected_result, result['parsed_text'])
-        self.assertEqual(result['outgoing_links'], ['http://somewhere.com'])
+        self.assertEqual(result['outgoing_links'], ['somewhere.com'])
         self.assertEqual(result['images'], [])
         self.assertEqual(result['internal_links'], [])
 
@@ -240,7 +243,7 @@ Lorem ipsum: http://somewhere.com/somewhere-something/
 
         result = parse(test_text, MockRequest(), MockPoster(), minify=True)
         self.assertEqual(expected_result, result['parsed_text'])
-        self.assertEqual(result['outgoing_links'], ['http://somewhere.com/somewhere-something/'])
+        self.assertEqual(result['outgoing_links'], ['somewhere.com/somewhere-something/'])
         self.assertEqual(result['images'], [])
         self.assertEqual(result['internal_links'], [])