Merge pull request #5241 from betagouv/fix-links-detection

Usager : correction de certains textes faussement détectés comme des liens
This commit is contained in:
Pierre de La Morinerie 2020-06-22 15:13:27 +02:00 committed by GitHub
commit 797b8afba0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 3 deletions

View file

@ -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, '<a target="_blank" rel="noopener" href="\0">\0</a>')
with_links = Anchored::Linker.auto_link(html_formatted, target: '_blank', rel: 'noopener')
sanitize(with_links, attributes: ['target', 'rel', 'href'])
end
end

View file

@ -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("<p>Cliquez sur <a href=\"https://d-s.fr\" target=\"_blank\" rel=\"noopener\">https://d-s.fr</a> pour continuer.</p>") }
end
it { is_expected.to eq("<p><a target=\"_blank\" rel=\"noopener\" href=\"https://d-s.fr\">https://d-s.fr</a></p>") }
context "using a non-authorized scheme" do
let(:description) { "Cliquez sur file://etc/password pour continuer." }
it { is_expected.to eq("<p>Cliquez sur file://etc/password pour continuer.</p>") }
end
context "not actually an URL" do
let(:description) { "Pour info: il ne devrait y avoir aucun lien." }
it { is_expected.to eq("<p>Pour info: il ne devrait y avoir aucun lien.</p>") }
end
end
context "with empty decription" do