Skip to content

Commit

Permalink
Apply consistent symbol/string attribute transformation for Hash values
Browse files Browse the repository at this point in the history
Symbol attribute keys are dasherized before rendering, whereas string
keys are passed through verbatim.

However if the value is a Hash, the dasherize transform will not be
applied to the key even if it is a symbol. This change makes it so the
dasherization is applied even in this case.

```rb
a(data_turbo: { method: "post", confirm: "Are you sure?" })
```

Should render

```html
<a data-turbo-method="post" data-turbo-confirm="Are you sure?"></a>
```
  • Loading branch information
willcosgrove committed Dec 5, 2023
1 parent 6a0eb85 commit bd65bd5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@ def __build_attributes__(attributes, buffer:)
__build_attributes__(
v.transform_keys { |subkey|
case subkey
when Symbol then"#{k}-#{subkey.name.tr('_', '-')}"
else "#{k}-#{subkey}"
when Symbol then"#{name}-#{subkey.name.tr('_', '-')}"
else "#{name}-#{subkey}"
end
}, buffer: buffer
)
Expand Down
48 changes: 48 additions & 0 deletions test/phlex/view/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@
expect(output).to be == %(<#{tag} class="class" id="id" disabled>content</#{tag}>)
end
end

with "<#{tag}> with string attribute keys" do
view do
define_method :template do
send(method_name, "attribute_with_underscore" => true) { "content" }
end
end

it "produces the correct output" do
expect(output).to be == %(<#{tag} attribute_with_underscore>content</#{tag}>)
end
end

with "<#{tag}> with hash attribute values" do
view do
define_method :template do
send(method_name, aria: { hidden: true }, data_turbo: { frame: "_top" }) { "content" }
end
end

it "produces the correct output" do
expect(output).to be == %(<#{tag} aria-hidden data-turbo-frame="_top">content</#{tag}>)
end
end
end

Phlex::HTML::VoidElements.registered_elements.each do |method_name, tag|
Expand Down Expand Up @@ -73,5 +97,29 @@
expect(output).to be == %(<#{tag} class="class" id="id" disabled>)
end
end

with "<#{tag}> with string attribute keys" do
view do
define_method :template do
send(method_name, "attribute_with_underscore" => true)
end
end

it "produces the correct output" do
expect(output).to be == %(<#{tag} attribute_with_underscore>)
end
end

with "<#{tag}> with hash attribute values" do
view do
define_method :template do
send(method_name, aria: { hidden: true }, data_turbo: { frame: "_top" })
end
end

it "produces the correct output" do
expect(output).to be == %(<#{tag} aria-hidden data-turbo-frame="_top">)
end
end
end
end

0 comments on commit bd65bd5

Please sign in to comment.