From c8c916e6570ac70005254ab8952cecde4e78fbe8 Mon Sep 17 00:00:00 2001 From: FND Date: Tue, 10 Dec 2024 09:41:06 +0100 Subject: [PATCH] Change URL truncation to account for ellipses (#33229) --- app/lib/text_formatter.rb | 6 ++++++ spec/lib/text_formatter_spec.rb | 22 +++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/lib/text_formatter.rb b/app/lib/text_formatter.rb index 7fbb6fce12..c1b0a81652 100644 --- a/app/lib/text_formatter.rb +++ b/app/lib/text_formatter.rb @@ -68,6 +68,12 @@ class TextFormatter suffix = url[prefix.length + 30..] cutoff = url[prefix.length..].length > 30 + if suffix && suffix.length == 1 # revert truncation to account for ellipsis + display_url += suffix + suffix = nil + cutoff = false + end + tag.a href: url, target: '_blank', rel: rel.join(' '), translate: 'no' do tag.span(prefix, class: 'invisible') + tag.span(display_url, class: (cutoff ? 'ellipsis' : '')) + diff --git a/spec/lib/text_formatter_spec.rb b/spec/lib/text_formatter_spec.rb index bde17bb79c..a71655ed2e 100644 --- a/spec/lib/text_formatter_spec.rb +++ b/spec/lib/text_formatter_spec.rb @@ -50,7 +50,7 @@ RSpec.describe TextFormatter do end end - context 'when given a stand-alone google URL' do + context 'when given a stand-alone Google URL' do let(:text) { 'http://google.com' } it 'matches the full URL' do @@ -280,6 +280,26 @@ RSpec.describe TextFormatter do end end + context 'when given a lengthy URL' do + let(:text) { 'lorem https://prepitaph.org/wip/web-dovespair/ ipsum' } + + it 'truncates the URL' do + expect(subject).to include '' + expect(subject).to include 'prepitaph.org/wip/web-dovespai' + expect(subject).to include '' + end + end + + context 'when given a sufficiently short URL' do + let(:text) { 'lorem https://prepitaph.org/wip/web-devspair/ ipsum' } + + it 'does not truncate the URL' do + expect(subject).to include '' + expect(subject).to include 'prepitaph.org/wip/web-devspair/' + expect(subject).to include '' + end + end + context 'when given text containing a hashtag' do let(:text) { '#hashtag' }