Skip to content

Commit

Permalink
Be careful with trailing punctuation (fixes mozilla#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
James Socol committed Jun 14, 2011
1 parent 8d52471 commit 8de28ab
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
13 changes: 10 additions & 3 deletions bleach/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@

proto_re = re.compile(r'^[\w-]+:/{0,3}')

punct_re = re.compile(r'([\.,]+)$')

email_re = re.compile(
r"(?<!//)"
r"(([-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom
Expand Down Expand Up @@ -163,14 +165,19 @@ def email_repl(match):

def link_repl(match):
url = match.group(0)
end = u''
m = re.search(punct_re, url)
if m:
end = m.group(0)
url = url[0:m.start()]
if re.search(proto_re, url):
href = url
else:
href = u''.join(['http://', url])
href = u''.join([u'http://', url])

repl = u'<a href="%s"%s>%s</a>'
repl = u'<a href="%s"%s>%s</a>%s'

return repl % (filter_url(href), rel, filter_text(url))
return repl % (filter_url(href), rel, filter_text(url), end)

linkify_nodes(forest)

Expand Down
23 changes: 21 additions & 2 deletions bleach/tests/test_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,24 @@ def test_libgl():

def test_end_of_sentence():
"""example.com. should match."""
eq_('<a href="http://example.com" rel="nofollow">example.com</a>.',
linkify('example.com.'))
out = u'<a href="http://%s" rel="nofollow">%s</a>%s'
in_ = u'%s%s'

def check(u, p):
eq_(out % (u, u, p), linkify(in_ % (u, p)))

tests = (
('example.com', '.'),
('example.com', '...'),
('ex.com/foo', '.'),
('ex.com/foo', '....'),
)

for u, p in tests:
yield check, u, p


def test_end_of_clause():
"""example.com/foo, shouldn't include the ,"""
eq_('<a href="http://ex.com/foo" rel="nofollow">ex.com/foo</a>, bar',
linkify('ex.com/foo, bar'))

0 comments on commit 8de28ab

Please sign in to comment.