Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 738c700

Browse files
author
José Valim
committedSep 5, 2017
Continue supporting iodata in link/2
1 parent be84c5f commit 738c700

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed
 

‎lib/phoenix_html/link.ex

+16-21
Original file line numberDiff line numberDiff line change
@@ -176,32 +176,27 @@ defmodule Phoenix.HTML.Link do
176176
{value, opts}
177177
end
178178

179-
defp valid_destination!(to, calling_func) do
180-
if invalid_destination?(to) do
181-
raise ArgumentError, """
182-
unsupported scheme given to #{calling_func}. In case you want to link to an
183-
unknown or unsafe scheme, such as javascript, use a tuple: {:javascript, rest}
184-
"""
185-
end
186-
187-
valid_destination!(to)
188-
end
189-
defp valid_destination!({:safe, to}) do
190-
{:safe, valid_destination!(to)}
179+
defp valid_destination!({:safe, to}, context) do
180+
{:safe, valid_string_destination!(IO.iodata_to_binary(to), context)}
191181
end
192-
defp valid_destination!({other, to}) when is_atom(other) do
182+
defp valid_destination!({other, to}, _context) when is_atom(other) do
193183
[Atom.to_string(other), ?:, to]
194184
end
195-
defp valid_destination!(to), do: to
185+
defp valid_destination!(to, context) do
186+
valid_string_destination!(IO.iodata_to_binary(to), context)
187+
end
196188

197189
for scheme <- @valid_uri_schemes do
198-
defp invalid_destination?(unquote(scheme) <> _), do: false
190+
defp valid_string_destination!(unquote(scheme) <> _ = string, _context), do: string
199191
end
200-
defp invalid_destination?({scheme, _}) when is_atom(scheme) do
201-
false
202-
end
203-
defp invalid_destination?(to) when is_binary(to) do
204-
String.contains?(to, ":")
192+
defp valid_string_destination!(to, context) do
193+
if String.contains?(to, ":") do
194+
raise ArgumentError, """
195+
unsupported scheme given to #{context}. In case you want to link to an
196+
unknown or unsafe scheme, such as javascript, use a tuple: {:javascript, rest}
197+
"""
198+
else
199+
to
200+
end
205201
end
206-
defp invalid_destination?(_), do: true
207202
end

‎test/phoenix_html/link_test.exs

+13-6
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ defmodule Phoenix.HTML.LinkTest do
3535
assert safe_to_string(link("foo", to: {:javascript, "alert(1)"})) ==
3636
~s[<a href="javascript:alert(1)">foo</a>]
3737

38+
assert safe_to_string(link("foo", to: {:javascript, 'alert(1)'})) ==
39+
~s[<a href="javascript:alert(1)">foo</a>]
40+
3841
assert safe_to_string(link("foo", to: {:javascript, {:safe, "alert(1)"}})) ==
3942
~s[<a href="javascript:alert(1)">foo</a>]
4043

41-
assert safe_to_string(link("foo", to: {:safe, {:javascript, "alert(1)"}})) ==
44+
assert safe_to_string(link("foo", to: {:javascript, {:safe, 'alert(1)'}})) ==
4245
~s[<a href="javascript:alert(1)">foo</a>]
4346
end
4447

@@ -58,13 +61,17 @@ defmodule Phoenix.HTML.LinkTest do
5861
link(to: "/hello-world")
5962
end
6063

61-
msg = """
62-
unsupported scheme given to link/2. In case you want to link to an
63-
unknown or unsafe scheme, such as javascript, use a tuple: {:javascript, rest}
64-
"""
65-
assert_raise ArgumentError, msg, fn ->
64+
assert_raise ArgumentError, ~r"unsupported scheme given to link/2", fn ->
6665
link("foo", to: "javascript:alert(1)")
6766
end
67+
68+
assert_raise ArgumentError, ~r"unsupported scheme given to link/2", fn ->
69+
link("foo", to: {:safe, "javascript:alert(1)"})
70+
end
71+
72+
assert_raise ArgumentError, ~r"unsupported scheme given to link/2", fn ->
73+
link("foo", to: {:safe, 'javascript:alert(1)'})
74+
end
6875
end
6976

7077
test "button with post (default)" do

0 commit comments

Comments
 (0)