Opened 7 years ago

Closed 6 years ago

Last modified 4 years ago

#9883 closed (fixed)

the linebreaks filter strips leading and trailing whitespace from paragraphs

Reported by: akaihola Owned by: keithb
Component: Template system Version: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: UI/UX:

Description

The documentation for linebreaks says:

Replaces line breaks in plain text with appropriate HTML; a single newline becomes an HTML
line break (<br />) and a new line followed by a blank line becomes a paragraph break (</p>).

For example:
  {{ value|linebreaks }}
If value is Joel\nis a slug, the output will be <p>Joel<br>is a slug</p>.

However, {{value|linebreaks}} does more than that: it strips leading whitespace from the first line and trailing whitespace from the last line of every paragraph:

>>> linebreaks('  one  \n  two  \n  three  \n  four  \n\n  eins  \n  zwei  \n  drei  \n')
<p>one  <br />  two  <br />  three  <br />  four</p>

<p>eins  <br />  zwei  <br />  drei</p>

I admit this rarely is a problem, but I see no reason why it should be doing that (and not mention it in the docs).

My obscure use case is outputting monospace text where some tables are laid out using the 'NO-BREAK SPACE' (U+00A0) Unicode character, which Python's .strip() happily strips away. If a paragraph starts with a table and the first row is indented, the output is misformatted. Using linebreaksbr or converting U+00A0 to &nbsp; are my possible work-arounds, but it would been nice if the Django documentation had warned me :)

Attachments (1)

no_strip.diff (767 bytes) - added by keithb 6 years ago.

Download all attachments as: .zip

Change History (10)

comment:1 Changed 7 years ago by jacob

  • milestone set to 1.1
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Changed 6 years ago by keithb

comment:2 Changed 6 years ago by keithb

  • Has patch set
  • Resolution set to fixed
  • Status changed from new to closed
  • Triage Stage changed from Accepted to Ready for checkin

James found the original commit back from prehistoric Django times and found no good reason to do the stripping. no_strip.diff removes the strip() call in both normal and autoescape code paths.

Tests that utilize this functionality still pass.

comment:3 Changed 6 years ago by keithb

  • Resolution fixed deleted
  • Status changed from closed to reopened

oops. went too far with the ticket state.

comment:4 Changed 6 years ago by keithb

  • Owner changed from nobody to keithb
  • Status changed from reopened to new

comment:5 Changed 6 years ago by keithb

  • Status changed from new to assigned

comment:6 Changed 6 years ago by gwilson

  • Needs tests set
  • Patch needs improvement set
  • Triage Stage changed from Ready for checkin to Accepted

Let's have a test that shows the problem.

comment:7 Changed 6 years ago by jacob

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

(In [10225]) Fixed #9883: no longer do strange things with whitespace in the linebreaks filter. Thanks, keithb.

comment:8 Changed 6 years ago by jacob

(In [10292]) [1.0.X] Fixed #9883: no longer do strange things with whitespace in the linebreaks filter. Thanks, keithb.

Backport of r10225 from trunk.

comment:9 Changed 4 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

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