瀏覽代碼

Scrub urls fix (#3918)

* Don't expand mailto urls

* Add test case
version-14
Faris Ansari 8 年之前
committed by Makarand Bauskar
父節點
當前提交
7397b68290
共有 2 個檔案被更改,包括 33 行新增5 行删除
  1. +28
    -2
      frappe/tests/test_utils.py
  2. +5
    -3
      frappe/utils/data.py

+ 28
- 2
frappe/tests/test_utils.py 查看文件

@@ -4,7 +4,7 @@ from __future__ import unicode_literals


import unittest import unittest


from frappe.utils import evaluate_filters, money_in_words
from frappe.utils import evaluate_filters, money_in_words, scrub_urls, get_url


class TestFilters(unittest.TestCase): class TestFilters(unittest.TestCase):
def test_simple_dict(self): def test_simple_dict(self):
@@ -57,4 +57,30 @@ class TestMoney(unittest.TestCase):
self.assertEqual( self.assertEqual(
money_in_words(num[0], "NGN"), num[1], "{0} is not the same as {1}". money_in_words(num[0], "NGN"), num[1], "{0} is not the same as {1}".
format(money_in_words(num[0], "NGN"), num[1]) format(money_in_words(num[0], "NGN"), num[1])
)
)

class TestDataManipulation(unittest.TestCase):
def test_scrub_urls(self):
html = '''
<p>You have a new message from: <b>John</b></p>
<p>Hey, wassup!</p>
<div class="more-info">
<a href="http://test.com">Test link 1</a>
<a href="/about">Test link 2</a>
<a href="login">Test link 3</a>
<img src="/assets/frappe/test.jpg">
</div>
<div style="background-image: url('/assets/frappe/bg.jpg')">
Please mail us at <a href="mailto:test@example.com">email</a>
</div>
'''

html = scrub_urls(html)
url = get_url()

self.assertTrue('<a href="http://test.com">Test link 1</a>' in html)
self.assertTrue('<a href="{0}/about">Test link 2</a>'.format(url) in html)
self.assertTrue('<a href="{0}/login">Test link 3</a>'.format(url) in html)
self.assertTrue('<img src="{0}/assets/frappe/test.jpg">'.format(url) in html)
self.assertTrue('style="background-image: url(\'{0}/assets/frappe/bg.jpg\') !important"'.format(url) in html)
self.assertTrue('<a href="mailto:test@example.com">email</a>' in html)

+ 5
- 3
frappe/utils/data.py 查看文件

@@ -778,9 +778,11 @@ def expand_relative_urls(html):


def _expand_relative_urls(match): def _expand_relative_urls(match):
to_expand = list(match.groups()) to_expand = list(match.groups())
if not to_expand[2].startswith("/"):
to_expand[2] = "/" + to_expand[2]
to_expand.insert(2, url)

if not to_expand[2].startswith('mailto'):
if not to_expand[2].startswith("/"):
to_expand[2] = "/" + to_expand[2]
to_expand.insert(2, url)


if 'url' in to_expand[0] and to_expand[1].startswith('(') and to_expand[-1].endswith(')'): if 'url' in to_expand[0] and to_expand[1].startswith('(') and to_expand[-1].endswith(')'):
# background-image: url('/assets/...') - workaround for wkhtmltopdf print-media-type # background-image: url('/assets/...') - workaround for wkhtmltopdf print-media-type


Loading…
取消
儲存