Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix anchoring in ID validation regex. #3956

Merged
merged 2 commits into from Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -60,7 +60,7 @@ def set_lang(lang)
def check_id_is_valid(id)
return if id.blank?

raise(ArgumentError, "Id (#{id}) cannot start with a number or contain whitespace and can only contain letters, digits, `_` and `-`") unless /^[a-zA-Z][\w:-]*$/.match?(id)
raise(ArgumentError, "Id (#{id}) cannot start with a number or contain whitespace and can only contain letters, digits, `_` and `-`") unless /\A[a-zA-Z][\w:-]*\z/.match?(id)
sengi marked this conversation as resolved.
Show resolved Hide resolved
end

def check_data_attributes_are_valid(attributes)
Expand Down
Expand Up @@ -49,22 +49,18 @@
end

it "does not accept invalid ids" do
["2idstartingwithanumber", "id containing spaces", "idwitha.character"].each do |id|
error = "Id (#{id}) cannot start with a number or contain whitespace and can only contain letters, digits, `_` and `-`"
["1dstartingwithnumber", "id with spaces", "idwith.dot", "id\nwithnewline"].each do |id|
expect {
GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(id:)
}.to raise_error(ArgumentError, error)
}.to raise_error(ArgumentError, / contain/)
end
end

it "does not accept invalid ids when passed" do
["2idstartingwithanumber", "id containing spaces", "idwitha.character"].each do |id|
error = "Id (#{id}) cannot start with a number or contain whitespace and can only contain letters, digits, `_` and `-`"
expect {
helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(id: "original")
helper.set_id(id)
}.to raise_error(ArgumentError, error)
end
expect {
helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(id: "valid")
helper.set_id("not. a. valid. id")
}.to raise_error(ArgumentError)
end

it "can add a class to already passed classes" do
Expand Down