Code

Opened 7 years ago

Closed 7 years ago

#3714 closed (fixed)

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

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

Download all attachments as: .zip

Change History (7)

Changed 7 years ago by clelland@…

Fix for word wrap problem

comment:1 Changed 7 years ago by clelland@…

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

comment:2 Changed 7 years ago by SmileyChris

  • Needs tests set
  • Triage Stage changed from Unreviewed to Accepted

Could we have a test for this please?

Changed 7 years ago by clelland@…

Same patch, now with doctests

comment:3 Changed 7 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 7 years ago by SmileyChris

  • Triage Stage changed from Accepted to Ready for checkin

comment:5 Changed 7 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.