def test_linkify_with_outgoing_text_links(mock_linkify_bounce_url_callback): def side_effect(attrs, new=False): attrs[(None, 'href')] = 'bar' return attrs mock_linkify_bounce_url_callback.side_effect = side_effect 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'
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[(None, 'href')] = 'bar' return attrs mock_linkify_bounce_url_callback.side_effect = side_effect res = urlresolvers.linkify_with_outgoing( 'a markup <a href="http://example.com">link</a> with text') # Use PyQuery because the attributes could be rendered in any order. doc = PyQuery(res) assert doc('a[href="bar"][rel="nofollow"]')[0].text == 'link'
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 escape_all(value): """Escape html in JSON value, including nested items. Only linkify full urls, including a scheme, if "linkify_only_full" is True. """ if isinstance(value, str): value = jinja2.escape(force_text(value)) value = linkify_with_outgoing(value) return value elif isinstance(value, list): for i, lv in enumerate(value): value[i] = escape_all(lv) elif isinstance(value, dict): for k, lv in value.items(): value[k] = escape_all(lv) elif isinstance(value, Translation): value = jinja2.escape(force_text(value)) return value
def escape_all(value): """Escape html in JSON value, including nested items. Only linkify full urls, including a scheme, if "linkify_only_full" is True. """ if isinstance(value, six.string_types): value = jinja2.escape(force_text(value)) value = linkify_with_outgoing(value) return value elif isinstance(value, list): for i, lv in enumerate(value): value[i] = escape_all(lv) elif isinstance(value, dict): for k, lv in six.iteritems(value): value[k] = escape_all(lv) elif isinstance(value, Translation): value = jinja2.escape(force_text(value)) return value
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)