Django

Code

Ticket #3714 (closed: fixed)

Opened 1 year ago

Last modified 1 year ago

django.utils.text.wrap() does not properly handle indented text

Reported by: Bryan Chow Assigned to: adrian
Milestone: Component: Core framework
Version: SVN Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

wrap() in django.utils.text does not properly handle indented lines of text. When it encounters a line that begins with spaces, it fails to reset the character position counter and thus miscalculates the length of that line. This is due to the use of splitline() on line 20; substituting split('\n') for splitline() resolves this issue.

On line 14 of text.py, the text to be wrapped is split on spaces into words. Therefore, if a line is indented, a word ending with a line break will be encountered.

'word\n'.splitlines() returns ['word']

'word\n'.split('\n') returns ['word', '']

In the former case, the empty string representing the first word of the next line is disregarded, and thus the test on line 29 doesn't notice that a new line has started.

Attachments

text-wrap.patch (452 bytes) - added by clelland@gmail.com on 03/13/07 01:38:16.
Fix for word wrap problem
text-wrap-with-tests.patch (1.3 kB) - added by clelland@gmail.com on 03/13/07 11:36:23.
Same patch, now with doctests

Change History

03/13/07 01:38:16 changed by clelland@gmail.com

  • attachment text-wrap.patch added.

Fix for word wrap problem

03/13/07 01:58:26 changed by clelland@gmail.com

  • needs_better_patch changed.
  • has_patch set to 1.
  • needs_tests changed.
  • needs_docs changed.

03/13/07 02:44:55 changed by SmileyChris

  • needs_tests set to 1.
  • stage changed from Unreviewed to Accepted.

Could we have a test for this please?

03/13/07 11:36:23 changed by clelland@gmail.com

  • attachment text-wrap-with-tests.patch added.

Same patch, now with doctests

03/13/07 11:48:48 changed by clelland@gmail.com

  • needs_tests deleted.

I've added a couple of doctests with this attachment, testing for the indentation behaviour, as well as looking for off-by-one wrapping errors.

03/13/07 15:05:47 changed by SmileyChris

  • stage changed from Accepted to Ready for checkin.

03/20/07 06:07:52 changed by russellm

  • status changed from new to closed.
  • resolution set to fixed.

(In [4753]) Fixed #3714 -- Fixed handling of indented text in wordwrap template filter. Thanks for the fix, Ian Clelland.


Add/Change #3714 (django.utils.text.wrap() does not properly handle indented text)




Change Properties
Action