Skip to content

Add Phoenix.HTML.Safe to Duration #463

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 3 commits into from
Apr 30, 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
6 changes: 6 additions & 0 deletions lib/phoenix_html/safe.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ defimpl Phoenix.HTML.Safe, for: DateTime do
end
end

if Code.ensure_loaded?(Duration) do
defimpl Phoenix.HTML.Safe, for: Duration do
defdelegate to_iodata(data), to: Duration, as: :to_iso8601
end
end

defimpl Phoenix.HTML.Safe, for: List do
def to_iodata(list), do: recur(list)

Expand Down
7 changes: 7 additions & 0 deletions test/phoenix_html/safe_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ defmodule Phoenix.HTML.SafeTest do
assert Safe.to_iodata(datetime) == "2000-01-01T12:13:14+00:30"
end

if Code.ensure_loaded?(Duration) do
test "impl for Duration" do
duration = Duration.new!(month: 1)
assert Safe.to_iodata(duration) == "P1M"
end
end

test "impl for URI" do
uri = %URI{scheme: "http", host: "www.example.org", path: "/foo", query: "secret=<a&b>"}

Expand Down