fix(announces): link rendering not catched by turbo and fix a11y attributes

This commit is contained in:
Colin Darie 2023-11-14 12:21:06 +01:00
parent 8051beb068
commit 99fe4694af
3 changed files with 35 additions and 2 deletions

View file

@ -27,4 +27,18 @@ module ReleaseNotesHelper
ReleaseNote.default_categories_for_role(:usager)
end
end
def render_release_note_content(content)
allowed_attributes.merge ["rel", "target"] # title already allowed
content.body.fragment.source.css("a[href]").each do |link|
uri = URI.parse(link['href'])
link.set_attribute('rel', 'noreferrer noopener')
link.set_attribute('target', '_blank')
link.set_attribute('title', new_tab_suffix(uri.host))
end
content
end
end

View file

@ -2,9 +2,9 @@
%h3= l(notes[0].released_on, format: :long)
- notes.each do |note|
.fr-mb-4w.fr-px-2w.fr-py-2w.fr-background-alt--grey
.fr-mb-4w.fr-px-2w.fr-py-2w.fr-background-alt--grey{ data: { turbo: "false" } }
%p
- note.categories.each do |category|
= announce_category_badge(category)
= note.body
= render_release_note_content(note.body)

View file

@ -0,0 +1,19 @@
RSpec.describe ReleaseNotesHelper, type: :helper do
describe "#render_release_note_content" do
let(:release_note) { build(:release_note) }
it "adds noreferrer, noopener, and target to absolute links" do
release_note.body = "Go to <a href='http://example.com'>Example</a>"
processed_content = helper.render_release_note_content(release_note.body)
expect(processed_content.body.to_s).to include('Go to <a href="http://example.com" rel="noreferrer noopener" target="_blank" title="example.com — Nouvel onglet">Example</a>')
end
it "handles content without links" do
release_note.body = "No links here"
processed_content = helper.render_release_note_content(release_note.body)
expect(processed_content.body.to_s).to include("No links here")
end
end
end