Opened 4 years ago

Closed 3 years ago

#19496 closed New feature (fixed)

truncatechars_html

Reported by: Stefano Vettorazzi Campos Owned by: Daniele Procida
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 4 years ago by Russell Keith-Magee

Easy pickings: set
Triage Stage: UnreviewedAccepted

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

comment:2 Changed 4 years ago by Nick Sandford

Has patch: set
Owner: changed from nobody to Nick Sandford
Status: newassigned

comment:3 Changed 4 years ago by Nick Sandford

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 4 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 4 years ago by martmatwarne

Owner: changed from Nick Sandford to martmatwarne

comment:6 Changed 4 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 4 years ago by martmatwarne

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

comment:8 Changed 4 years ago by Daniele Procida

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 4 years ago by Daniele Procida

Owner: changed from martmatwarne to Daniele Procida

comment:10 Changed 3 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 3 years ago by Tim Graham

Easy pickings: unset
Version: 1.4master

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

comment:12 Changed 3 years 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 3 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

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