예제 #1
0
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'
예제 #2
0
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'
예제 #3
0
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'
예제 #4
0
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'
예제 #5
0
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
예제 #6
0
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
예제 #7
0
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
예제 #8
0
파일: utils.py 프로젝트: diox/olympia
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
예제 #9
0
 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)
예제 #10
0
 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)