def test_mangle_text():
"""We can muck with the inner text of a link."""
def ft(attrs, new=False):
attrs['_text'] = 'bar'
return attrs
eq_('<a href="http://ex.mp">bar</a> <a href="http://ex.mp/foo">bar</a>',
linkify('http://ex.mp <a href="http://ex.mp/foo">foo</a>', [ft]))
python类linkify()的实例源码
def test_prevent_links():
"""Returning None from any callback should remove links or prevent them
from being created."""
def no_new_links(attrs, new=False):
if new:
return None
return attrs
def no_old_links(attrs, new=False):
if not new:
return None
return attrs
def noop(attrs, new=False):
return attrs
in_text = 'a ex.mp <a href="http://example.com">example</a>'
out_text = 'a <a href="http://ex.mp">ex.mp</a> example'
tests = (
([noop], ('a <a href="http://ex.mp">ex.mp</a> '
'<a href="http://example.com">example</a>'), 'noop'),
([no_new_links, noop], in_text, 'no new, noop'),
([noop, no_new_links], in_text, 'noop, no new'),
([no_old_links, noop], out_text, 'no old, noop'),
([noop, no_old_links], out_text, 'noop, no old'),
([no_old_links, no_new_links], 'a ex.mp example', 'no links'),
)
def _check(cb, o, msg):
eq_(o, linkify(in_text, cb), msg)
for (cb, o, msg) in tests:
yield _check, cb, o, msg
def test_set_attrs():
"""We can set random attributes on links."""
def set_attr(attrs, new=False):
attrs['rev'] = 'canonical'
return attrs
in_(('<a href="http://ex.mp" rev="canonical">ex.mp</a>',
'<a rev="canonical" href="http://ex.mp">ex.mp</a>'),
linkify('ex.mp', [set_attr]))
def test_tlds():
in_(('<a href="http://example.com" rel="nofollow">example.com</a>',
'<a rel="nofollow" href="http://example.com">example.com</a>'),
linkify('example.com'))
in_(('<a href="http://example.co.uk" rel="nofollow">example.co.uk</a>',
'<a rel="nofollow" href="http://example.co.uk">example.co.uk</a>'),
linkify('example.co.uk'))
in_(('<a href="http://example.edu" rel="nofollow">example.edu</a>',
'<a rel="nofollow" href="http://example.edu">example.edu</a>'),
linkify('example.edu'))
eq_('example.xxx', linkify('example.xxx'))
eq_(' brie', linkify(' brie'))
in_(('<a href="http://bit.ly/fun" rel="nofollow">bit.ly/fun</a>',
'<a rel="nofollow" href="http://bit.ly/fun">bit.ly/fun</a>'),
linkify('bit.ly/fun'))
def test_escaping():
eq_('< unrelated', linkify('< unrelated'))
def test_nofollow_off():
eq_('<a href="http://example.com">example.com</a>',
linkify('example.com', []))
def test_link_in_html():
in_(('<i><a href="http://yy.com" rel="nofollow">http://yy.com</a></i>',
'<i><a rel="nofollow" href="http://yy.com">http://yy.com</a></i>'),
linkify('<i>http://yy.com</i>'))
in_(('<em><strong><a href="http://xx.com" rel="nofollow">http://xx.com'
'</a></strong></em>',
'<em><strong><a rel="nofollow" href="http://xx.com">http://xx.com'
'</a></strong></em>'),
linkify('<em><strong>http://xx.com</strong></em>'))
def test_add_rel_nofollow():
"""Verify that rel="nofollow" is added to an existing link"""
in_(('<a href="http://yy.com" rel="nofollow">http://yy.com</a>',
'<a rel="nofollow" href="http://yy.com">http://yy.com</a>'),
linkify('<a href="http://yy.com">http://yy.com</a>'))
def test_url_with_path():
in_(('<a href="http://example.com/path/to/file" rel="nofollow">'
'http://example.com/path/to/file</a>',
'<a rel="nofollow" href="http://example.com/path/to/file">'
'http://example.com/path/to/file</a>'),
linkify('http://example.com/path/to/file'))
def test_link_ftp():
in_(('<a href="ftp://ftp.mozilla.org/some/file" rel="nofollow">'
'ftp://ftp.mozilla.org/some/file</a>',
'<a rel="nofollow" href="ftp://ftp.mozilla.org/some/file">'
'ftp://ftp.mozilla.org/some/file</a>'),
linkify('ftp://ftp.mozilla.org/some/file'))
def test_link_fragment():
in_(('<a href="http://xx.com/path#frag" rel="nofollow">'
'http://xx.com/path#frag</a>',
'<a rel="nofollow" href="http://xx.com/path#frag">'
'http://xx.com/path#frag</a>'),
linkify('http://xx.com/path#frag'))
def test_escaped_html():
"""If I pass in escaped HTML, it should probably come out escaped."""
s = '<em>strong</em>'
eq_(s, linkify(s))
def test_non_url():
"""document.vulnerable should absolutely not be linkified."""
s = 'document.vulnerable'
eq_(s, linkify(s))
def test_javascript_url():
"""javascript: urls should never be linkified."""
s = 'javascript:document.vulnerable'
eq_(s, linkify(s))
def test_unsafe_url():
"""Any unsafe char ({}[]<>, etc.) in the path should end URL scanning."""
in_(('All your{"<a href="http://xx.yy.com/grover.png" '
'rel="nofollow">xx.yy.com/grover.png</a>"}base are',
'All your{"<a rel="nofollow" href="http://xx.yy.com/grover.png"'
'>xx.yy.com/grover.png</a>"}base are'),
linkify('All your{"xx.yy.com/grover.png"}base are'))
def test_libgl():
"""libgl.so.1 should not be linkified."""
eq_('libgl.so.1', linkify('libgl.so.1'))
def test_end_of_clause():
"""example.com/foo, shouldn't include the ,"""
in_(('<a href="http://ex.com/foo" rel="nofollow">ex.com/foo</a>, bar',
'<a rel="nofollow" href="http://ex.com/foo">ex.com/foo</a>, bar'),
linkify('ex.com/foo, bar'))