def clean_localized_string(self): # All links (text and markup) are normalized. linkified = urlresolvers.linkify_with_outgoing(self.localized_string) # Keep only the allowed tags and attributes, escape the rest. return bleach.clean(linkified, tags=self.allowed_tags, attributes=self.allowed_attributes)
def test_linkify_with_outgoing(mock_linkify_bounce_url_callback): def side_effect(attrs, new=False): attrs["href"] = "bar" return attrs mock_linkify_bounce_url_callback.side_effect = side_effect # Without nofollow. res = urlresolvers.linkify_with_outgoing("http://example.com", nofollow=False) eq_(res, '<a href="bar">http://example.com</a>') # With nofollow (default). res = urlresolvers.linkify_with_outgoing("http://example.com") eq_(res, '<a href="bar" rel="nofollow">http://example.com</a>') res = urlresolvers.linkify_with_outgoing("http://example.com", nofollow=True) eq_(res, '<a href="bar" rel="nofollow">http://example.com</a>')
def test_linkify_with_outgoing(mock_linkify_bounce_url_callback): def side_effect(attrs, new=False): attrs['href'] = 'bar' return attrs mock_linkify_bounce_url_callback.side_effect = side_effect # Without nofollow. res = urlresolvers.linkify_with_outgoing('http://example.com', nofollow=False) eq_(res, '<a href="bar">http://example.com</a>') # With nofollow (default). res = urlresolvers.linkify_with_outgoing('http://example.com') eq_(res, '<a href="bar" rel="nofollow">http://example.com</a>') res = urlresolvers.linkify_with_outgoing('http://example.com', nofollow=True) eq_(res, '<a href="bar" rel="nofollow">http://example.com</a>')
def clean(self): linkified = urlresolvers.linkify_with_outgoing(self.localized_string) try: clean = bleach.clean(linkified, tags=['a'], attributes={'a': ['href', 'rel']}) except Exception as e: log.error('Failed to clean %s: %r' % (linkified, e), exc_info=sys.exc_info()) clean = '' self.localized_string_clean = clean
def test_linkify_with_outgoing_text_links(mock_linkify_bounce_url_callback): def side_effect(attrs, new=False): attrs['href'] = 'bar' return attrs mock_linkify_bounce_url_callback.side_effect = side_effect # Without nofollow. res = urlresolvers.linkify_with_outgoing('a text http://example.com link', nofollow=False) eq_(res, 'a text <a href="bar">http://example.com</a> link') # With nofollow (default). res = urlresolvers.linkify_with_outgoing('a text http://example.com link') eq_(res, 'a text <a rel="nofollow" href="bar">http://example.com</a> link') res = urlresolvers.linkify_with_outgoing('a text http://example.com link', nofollow=True) eq_(res, 'a text <a rel="nofollow" href="bar">http://example.com</a> link')
def clean(self): from amo.utils import clean_nl super(PurifiedTranslation, self).clean() try: cleaned = bleach.clean(self.localized_string) except Exception as e: log.error('Failed to clean %s: %r' % (self.localized_string, e), exc_info=sys.exc_info()) cleaned = '' linkified = urlresolvers.linkify_with_outgoing(cleaned) self.localized_string_clean = clean_nl(linkified).strip()
def test_linkify_with_outgoing_text_links(mock_linkify_bounce_url_callback): def side_effect(attrs, new=False): attrs['href'] = 'bar' return attrs mock_linkify_bounce_url_callback.side_effect = side_effect # Without nofollow. res = urlresolvers.linkify_with_outgoing('a text http://example.com link', nofollow=False) eq_(res, 'a text <a href="bar">http://example.com</a> link') # With nofollow (default). res = urlresolvers.linkify_with_outgoing('a text http://example.com link') # Use PyQuery because the attributes could be rendered in any order. doc = PyQuery(res) assert doc('a[href="bar"][rel="nofollow"]')[0].text == 'http://example.com' res = urlresolvers.linkify_with_outgoing('a text http://example.com link', nofollow=True) assert doc('a[href="bar"][rel="nofollow"]')[0].text == 'http://example.com'
def test_linkify_with_outgoing_markup_links(mock_linkify_bounce_url_callback): def side_effect(attrs, new=False): attrs['href'] = 'bar' return attrs mock_linkify_bounce_url_callback.side_effect = side_effect # Without nofollow. res = urlresolvers.linkify_with_outgoing( 'a markup <a href="http://example.com">link</a> with text', nofollow=False) eq_(res, 'a markup <a href="bar">link</a> with text') # With nofollow (default). res = urlresolvers.linkify_with_outgoing( 'a markup <a href="http://example.com">link</a> with text') eq_(res, 'a markup <a rel="nofollow" href="bar">link</a> with text') res = urlresolvers.linkify_with_outgoing( 'a markup <a href="http://example.com">link</a> with text', nofollow=True) eq_(res, 'a markup <a rel="nofollow" href="bar">link</a> with text')
def escape_all(v): """Escape html in JSON value, including nested items.""" if isinstance(v, basestring): v = jinja2.escape(smart_unicode(v)) v = linkify_with_outgoing(v) return v elif isinstance(v, list): for i, lv in enumerate(v): v[i] = escape_all(lv) elif isinstance(v, dict): for k, lv in v.iteritems(): v[k] = escape_all(lv) elif isinstance(v, Translation): v = jinja2.escape(smart_unicode(v.localized_string)) return v
def escape_all(v, linkify_only_full=False): """Escape html in JSON value, including nested items. Only linkify full urls, including a scheme, if "linkify_only_full" is True. """ if isinstance(v, basestring): v = jinja2.escape(smart_unicode(v)) v = linkify_with_outgoing(v, only_full=linkify_only_full) return v elif isinstance(v, list): for i, lv in enumerate(v): v[i] = escape_all(lv, linkify_only_full=linkify_only_full) elif isinstance(v, dict): for k, lv in v.iteritems(): v[k] = escape_all(lv, linkify_only_full=linkify_only_full) elif isinstance(v, Translation): v = jinja2.escape(smart_unicode(v.localized_string)) return v
def test_linkify_with_outgoing_raises(mock_linkify): mock_linkify.side_effect = Exception('crash test') res = urlresolvers.linkify_with_outgoing('http://example.com') eq_(res, 'http://example.com')