diff --git a/app/helpers/string_to_html_helper.rb b/app/helpers/string_to_html_helper.rb index 1bdb188f0..8806863f6 100644 --- a/app/helpers/string_to_html_helper.rb +++ b/app/helpers/string_to_html_helper.rb @@ -1,7 +1,7 @@ module StringToHtmlHelper def string_to_html(str, wrapper_tag = 'p') html_formatted = simple_format(str, {}, { wrapper_tag: wrapper_tag }) - with_links = html_formatted.gsub(URI.regexp, '\0') + with_links = Anchored::Linker.auto_link(html_formatted, target: '_blank', rel: 'noopener') sanitize(with_links, attributes: ['target', 'rel', 'href']) end end diff --git a/spec/helpers/string_to_html_helper_spec.rb b/spec/helpers/string_to_html_helper_spec.rb index 34f43e0f0..847b6def7 100644 --- a/spec/helpers/string_to_html_helper_spec.rb +++ b/spec/helpers/string_to_html_helper_spec.rb @@ -9,9 +9,20 @@ RSpec.describe StringToHtmlHelper, type: :helper do end context "with a link" do - let(:description) { "https://d-s.fr" } + context "using an authorized scheme" do + let(:description) { "Cliquez sur https://d-s.fr pour continuer." } + it { is_expected.to eq("

Cliquez sur https://d-s.fr pour continuer.

") } + end - it { is_expected.to eq("

https://d-s.fr

") } + context "using a non-authorized scheme" do + let(:description) { "Cliquez sur file://etc/password pour continuer." } + it { is_expected.to eq("

Cliquez sur file://etc/password pour continuer.

") } + end + + context "not actually an URL" do + let(:description) { "Pour info: il ne devrait y avoir aucun lien." } + it { is_expected.to eq("

Pour info: il ne devrait y avoir aucun lien.

") } + end end context "with empty decription" do