Skip to content

Commit

Permalink
Make linkify understand port numbers. Fixes mozilla#38
Browse files Browse the repository at this point in the history
  • Loading branch information
James Socol committed Sep 2, 2011
1 parent dda12f0 commit 5d42e08
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bleach/__init__.py
Expand Up @@ -59,7 +59,7 @@
url_re = re.compile(
r"""\(* # Match any opening parentheses.
\b(?<![@.])(?:\w[\w-]*:/{0,3}(?:(?:\w+:)?\w+@)?)? # http://
([\w-]+\.)+(?:%s)(?!\.\w)\b # xx.yy.tld
([\w-]+\.)+(?:%s)(?:\:\d+)?(?!\.\w)\b # xx.yy.tld(:##)?
(?:[/?][^\s\{\}\|\\\^\[\]`<>"]*)?
# /path/zz (excluding "unsafe" chars from RFC 1738,
# except for # and ~, which happen in practice)
Expand Down
21 changes: 19 additions & 2 deletions bleach/tests/test_links.py
Expand Up @@ -242,8 +242,7 @@ def test_sarcasm():


def test_wrapping_parentheses():
"""urls wrapped in balanced paranthesis shall not include them in the href
"""
"""URLs wrapped in parantheses should not include them."""
out = u'%s<a href="http://%s" rel="nofollow">%s</a>%s'

tests = (
Expand Down Expand Up @@ -277,3 +276,21 @@ def check(test, expected_output):

for test, expected_output in tests:
yield check, test, expected_output


def test_ports():
"""URLs can contain port numbers."""
tests = (
('http://foo.com:8000', ('http://foo.com:8000', '')),
('http://foo.com:8000/', ('http://foo.com:8000/', '')),
('http://bar.com:xkcd', ('http://bar.com', ':xkcd')),
('http://foo.com:81/bar', ('http://foo.com:81/bar', '')),
('http://foo.com:', ('http://foo.com', ':')),
)

def check(test, output):
eq_(u'<a href="{0}" rel="nofollow">{0}</a>{1}'.format(*output),
linkify(test))

for test, output in tests:
yield check, test, output

0 comments on commit 5d42e08

Please sign in to comment.