Ticket #10931: 10931-utils_text-r10651.patch

File 10931-utils_text-r10651.patch, 4.2 KB (added by George Song, 16 years ago)

Updated patch with tests.

  • django/utils/text.py

     
    6161        return u''
    6262    html4_singlets = ('br', 'col', 'link', 'base', 'img', 'param', 'area', 'hr', 'input')
    6363    # Set up regular expressions
    64     re_words = re.compile(r'&.*?;|<.*?>|(\w[\w-]*)', re.U)
    65     re_tag = re.compile(r'<(/)?([^ ]+?)(?: (/)| .*?)?>')
     64    re_words = re.compile(r'&.*?;|<.*?>|(\w[\w-]*)', re.U|re.S)
     65    re_tag = re.compile(r'<(/)?([^ ]+?)(?: (/)| .*?)?>', re.S)
    6666    # Count non-HTML words and keep note of open tags
    6767    pos = 0
    6868    ellipsis_pos = 0
  • tests/regressiontests/utils/tests.py

     
    99import timesince
    1010import datastructures
    1111import itercompat
     12import text
    1213from decorators import DecoratorFromMiddlewareTests
    1314
    1415# We need this because "datastructures" uses sorted() and the tests are run in
     
    2324    'timesince': timesince,
    2425    'datastructures': datastructures,
    2526    'itercompat': itercompat,
     27    'text': text,
    2628}
    2729
    2830class TestUtilsHtml(TestCase):
  • tests/regressiontests/utils/text.py

     
     1"""
     2>>> from django.utils.text import truncate_html_words
     3
     4>>> a = "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout."
     5>>> truncate_html_words(a, 8)
     6u'It is a long established fact that a ...'
     7
     8>>> a = "It is a long \\nestablished fact that a reader will be distracted by the readable content of  a page when looking at its layout."
     9>>> truncate_html_words(a, 8)
     10u'It is a long \\nestablished fact that a ...'
     11
     12>>> a = 'It is a <a href="xyz.html"\\nid="xyz">long established fact that a reader will be distracted by the readable content</a> of a page when looking at its layout.'
     13>>> truncate_html_words(a, 8)
     14u'It is a <a href="xyz.html"\\nid="xyz">long established fact that a ...</a>'
     15
     16>>> a = 'Fact: Python > Ruby. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.'
     17>>> truncate_html_words(a, 8)
     18u'Fact: Python > Ruby. It is a long established ...'
     19
     20>>> a = 'Fact: Ruby < <strong>Python</strong>.<br /> <a href="xyz.html" id="xyz">It <em>is</em> a long established fact that a reader will be distracted</a> bythe readable content of a page when looking at its layout.'
     21>>> truncate_html_words(a, 8)
     22u'Fact: Ruby < <strong>Python</strong>.<br /> <a href="xyz.html" id="xyz">It <em>is</em> a long established ...</a>'
     23
     24>>> a = 'Fact: Ruby < <strong>Python</strong>.<br /> <a href="xyz.html"\\nid="xyz">It <em>is</em> a long established fact that a reader will be distracted</a> by the readable content of a page when looking at its layout.'
     25>>> truncate_html_words(a, 8)
     26u'Fact: Ruby < <strong>Python</strong>.<br /> <a href="xyz.html"\\nid="xyz">It <em>is</em> a long established ...</a>'
     27
     28>>> a = 'Fact: Ruby < <strong>Python</strong>.<br /> <a href="xyz.html"\\nid="xyz">It > <em>is</em> a long established fact that a reader will be distracted</a> by the readable content of a page when looking at its layout.'
     29>>> truncate_html_words(a, 8)
     30u'Fact: Ruby < <strong>Python</strong>.<br /> <a href="xyz.html"\\nid="xyz">It > <em>is</em> a long established ...</a>'
     31
     32# Oops. Not smart enough after all. But proper behavior according to code.
     33>>> a = 'It is a <long established fact> that a reader will be distracted by the readable content of a page when looking at its layout.'
     34>>> truncate_html_words(a, 8)
     35u'It is a <long established fact> that a reader will be ...</long>'
     36"""
     37
     38if __name__ == "__main__":
     39    import doctest
     40    doctest.testmod()
Back to Top