From 834e7630d00e301086a867f087d962f1eaf766fd Mon Sep 17 00:00:00 2001 From: Stephen Ierodiaconou Date: Tue, 8 Jul 2025 15:04:42 +0000 Subject: [PATCH 1/2] Allow SGML attributes to accept objects which respond to to_h --- lib/phlex/sgml.rb | 2 ++ quickdraw/sgml/attributes.test.rb | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/phlex/sgml.rb b/lib/phlex/sgml.rb index 57941ef3..9963f2be 100644 --- a/lib/phlex/sgml.rb +++ b/lib/phlex/sgml.rb @@ -508,6 +508,8 @@ def json_escape(string) end when Phlex::SGML::SafeObject v.to_s.gsub('"', """) + when -> (v) { v.respond_to?(:to_h) } + __nested_attributes__(v.to_h, "#{name}-", buffer) else raise Phlex::ArgumentError.new("Invalid attribute value for #{k}: #{v.inspect}.") end diff --git a/quickdraw/sgml/attributes.test.rb b/quickdraw/sgml/attributes.test.rb index bfbd4f22..96545d5b 100644 --- a/quickdraw/sgml/attributes.test.rb +++ b/quickdraw/sgml/attributes.test.rb @@ -29,6 +29,16 @@ assert_equal error.message, "Unsafe attribute name detected: onclick." end +test "data with hash" do + data = phlex { div(data: { attr: "test" }) } + assert_equal_html data, %(
) +end + +test "data with object which responds to to_h" do + data = phlex { div(data: Data.define(:attr).new(attr: "test")) } + assert_equal_html data, %(
) +end + test "href with hash" do error = assert_raises(Phlex::ArgumentError) do phlex { a(href: {}) } From a86663910b6dce5587e3879631861a5f88893c7e Mon Sep 17 00:00:00 2001 From: Stephen Ierodiaconou Date: Tue, 8 Jul 2025 15:09:31 +0000 Subject: [PATCH 2/2] Fix whitespace --- lib/phlex/sgml.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/phlex/sgml.rb b/lib/phlex/sgml.rb index 9963f2be..701c5a5c 100644 --- a/lib/phlex/sgml.rb +++ b/lib/phlex/sgml.rb @@ -508,7 +508,7 @@ def json_escape(string) end when Phlex::SGML::SafeObject v.to_s.gsub('"', """) - when -> (v) { v.respond_to?(:to_h) } + when -> (v) { v.respond_to?(:to_h) } __nested_attributes__(v.to_h, "#{name}-", buffer) else raise Phlex::ArgumentError.new("Invalid attribute value for #{k}: #{v.inspect}.")