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

Closed
lighthouse-import opened this Issue May 16, 2011 · 7 comments

Projects

None yet

1 participant

@lighthouse-import

Imported from Lighthouse. Original ticket at: http://rails.lighthouseapp.com/projects/8994/tickets/1597
Created by Gaius Centus Novus - 2011-02-17 08:07:22 UTC

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…".

Imported from Lighthouse.
Comment by Peter Wagenet - 2008-12-18 19:52:06 UTC

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.

Imported from Lighthouse.
Comment by Peter Wagenet - 2008-12-18 19:53:07 UTC

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

Imported from Lighthouse.
Comment by Pratik - 2008-12-22 00:45:30 UTC

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.

Imported from Lighthouse.
Comment by Gaius Centus Novus - 2008-12-22 00:59:39 UTC

CGI::unescapeHTML is completely borked. See my question on StackOverflow: http://stackoverflow.com/questions/378847/is-there-a-better-html-escaping-and-unescaping-tool-than-cgi-for-ruby

Imported from Lighthouse.
Comment by Jeremy Kemper - 2010-05-04 17:48:37 UTC

[bulk edit]

Imported from Lighthouse.
Comment by Ryan Bigg - 2010-11-08 01:53:48 UTC

Automatic cleanup of spam.

Attachments saved to Gist: http://gist.github.com/971597

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment