Commits

Ned Jackson Lovely  committed 8f0c8ee

Fix to #93, this time respecting whitespace

Rejigger of title split to respect whitespace, also capitalize follow-on
hyphenated words.

  • Participants
  • Parent commits 1305cfd

Comments (0)

Files changed (2)

File jinja2/filters.py

     """Return a titlecased version of the value. I.e. words will start with
     uppercase letters, all remaining characters are lowercase.
     """
-    return soft_unicode(s).title()
+    rv = []
+    for item in re.compile(r'([-\s]+)(?u)').split(s):
+        if not item:
+            continue
+        rv.append(item[0].upper() + item[1:])
+    return ''.join(rv)
 
 
 def do_dictsort(value, case_sensitive=False, by='key'):

File jinja2/testsuite/filters.py

     def test_title(self):
         tmpl = env.from_string('''{{ "foo bar"|title }}''')
         assert tmpl.render() == "Foo Bar"
+        tmpl = env.from_string('''{{ "foo's bar"|title }}''')
+        assert tmpl.render() == "Foo's Bar"
+        tmpl = env.from_string('''{{ "foo   bar"|title }}''')
+        assert tmpl.render() == "Foo   Bar"
+        tmpl = env.from_string('''{{ "f bar f"|title }}''')
+        assert tmpl.render() == "F Bar F"
+        tmpl = env.from_string('''{{ "foo-bar"|title }}''')
+        assert tmpl.render() == "Foo-Bar"
+        tmpl = env.from_string('''{{ "foo\tbar"|title }}''')
+        assert tmpl.render() == "Foo\tBar"
 
     def test_truncate(self):
         tmpl = env.from_string(