Skip to content

Commit

Permalink
Fix attribute value safe bug
Browse files Browse the repository at this point in the history
Fixed bug for when attribute values are derived from
lists or maps
  • Loading branch information
bcardarella committed Feb 21, 2024
1 parent 8bffbc2 commit cc0fbcd
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
7 changes: 2 additions & 5 deletions lib/live_view_native/template.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ defmodule LiveViewNative.Template do
end)
end

def escape({:safe, _} = safe), do: safe
def escape(other), do: {:safe, LiveViewNative.Engine.encode_to_iodata!(other)}

def attributes_escape(attrs) when is_list(attrs) do
{:safe, build_attrs(attrs)}
end
Expand All @@ -22,7 +19,7 @@ defmodule LiveViewNative.Template do
end

defp build_attrs([{k, true} | t]),
do: [?\s, key_escape(k) | build_attrs(t)]
do: [?\s, key_escape(k) | build_attrs(t)]

defp build_attrs([{_, false} | t]),
do: build_attrs(t)
Expand Down Expand Up @@ -104,6 +101,6 @@ defmodule LiveViewNative.Template do
defp attr_escape(attr)
defp attr_escape({:safe, data}), do: data
defp attr_escape(nil), do: []
defp attr_escape(other) when is_binary(other), do: LiveViewNative.Template.escape(other)
defp attr_escape(other) when is_binary(other), do: Phoenix.HTML.Engine.html_escape(other)
defp attr_escape(other), do: LiveViewNative.Template.Safe.to_iodata(other)
end
2 changes: 1 addition & 1 deletion lib/live_view_native/template/safe.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ defimpl LiveViewNative.Template.Safe, for: Atom do
end

defimpl LiveViewNative.Template.Safe, for: BitString do
defdelegate to_iodata(data), to: LiveViewNative.Template, as: :escape
defdelegate to_iodata(data), to: Phoenix.HTML, as: :html_escape
end

defimpl LiveViewNative.Template.Safe, for: Time do
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ defmodule LiveViewNative.MixProject do
{:phoenix_live_view, github: "phoenixframework/phoenix_live_view", ref: "4939fb8", override: true},
{:phoenix_live_reload, "~> 1.4", only: :test},
{:phoenix_template, "~> 1.0.4"},
{:phoenix_html, "~> 3.3 or ~> 4.0 or ~> 4.1"},
{:floki, ">= 0.30.0", only: :test},
{:plug, "~> 1.15"},
{:jason, "~> 1.2"},
Expand Down
50 changes: 49 additions & 1 deletion test/live_view_native/template_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,64 @@ defmodule LiveViewNative.TemplateTest do
use ExUnit.Case, async: false
import LiveViewNative.Component, only: [sigil_LVN: 2]

describe "value embedding" do
test "can embed values with EEx statement" do
assigns = %{foo: "bar"}

assert ~LVN"""
<Foo><%= @foo %></Foo>
"""
|> render() =~ ~S(<Foo>bar</Foo>)
end

test "can embed values from maps" do
data = %{"foo" => "bar"}
assigns = %{data: data}

assert ~LVN"""
<Foo><%= @data["foo"] %></Foo>
"""
|> render() =~ ~S(<Foo>bar</Foo>)
end
end

describe "attributes" do
test "won't stringify attribute names" do
assigns = %{}

assert ~LVN"""
<Foo foo_bar = "123" />
<Foo foo_bar="123" />
"""
|> render() =~ ~S(<Foo foo_bar="123"></Foo>)
end

test "accepts string values" do
assigns = %{}

assert ~LVN"""
<Foo foo={"bar"} />
"""
|> render() =~ ~S(<Foo foo="bar"></Foo>)
end

test "accepts values from maps" do
assigns = %{}

assert ~LVN"""
<Foo foo={%{"foo" => "bar"}["foo"]} />
"""
|> render() =~ ~S(<Foo foo="bar"></Foo>)
end

test "accepts values from lists" do
assigns = %{}

assert ~LVN"""
<Foo foo={[foo: "bar"][:foo]} />
"""
|> render() =~ ~S(<Foo foo="bar"></Foo>)
end

test "accepts numbers for id" do
assigns = %{}

Expand Down

0 comments on commit cc0fbcd

Please sign in to comment.