Ticket #6514: 6514.2.diff

File 6514.2.diff, 6.4 KB (added by Chris Beaven, 16 years ago)
  • django/utils/html.py

     
    102102            if middle.startswith('www.') or ('@' not in middle and not middle.startswith('http://') and \
    103103                    len(middle) > 0 and middle[0] in string.ascii_letters + string.digits and \
    104104                    (middle.endswith('.org') or middle.endswith('.net') or middle.endswith('.com'))):
    105                 middle = '<a href="http://%s"%s>%s</a>' % (
    106                         urlquote(middle, safe='/&=:;#?+'),  nofollow_attr,
    107                         trim_url(middle))
     105                middle = 'http://%s' % middle
    108106            if middle.startswith('http://') or middle.startswith('https://'):
     107                url = urlquote(middle, safe='/&=:;#?+')
     108                if autoescape and not safe_input:
     109                    url = escape(url)
     110                trimmed_url = trim_url(middle)
    109111                middle = '<a href="%s"%s>%s</a>' % (
    110                         urlquote(middle, safe='/&=:;#?+'), nofollow_attr,
    111                         trim_url(middle))
    112             if '@' in middle and not middle.startswith('www.') and \
    113                     not ':' in middle and simple_email_re.match(middle):
     112                            url, nofollow_attr, trimmed_url
     113                        )
     114            elif '@' in middle and not middle.startswith('www.') and \
     115                      not ':' in middle and simple_email_re.match(middle):
     116                if autoescape:
     117                    middle = conditional_escape(middle)
    114118                middle = '<a href="mailto:%s">%s</a>' % (middle, middle)
    115119            if lead + middle + trail != word:
    116                 words[i] = lead + middle + trail
     120                if autoescape and not safe_input:
     121                    lead, trail = escape(lead), escape(trail)
     122                words[i] = mark_safe(lead + middle + trail)
    117123            elif autoescape and not safe_input:
    118124                words[i] = escape(word)
    119125        elif safe_input:
  • tests/regressiontests/templates/filters.py

     
    9898        'filter-upper01': ('{% autoescape off %}{{ a|upper }} {{ b|upper }}{% endautoescape %}', {"a": "a & b", "b": mark_safe("a &amp; b")}, u"A & B A &AMP; B"),
    9999        'filter-upper02': ('{{ a|upper }} {{ b|upper }}', {"a": "a & b", "b": mark_safe("a &amp; b")}, u"A &amp; B A &amp;AMP; B"),
    100100
    101         'filter-urlize01': ('{% autoescape off %}{{ a|urlize }} {{ b|urlize }}{% endautoescape %}', {"a": "http://example.com/x=&y=", "b": mark_safe("http://example.com?x=&y=")}, u'<a href="http://example.com/x=&y=" rel="nofollow">http://example.com/x=&y=</a> <a href="http://example.com?x=&y=" rel="nofollow">http://example.com?x=&y=</a>'),
    102         'filter-urlize02': ('{{ a|urlize }} {{ b|urlize }}', {"a": "http://example.com/x=&y=", "b": mark_safe("http://example.com?x=&y=")}, u'<a href="http://example.com/x=&y=" rel="nofollow">http://example.com/x=&amp;y=</a> <a href="http://example.com?x=&y=" rel="nofollow">http://example.com?x=&y=</a>'),
     101        'filter-urlize01': ('{% autoescape off %}{{ a|urlize }} {{ b|urlize }}{% endautoescape %}', {"a": "http://example.com/x=&y=", "b": mark_safe("http://example.com?x=&amp;y=")}, u'<a href="http://example.com/x=&y=" rel="nofollow">http://example.com/x=&y=</a> <a href="http://example.com?x=&amp;y=" rel="nofollow">http://example.com?x=&amp;y=</a>'),
     102        'filter-urlize02': ('{{ a|urlize }} {{ b|urlize }}', {"a": "http://example.com/?x=&y=", "b": mark_safe("http://example.com?x=&amp;y=")}, u'<a href="http://example.com/?x=&amp;y=" rel="nofollow">http://example.com/?x=&amp;y=</a> <a href="http://example.com?x=&amp;y=" rel="nofollow">http://example.com?x=&amp;y=</a>'),
    103103        'filter-urlize03': ('{% autoescape off %}{{ a|urlize }}{% endautoescape %}', {"a": mark_safe("a &amp; b")}, 'a &amp; b'),
    104104        'filter-urlize04': ('{{ a|urlize }}', {"a": mark_safe("a &amp; b")}, 'a &amp; b'),
    105105
     
    109109        'filter-urlize06': ('{{ a|urlize }}', {"a": "<script>alert('foo')</script>"}, '&lt;script&gt;alert(&#39;foo&#39;)&lt;/script&gt;'),
    110110
    111111        'filter-urlizetrunc01': ('{% autoescape off %}{{ a|urlizetrunc:"8" }} {{ b|urlizetrunc:"8" }}{% endautoescape %}', {"a": '"Unsafe" http://example.com/x=&y=', "b": mark_safe('&quot;Safe&quot; http://example.com?x=&y=')}, u'"Unsafe" <a href="http://example.com/x=&y=" rel="nofollow">http:...</a> &quot;Safe&quot; <a href="http://example.com?x=&y=" rel="nofollow">http:...</a>'),
    112         'filter-urlizetrunc02': ('{{ a|urlizetrunc:"8" }} {{ b|urlizetrunc:"8" }}', {"a": '"Unsafe" http://example.com/x=&y=', "b": mark_safe('&quot;Safe&quot; http://example.com?x=&y=')}, u'&quot;Unsafe&quot; <a href="http://example.com/x=&y=" rel="nofollow">http:...</a> &quot;Safe&quot; <a href="http://example.com?x=&y=" rel="nofollow">http:...</a>'),
     112        'filter-urlizetrunc02': ('{{ a|urlizetrunc:"8" }} {{ b|urlizetrunc:"8" }}', {"a": '"Unsafe" http://example.com/?x=&y=', "b": mark_safe('&quot;Safe&quot; http://example.com?x=&amp;y=')}, u'&quot;Unsafe&quot; <a href="http://example.com/?x=&amp;y=" rel="nofollow">http:...</a> &quot;Safe&quot; <a href="http://example.com?x=&amp;y=" rel="nofollow">http:...</a>'),
    113113
    114114        'filter-wordcount01': ('{% autoescape off %}{{ a|wordcount }} {{ b|wordcount }}{% endautoescape %}', {"a": "a & b", "b": mark_safe("a &amp; b")}, "3 3"),
    115115        'filter-wordcount02': ('{{ a|wordcount }} {{ b|wordcount }}', {"a": "a & b", "b": mark_safe("a &amp; b")}, "3 3"),
     
    240240        'chaining13': ('{{ a|safe|force_escape }}', {"a": "a < b"}, "a &lt; b"),
    241241        'chaining14': ('{% autoescape off %}{{ a|safe|force_escape }}{% endautoescape %}', {"a": "a < b"}, "a &lt; b"),
    242242
    243         # Filters decorated with stringfilter still respect is_safe. 
     243        # Filters decorated with stringfilter still respect is_safe.
    244244        'autoescape-stringfilter01': (r'{{ unsafe|capfirst }}', {'unsafe': UnsafeClass()}, 'You &amp; me'),
    245245        'autoescape-stringfilter02': (r'{% autoescape off %}{{ unsafe|capfirst }}{% endautoescape %}', {'unsafe': UnsafeClass()}, 'You & me'),
    246246        'autoescape-stringfilter03': (r'{{ safe|capfirst }}', {'safe': SafeClass()}, 'You &gt; me'),
Back to Top