Skip to content

Commit

Permalink
Merge pull request #420 from joeldrapper/revert-419-revert-418-data_s…
Browse files Browse the repository at this point in the history
…tring_keys

Render data hash with string keys, take 2
  • Loading branch information
joeldrapper authored Jan 7, 2023
2 parents 86247d7 + 5b2ee7a commit 311fc1b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/phlex/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,14 @@ def capture(&block)
when Symbol
buffer << " " << name << '="' << ERB::Util.html_escape(v.name) << '"'
when Hash
_build_attributes(v.transform_keys { "#{k}-#{_1.name.tr('_', '-')}" }, buffer: buffer)
_build_attributes(
v.transform_keys { |subkey|
case subkey
when Symbol then"#{k}-#{subkey.name.tr('_', '-')}"
else "#{k}-#{subkey}"
end
}, buffer: buffer
)
else
buffer << " " << name << '="' << ERB::Util.html_escape(v.to_s) << '"'
end
Expand Down
12 changes: 12 additions & 0 deletions test/phlex/view/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ def template
end
end

with "string keyed hash attributes" do
view do
def template
div data: { "name_first_name" => "Joel" }
end
end

it "flattens the attributes without dasherizing them" do
expect(output).to be == %(<div data-name_first_name="Joel"></div>)
end
end

if RUBY_ENGINE == "ruby"
with "unique tag attributes" do
view do
Expand Down

0 comments on commit 311fc1b

Please sign in to comment.