Merge pull request #8798 from colinux/fix-simple-renderer-missing-title

a11y: ajoute l'attribut title précisant l'ouverture dans un nouvel onglet dans le simple renderer
This commit is contained in:
Colin Darie 2023-03-22 15:47:17 +00:00 committed by GitHub
commit a96ccba92c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 7 deletions

View file

@ -28,15 +28,11 @@ class SimpleFormatComponent < ApplicationComponent
.join("\n\n") # .join("\n\n") #
@allow_a = allow_a @allow_a = allow_a
@renderer = Redcarpet::Markdown.new( @renderer = Redcarpet::Markdown.new(
Redcarpet::BareRenderer.new(link_attributes: external_link_attributes, class_names_map: class_names_map), Redcarpet::BareRenderer.new(class_names_map:),
REDCARPET_EXTENSIONS.merge(autolink: @allow_a) REDCARPET_EXTENSIONS.merge(autolink: @allow_a)
) )
end end
def external_link_attributes
{ target: '_blank', rel: 'noopener noreferrer' }
end
def tags def tags
if @allow_a if @allow_a
Rails.configuration.action_view.sanitized_allowed_tags + ['a'] Rails.configuration.action_view.sanitized_allowed_tags + ['a']
@ -46,6 +42,6 @@ class SimpleFormatComponent < ApplicationComponent
end end
def attributes def attributes
['target', 'rel', 'href', 'class'] ['target', 'rel', 'href', 'class', 'title']
end end
end end

View file

@ -142,7 +142,7 @@ module ApplicationHelper
end end
def new_tab_suffix(title) def new_tab_suffix(title)
"#{title}#{I18n.t('utils.new_tab')}" [title, I18n.t('utils.new_tab')].compact.join(' — ')
end end
def download_details(attachment) def download_details(attachment)

View file

@ -1,6 +1,7 @@
module Redcarpet module Redcarpet
class BareRenderer < Redcarpet::Render::HTML class BareRenderer < Redcarpet::Render::HTML
include ActionView::Helpers::TagHelper include ActionView::Helpers::TagHelper
include ApplicationHelper
# won't use rubocop tag method because it is missing output buffer # won't use rubocop tag method because it is missing output buffer
# rubocop:disable Rails/ContentTag # rubocop:disable Rails/ContentTag
@ -16,6 +17,17 @@ module Redcarpet
def paragraph(text) def paragraph(text)
content_tag(:p, text, { class: @options[:class_names_map].fetch(:paragraph) {} }, false) content_tag(:p, text, { class: @options[:class_names_map].fetch(:paragraph) {} }, false)
end end
def link(href, title, content)
content_tag(:a, content, { href:, title: new_tab_suffix(title), **external_link_attributes }, false)
end
def autolink(link, link_type)
return super unless link_type == :url
link(link, nil, link)
end
# rubocop:enable Rails/ContentTag # rubocop:enable Rails/ContentTag
end end
end end

View file

@ -64,6 +64,11 @@ TEXT
context 'enabled' do context 'enabled' do
let(:allow_a) { true } let(:allow_a) { true }
it { expect(page).to have_selector("a") } it { expect(page).to have_selector("a") }
it "inject expected attributes" do
link = page.find_link("https://www.demarches-simplifiees.fr").native
expect(link[:rel]).to eq("noopener noreferrer")
expect(link[:title]).to eq("Nouvel onglet")
end
end end
context 'disabled' do context 'disabled' do