Opened 17 years ago

Closed 17 years ago

#3714 closed (fixed)

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

Reported by: Bryan Chow Owned by: Adrian Holovaty
Component: Core (Other) Version: dev
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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 (2)

text-wrap.patch (452 bytes ) - added by clelland@… 17 years ago.
Fix for word wrap problem
text-wrap-with-tests.patch (1.3 KB ) - added by clelland@… 17 years ago.
Same patch, now with doctests

Download all attachments as: .zip

Change History (7)

by clelland@…, 17 years ago

Attachment: text-wrap.patch added

Fix for word wrap problem

comment:1 by clelland@…, 17 years ago

Has patch: set

comment:2 by Chris Beaven, 17 years ago

Needs tests: set
Triage Stage: UnreviewedAccepted

Could we have a test for this please?

by clelland@…, 17 years ago

Attachment: text-wrap-with-tests.patch added

Same patch, now with doctests

comment:3 by clelland@…, 17 years ago

Needs tests: unset

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.

comment:4 by Chris Beaven, 17 years ago

Triage Stage: AcceptedReady for checkin

comment:5 by Russell Keith-Magee, 17 years ago

Resolution: fixed
Status: newclosed

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

Note: See TracTickets for help on using tickets.
Back to Top