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

raise on non-symbolic and non-lowercase :id #740

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ def __attributes__(attributes, buffer = +"")
raise ArgumentError.new("Unsafe attribute name detected: #{k}.")
end

if lower_name.to_sym == :id && k != :id
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it might be slightly faster to have a set of invalid IDs that we check for inclusion in, e.g.:

INVALID_IDS = Set[:iD, :Id, :ID, "id", "iD", "Id", "ID"].freeze

Might be worth benchmarking sometime.

raise ArgumentError.new(":id attribute should only be passed as a lowercase symbol.")
end

case v
when true
buffer << " " << name
Expand Down
29 changes: 29 additions & 0 deletions test/phlex/view/lowercase_symbolic_id_guard.test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

describe Phlex::SGML do
extend ViewHelper

with "non lowercase id" do
view do
def view_template
div(iD: "a")
end
end

it "raises" do
expect { output }.to raise_exception(Phlex::ArgumentError)
end
end

with "non symbolic id" do
view do
def view_template
div("id" => "a")
end
end

it "raises" do
expect { output }.to raise_exception(Phlex::ArgumentError)
end
end
end
Loading