diff --git a/lib/consul_form_builder.rb b/lib/consul_form_builder.rb index e9868185e..620a0886c 100644 --- a/lib/consul_form_builder.rb +++ b/lib/consul_form_builder.rb @@ -23,7 +23,7 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder if options[:label] == false super 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( label: label, @@ -51,7 +51,7 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder if text == false super else - super(attribute, sanitize(label_text(attribute, text)), options) + super(attribute, sanitized_label_text(attribute, text), options) end end @@ -68,6 +68,14 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder 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) label_options = options[:label_options] || {} diff --git a/spec/components/shared/agree_with_terms_of_service_field_component_spec.rb b/spec/components/shared/agree_with_terms_of_service_field_component_spec.rb new file mode 100644 index 000000000..6b7c8bc7a --- /dev/null +++ b/spec/components/shared/agree_with_terms_of_service_field_component_spec.rb @@ -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 diff --git a/spec/lib/consul_form_builder_spec.rb b/spec/lib/consul_form_builder_spec.rb index d8d9adf5f..db57847d2 100644 --- a/spec/lib/consul_form_builder_spec.rb +++ b/spec/lib/consul_form_builder_spec.rb @@ -13,6 +13,17 @@ describe ConsulFormBuilder do 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 title') + + 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 it "does not generate hints by default" do render builder.text_field(:title) diff --git a/spec/system/verification/residence_spec.rb b/spec/system/verification/residence_spec.rb index c54e44fee..3603d4ba9 100644 --- a/spec/system/verification/residence_spec.rb +++ b/spec/system/verification/residence_spec.rb @@ -164,8 +164,9 @@ describe "Residence" do login_as(create(:user)) visit new_residence_path - click_link "the terms and conditions of access" - expect(page).to have_content "Terms and conditions of access of the Census" + 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" + end end end