Opened 10 years ago

Closed 10 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: master
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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@… 10 years ago.
Fix for word wrap problem
text-wrap-with-tests.patch (1.3 KB) - added by clelland@… 10 years ago.
Same patch, now with doctests

Download all attachments as: .zip

Change History (7)

Changed 10 years ago by clelland@…

Attachment: text-wrap.patch added

Fix for word wrap problem

comment:1 Changed 10 years ago by clelland@…

Has patch: set
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

comment:2 Changed 10 years ago by Chris Beaven

Needs tests: set
Triage Stage: UnreviewedAccepted

Could we have a test for this please?

Changed 10 years ago by clelland@…

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

Same patch, now with doctests

comment:3 Changed 10 years ago by clelland@…

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 Changed 10 years ago by Chris Beaven

Triage Stage: AcceptedReady for checkin

comment:5 Changed 10 years ago by Russell Keith-Magee

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