Repository: phoenixframework/phoenix_html (elixir)

Issue:
`link(name, to: %URI{})` as a valid option
I'll submit a PR if this seems like a good idea, but it would be nice for the `to:` option to accept a `%URI{}` struct as a valid option. My thought is that it would be as simple as doing a `to_string` on the URI.

We could just pass through the string to the validate step, or it could have a slightly optimized version of the validation https://github.com/phoenixframework/phoenix_html/blob/master/lib/phoenix_html/link.ex#L254

Pull request description:
Allow %URI{} to be passed to link/2 and button/2 as :to
Closes #308 

Patch:
diff --git a/lib/phoenix_html/link.ex b/lib/phoenix_html/link.ex
index f18edb7..b00f5d4 100644
--- a/lib/phoenix_html/link.ex
+++ b/lib/phoenix_html/link.ex
@@ -239,6 +239,10 @@ defmodule Phoenix.HTML.Link do
     {value, opts}
   end
 
+  defp valid_destination!(%URI{} = uri, context) do
+    valid_destination!(URI.to_string(uri), context)
+  end
+
   defp valid_destination!({:safe, to}, context) do
     {:safe, valid_string_destination!(IO.iodata_to_binary(to), context)}
   end


Test patch:
diff --git a/test/phoenix_html/link_test.exs b/test/phoenix_html/link_test.exs
index ad17afd..d8ac210 100644
--- a/test/phoenix_html/link_test.exs
+++ b/test/phoenix_html/link_test.exs
@@ -11,6 +11,18 @@ defmodule Phoenix.HTML.LinkTest do
              ~s[<a data-csrf="#{csrf_token}" data-method="post" data-to="/world" href="/world" rel="nofollow">hello</a>]
   end
 
+  test "link with %URI{}" do
+    url = "https://elixir-lang.org/"
+
+    assert safe_to_string(link("elixir", to: url)) ==
+             safe_to_string(link("elixir", to: URI.parse(url)))
+
+    path = "/elixir"
+
+    assert safe_to_string(link("elixir", to: path)) ==
+             safe_to_string(link("elixir", to: URI.parse(path)))
+  end
+
   test "link with put/delete" do
     csrf_token = Plug.CSRFProtection.get_csrf_token()
 
@@ -93,6 +105,13 @@ defmodule Phoenix.HTML.LinkTest do
              ~s[<button data-csrf="#{csrf_token}" data-method="post" data-to="/world">hello</button>]
   end
 
+  test "button with %URI{}" do
+    url = "https://elixir-lang.org/"
+
+    assert safe_to_string(button("elixir", to: url, csrf_token: false)) ==
+             safe_to_string(button("elixir", to: URI.parse(url), csrf_token: false))
+  end
+
   test "button with post without csrf_token" do
     assert safe_to_string(button("hello", to: "/world", csrf_token: false)) ==
              ~s[<button data-method="post" data-to="/world">hello</button>]