Allow links in forms to open in new tabs

We used to open these links in new tabs, but accidentally stopped doing
so in commit 75a28fafc.

While, in general, automatically opening a link in a new tab/window is a
bad idea, the exception comes when people are filling in a form and
there are links to pages that contain information which will help them
fill in a form.

There are mainly two advantages of this approach. First, it makes less
likely for people to accidentally lose the information they were filling
in. And, second, having both the form and a help page open at the same
time can make it easier to fill in the form.

However, opening these links in new tabs also has disadvantages, like
taking control away from people or making it harder to navigate through
pages when using a mobile phone.

So this is a compromise solution.
This commit is contained in:
Javi Martín
2023-10-15 16:04:02 +02:00
parent 46f43236bb
commit cdf859621e
4 changed files with 45 additions and 4 deletions

View File

@@ -23,7 +23,7 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder
if options[:label] == false if options[:label] == false
super super
else else
label = tag.span sanitize(label_text(attribute, options[:label])), class: "checkbox" label = tag.span sanitized_label_text(attribute, options[:label]), class: "checkbox"
super(attribute, options.merge( super(attribute, options.merge(
label: label, label: label,
@@ -51,7 +51,7 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder
if text == false if text == false
super super
else else
super(attribute, sanitize(label_text(attribute, text)), options) super(attribute, sanitized_label_text(attribute, text), options)
end end
end end
@@ -68,6 +68,14 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder
end end
end end
def sanitized_label_text(attribute, text)
sanitize(label_text(attribute, text), attributes: allowed_attributes)
end
def allowed_attributes
self.class.sanitized_allowed_attributes + ["target"]
end
def label_options_for(options) def label_options_for(options)
label_options = options[:label_options] || {} label_options = options[:label_options] || {}

View File

@@ -0,0 +1,21 @@
require "rails_helper"
describe Shared::AgreeWithTermsOfServiceFieldComponent do
before do
dummy_model = Class.new do
include ActiveModel::Model
attr_accessor :terms_of_service
end
stub_const("DummyModel", dummy_model)
end
let(:form) { ConsulFormBuilder.new(:dummy, DummyModel.new, ApplicationController.new.view_context, {}) }
let(:component) { Shared::AgreeWithTermsOfServiceFieldComponent.new(form) }
it "contains links that open in a new window" do
render_inline component
expect(page).to have_css "a[target=_blank]", count: 2
end
end

View File

@@ -13,6 +13,17 @@ describe ConsulFormBuilder do
let(:builder) { ConsulFormBuilder.new(:dummy, DummyModel.new, ApplicationController.new.view_context, {}) } let(:builder) { ConsulFormBuilder.new(:dummy, DummyModel.new, ApplicationController.new.view_context, {}) }
describe "label" do
it "accepts links that open in a new window in its content" do
render builder.text_field(:title, label: 'My <a href="/" target="_blank" title="New tab">title</a>')
expect(page).to have_link count: 1
expect(page).to have_link "title"
expect(page).to have_link "New tab"
expect(page).to have_css "a[target=_blank]"
end
end
describe "hints" do describe "hints" do
it "does not generate hints by default" do it "does not generate hints by default" do
render builder.text_field(:title) render builder.text_field(:title)

View File

@@ -164,8 +164,9 @@ describe "Residence" do
login_as(create(:user)) login_as(create(:user))
visit new_residence_path visit new_residence_path
click_link "the terms and conditions of access"
within_window(window_opened_by { click_link "the terms and conditions of access" }) do
expect(page).to have_content "Terms and conditions of access of the Census" expect(page).to have_content "Terms and conditions of access of the Census"
end end
end
end end