Skip to content

Commit 834e763

Browse files
committed
Allow SGML attributes to accept objects which respond to to_h
1 parent 4921cbe commit 834e763

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

lib/phlex/sgml.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,8 @@ def json_escape(string)
508508
end
509509
when Phlex::SGML::SafeObject
510510
v.to_s.gsub('"', """)
511+
when -> (v) { v.respond_to?(:to_h) }
512+
__nested_attributes__(v.to_h, "#{name}-", buffer)
511513
else
512514
raise Phlex::ArgumentError.new("Invalid attribute value for #{k}: #{v.inspect}.")
513515
end

quickdraw/sgml/attributes.test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@
2929
assert_equal error.message, "Unsafe attribute name detected: onclick."
3030
end
3131

32+
test "data with hash" do
33+
data = phlex { div(data: { attr: "test" }) }
34+
assert_equal_html data, %(<div data-attr="test"></div>)
35+
end
36+
37+
test "data with object which responds to to_h" do
38+
data = phlex { div(data: Data.define(:attr).new(attr: "test")) }
39+
assert_equal_html data, %(<div data-attr="test"></div>)
40+
end
41+
3242
test "href with hash" do
3343
error = assert_raises(Phlex::ArgumentError) do
3444
phlex { a(href: {}) }

0 commit comments

Comments
 (0)