helpers: prevent some texts to be incorrectly detected as links

Users were having issues with texts like:

> Pour info: penser à faire cette action.

where `info:` was detected as being an URI.
This commit is contained in:
Pierre de La Morinerie 2020-06-22 12:42:10 +00:00
parent 9cb2510b29
commit af5b36ea6a
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