Skip to content

Commit 3847e8f

Browse files
authored
Add support for Date/Time attribute values (#903)
Closes #883
1 parent 036f0df commit 3847e8f

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/phlex/sgml.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,10 @@ def json_escape(string)
481481
v.name.tr("_", "-").gsub('"', """)
482482
when Integer, Float
483483
v.to_s
484+
when Date
485+
v.iso8601
486+
when Time
487+
v.respond_to?(:iso8601) ? v.iso8601 : v.strftime("%Y-%m-%dT%H:%M:%S%:z")
484488
when Hash
485489
case k
486490
when :style

quickdraw/sgml/attributes.test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,16 @@
176176
assert_equal_html output, %(<div attribute="1.234"></div>)
177177
end
178178

179+
test "_, Date" do
180+
output = phlex { div(attribute: Date.new(2023, 1, 15)) }
181+
assert_equal_html output, %(<div attribute="2023-01-15"></div>)
182+
end
183+
184+
test "_, Time" do
185+
output = phlex { div(attribute: Time.new(2023, 1, 15, 12, 30, 45)) }
186+
assert_equal_html output, %(<div attribute="2023-01-15T12:30:45+00:00"></div>)
187+
end
188+
179189
test "_, *invalid*" do
180190
assert_raises(Phlex::ArgumentError) do
181191
phlex { div(attribute: Object.new) }

0 commit comments

Comments
 (0)