Opened 3 years ago

Closed 19 months ago

#19496 closed New feature (fixed)

truncatechars_html

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

Description

Hi,
why don't have something like truncatewords_html for chars?
I don't have experience with the Django's source code, so I don't have a patch. But I can try if you want.

Change History (13)

comment:1 Changed 3 years ago by russellm

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

Sure; we've added truncatedchars; makes sense that there would be a HTML-sensitive equivalent.

comment:2 Changed 3 years ago by slurms

  • Has patch set
  • Owner changed from nobody to slurms
  • Status changed from new to assigned

comment:3 Changed 3 years ago by slurms

Used truncatewords_html as a guideline, added docs & tests. All tests pass under sqlite. Pull request here: https://github.com/django/django/pull/633

comment:4 Changed 3 years ago by DHessing

  • Patch needs improvement set
>>> truncatechars_html('<h1>test</h1>', 5)
u'<h1>t ...</h1>'

Should return u'<h1>te...</h1>'. Truncatewords adds an extra space in front of the ellipsis but truncatechars doesn't.

comment:5 Changed 2 years ago by martmatwarne

  • Owner changed from slurms to martmatwarne

comment:6 Changed 2 years ago by martmatwarne

The above issue should now be fixed. It also appears to have broken truncatechars which I've now fixed

https://github.com/django/django/pull/1126

comment:7 Changed 2 years ago by martmatwarne

Actually there still appears to be issues when using html with only one tag pair in it

comment:8 Changed 2 years ago by EvilDMP

I have marked this ticket as suitable for a first-time committer attending a Don't be afraid to commit workshop.

The next planned session will be hosted by Cardiff Dev Workshop on Saturday 8th June.

If you want to tackle this ticket before then, or at any time in fact, please don't let the fact that it's assigned to me stop you. Feel free to re-assign it to yourself and do whatever you like to it.

comment:9 Changed 2 years ago by EvilDMP

  • Owner changed from martmatwarne to EvilDMP

comment:10 Changed 2 years ago by saturn5971

truncatechars_html was incorrectly adding the '...' to text that was under the truncate length but only by 3 or fewer characters:

>>> truncatechars_html('abcd', 4)
u'a...'

I've provided an updated patch here, including an addition to the test:

https://github.com/django/django/pull/1689

comment:11 Changed 23 months ago by timo

  • Easy pickings unset
  • Version changed from 1.4 to master

I've left some comments on the PR for improvement.

comment:12 Changed 22 months ago by saturn5971

  • Cc saturn5971 added
  • Keywords truncatechars added; truncatchars removed

Thanks! I updated my pull request in accord with Timo's suggestions.

https://github.com/django/django/pull/1689

To eliminate redundant code between _html_chars and _html_words, I've collapsed them into a single function, _truncate_html. I made the requested documentation changes as well.

comment:13 Changed 19 months ago by Tim Graham <timograham@…>

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

In f94f466cd3461527fb76a3e8951039a3c2388829:

Fixed #19496 -- Added truncatechars_html filter.

Thanks esevece for the suggestion and Nick Sandford and Martin Warne
for the inital work on the patch.

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