Skip to content

Add support for Date/Time attribute values #903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@ def json_escape(string)
v.name.tr("_", "-").gsub('"', """)
when Integer, Float
v.to_s
when Date
v.iso8601
when Time
v.respond_to?(:iso8601) ? v.iso8601 : v.strftime("%Y-%m-%dT%H:%M:%S%:z")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be :%z?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davetron5000 I thought the same thing, but it's actually this special timezone flag: https://ruby-doc.org/3.4.1/strftime_formatting_rdoc.html#label-Timezone+Flags

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, TIL

when Hash
case k
when :style
Expand Down
10 changes: 10 additions & 0 deletions quickdraw/sgml/attributes.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@
assert_equal_html output, %(<div attribute="1.234"></div>)
end

test "_, Date" do
output = phlex { div(attribute: Date.new(2023, 1, 15)) }
assert_equal_html output, %(<div attribute="2023-01-15"></div>)
end

test "_, Time" do
output = phlex { div(attribute: Time.new(2023, 1, 15, 12, 30, 45)) }
assert_equal_html output, %(<div attribute="2023-01-15T12:30:45+00:00"></div>)
end

test "_, *invalid*" do
assert_raises(Phlex::ArgumentError) do
phlex { div(attribute: Object.new) }
Expand Down