Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActionView::Helpers::TextHelper#truncate should understand HTML encoding.

#1597

I'd like to use the HTML ellipsis (…) as my :omission in #truncate. The problem is that #truncate views that as a string of length 7, not of length 1. That is,


truncate('A really long string', :length => 10, :omission => '…')
# => "A r…"

But since … is really a single character in HTML, it should output "A real…".

Reported by Gaius Centus Novus · December 18th, 2008 @ 05:13 PM

State: incomplete
Milestone: 3.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Peter Wagenet
    Peter Wagenet
    • Tag changed from html, truncate, view to html, patch, tested, truncate, view

    The problem here is a bit more complicated that it seems. First, what if the user doesn't plan to format as HTML? I know that's an odd case, but should it be supported? If so, how do we know if "…" should be counted as one character or seven? If we do assume that it will always be output as HTML then counting it as one character is simple, as you can see in my attached patch. However, my patch does nothing about the case where we have special characters in the string to be truncated. Even worse, we could end up truncating in the middle of a special character which would be far from desired. There's probably the need for some additional work to be done here.

    December 18th, 2008 @ 07:52 PM

  2. Peter Wagenet
    Peter Wagenet

    The ellipses in my comment should actually be the HTML string written out.

    December 18th, 2008 @ 07:53 PM

  3. Pratik
    Pratik
    • Assigned user set to Pratik
    • State changed from new to incomplete

    We could have used CGI::unescapeHTML here, except it doesn't seem to be working with &#8230. So if there is a rack equivalent of that, it could work here.

    Thanks.

    December 22nd, 2008 @ 12:45 AM

  4. Gaius Centus Novus
    Gaius Centus Novus

    CGI::unescapeHTML is completely borked. See my question on StackOverflow: http://stackoverflow.com/questio...

    December 22nd, 2008 @ 12:59 AM

  5. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  6. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    November 8th, 2010 @ 01:53 AM

  7. bingbing