diff --git a/lib/govuk_publishing_components/presenters/component_wrapper_helper.rb b/lib/govuk_publishing_components/presenters/component_wrapper_helper.rb index 354cc51e7b..620b8aaaa2 100644 --- a/lib/govuk_publishing_components/presenters/component_wrapper_helper.rb +++ b/lib/govuk_publishing_components/presenters/component_wrapper_helper.rb @@ -240,7 +240,8 @@ def check_rel_is_valid(rel_attribute) return if rel_attribute.blank? options = %w[alternate author bookmark canonical dns-prefetch external expect help icon license manifest me modulepreload next nofollow noopener noreferrer opener pingback preconnect prefetch preload prerender prev privacy-policy search stylesheet tag terms-of-service] - unless options.include? rel_attribute + rel_array = rel_attribute.split(" ") + unless rel_array.all? { |r| options.include? r } raise(ArgumentError, "rel attribute (#{rel_attribute}) is not recognised") end end diff --git a/spec/lib/govuk_publishing_components/components/component_wrapper_helper_spec.rb b/spec/lib/govuk_publishing_components/components/component_wrapper_helper_spec.rb index 590ea2c1cc..490a7c2f77 100644 --- a/spec/lib/govuk_publishing_components/components/component_wrapper_helper_spec.rb +++ b/spec/lib/govuk_publishing_components/components/component_wrapper_helper_spec.rb @@ -396,11 +396,23 @@ }.to raise_error(ArgumentError, error) end - it "accepts valid rel value" do + it "accepts a valid rel value" do component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(rel: "nofollow") expect(component_helper.all_attributes[:rel]).to eql("nofollow") end + it "accepts multiple valid rel values" do + component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(rel: "nofollow noreferrer") + expect(component_helper.all_attributes[:rel]).to eql("nofollow noreferrer") + end + + it "does not accept multiple rel values if one of them is invalid" do + error = "rel attribute (nofollow noreferrer potato) is not recognised" + expect { + GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(rel: "nofollow noreferrer potato") + }.to raise_error(ArgumentError, error) + end + it "can set a rel, overriding a passed value" do helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(rel: "nofollow") helper.set_rel("noreferrer") @@ -412,6 +424,12 @@ helper.add_rel("noreferrer") expect(helper.all_attributes[:rel]).to eql("nofollow noreferrer") end + + it "can add multiple rels to an existing rel" do + helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(rel: "nofollow") + helper.add_rel("noreferrer external") + expect(helper.all_attributes[:rel]).to eql("nofollow noreferrer external") + end end describe "margins" do