From bd65bd53a17d006190bc9d02173c329cfb6a1c83 Mon Sep 17 00:00:00 2001 From: Will Cosgrove Date: Tue, 5 Dec 2023 16:49:57 -0600 Subject: [PATCH] Apply consistent symbol/string attribute transformation for Hash values 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 ``` --- lib/phlex/sgml.rb | 4 ++-- test/phlex/view/tags.rb | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/lib/phlex/sgml.rb b/lib/phlex/sgml.rb index a7465894..f800fad8 100644 --- a/lib/phlex/sgml.rb +++ b/lib/phlex/sgml.rb @@ -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 ) diff --git a/test/phlex/view/tags.rb b/test/phlex/view/tags.rb index fac7e124..7c1efe78 100644 --- a/test/phlex/view/tags.rb +++ b/test/phlex/view/tags.rb @@ -43,6 +43,30 @@ expect(output).to be == %(<#{tag} class="class" id="id" disabled>content) 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) + 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) + end + end end Phlex::HTML::VoidElements.registered_elements.each do |method_name, tag| @@ -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