Skip to content

Commit

Permalink
Merge pull request #4552 from alphagov/cw-rel-fix
Browse files Browse the repository at this point in the history
Fix component wrapper rel option
  • Loading branch information
andysellick authored Jan 15, 2025
2 parents ac48418 + 4240110 commit f6227cb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down

0 comments on commit f6227cb

Please sign in to comment.