Ticket #11911: 11911-2.patch

File 11911-2.patch, 3.8 KB (added by Aymeric Augustin, 12 years ago)
  • tests/regressiontests/defaultfilters/tests.py

     
    253253            u'<a href="http://en.wikipedia.org/wiki/Caf%C3%A9" rel="nofollow">'
    254254            u'http://en.wikipedia.org/wiki/Café</a>')
    255255
     256        # Check urlize keeps balanced parentheses - see #11911
     257        self.assertEqual(urlize('http://en.wikipedia.org/wiki/Django_(web_framework)'),
     258            u'<a href="http://en.wikipedia.org/wiki/Django_(web_framework)" rel="nofollow">'
     259            u'http://en.wikipedia.org/wiki/Django_(web_framework)</a>')
     260        self.assertEqual(urlize('(see http://en.wikipedia.org/wiki/Django_(web_framework))'),
     261            u'(see <a href="http://en.wikipedia.org/wiki/Django_(web_framework)" rel="nofollow">'
     262            u'http://en.wikipedia.org/wiki/Django_(web_framework)</a>)')
     263
    256264        # Check urlize adds nofollow properly - see #12183
    257265        self.assertEqual(urlize('foo@bar.com or www.bar.com'),
    258266            u'<a href="mailto:foo@bar.com">foo@bar.com</a> or '
  • django/utils/html.py

     
    1111from django.utils.text import normalize_newlines
    1212
    1313# Configuration for urlize() function.
    14 LEADING_PUNCTUATION  = ['(', '<', '&lt;']
    15 TRAILING_PUNCTUATION = ['.', ',', ')', '>', '\n', '&gt;']
     14TRAILING_PUNCTUATION = ['.', ',', ':', ';']
     15WRAPPING_PUNCTUATION = [('(', ')'), ('<', '>'), ('&lt;', '&gt;')]
    1616
    1717# List of possible strings used for bullets in bulleted lists.
    1818DOTS = [u'&middot;', u'*', u'\u2022', u'&#149;', u'&bull;', u'&#8226;']
     
    2020unencoded_ampersands_re = re.compile(r'&(?!(\w+|#\d+);)')
    2121unquoted_percents_re = re.compile(r'%(?![0-9A-Fa-f]{2})')
    2222word_split_re = re.compile(r'(\s+)')
    23 punctuation_re = re.compile('^(?P<lead>(?:%s)*)(?P<middle>.*?)(?P<trail>(?:%s)*)$' % \
    24     ('|'.join([re.escape(x) for x in LEADING_PUNCTUATION]),
    25     '|'.join([re.escape(x) for x in TRAILING_PUNCTUATION])))
    2623simple_url_re = re.compile(r'^https?://\w')
    2724simple_url_2_re = re.compile(r'^www\.|^(?!http)\w[^@]+\.(com|edu|gov|int|mil|net|org|[a-z]{2})$')
    2825simple_email_re = re.compile(r'^\S+@\S+\.\S+$')
     
    147144    for i, word in enumerate(words):
    148145        match = None
    149146        if '.' in word or '@' in word or ':' in word:
    150             match = punctuation_re.match(word)
    151         if match:
    152             lead, middle, trail = match.groups()
     147            # Deal with punctuation.
     148            lead, middle, trail = '', word, ''
     149            for punctuation in TRAILING_PUNCTUATION:
     150                if middle.endswith(punctuation):
     151                    middle = middle[:-len(punctuation)]
     152                    trail = punctuation + trail
     153            for opening, closing in WRAPPING_PUNCTUATION:
     154                if middle.startswith(opening):
     155                    middle = middle[len(opening):]
     156                    lead = lead + opening
     157                # Keep parentheses at the end only if they're balanced.
     158                if (middle.endswith(closing)
     159                    and middle.count(closing) = middle.count(opening) + 1):
     160                    middle = middle[:-len(closing)]
     161                    trail = closing + trail
     162
    153163            # Make URL we want to point to.
    154164            url = None
    155165            nofollow_attr = ' rel="nofollow"' if nofollow else ''
     
    162172                domain = domain.encode('idna')
    163173                url = 'mailto:%s@%s' % (local, domain)
    164174                nofollow_attr = ''
     175
    165176            # Make link.
    166177            if url:
    167178                trimmed = trim_url(middle)
Back to Top