Skip to content

Commit

Permalink
Merge pull request #180 from liveview-native/bc-fix-attr-encoding
Browse files Browse the repository at this point in the history
Fix attribute encoding
  • Loading branch information
bcardarella authored May 9, 2024
2 parents b8f52de + f6c3ecd commit a310ad6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
14 changes: 1 addition & 13 deletions lib/live_view_native/template/safe.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,7 @@ defimpl LiveViewNative.Template.Safe, for: [List, Map] do
def to_iodata(data) do
data
|> Jason.encode!()
|> escape()
end

defp escape(<<?"::utf8, rest::bits>>) do
[~S(\") | escape(rest)]
end

defp escape(<<char::utf8, rest::bits>>) do
[char | escape(rest)]
end

defp escape(<<>>) do
[]
|> Phoenix.HTML.Engine.html_escape()
end
end

Expand Down
17 changes: 12 additions & 5 deletions test/live_view_native/template_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,19 @@ defmodule LiveViewNative.TemplateTest do
end

test "json encode maps" do
assigns = %{}
assigns = %{
data: %{"a" => %{"b" => 1}, "c" => [1, 2]}
}

assert ~LVN"""
<Foo data={%{a: 1}} b="asf" c={123} />
"""
|> render() =~ ~S(<Foo data="{\"a\":1}" b="asf" c="123"></Foo>)
doc =
~LVN"""
<Foo data={@data} d="asf" e={123} />
"""
|> render()
|> Floki.parse_document!()

[json_data] = Floki.attribute(doc, "data")
assert assigns.data == Jason.decode!(json_data)
end

test "json encode list" do
Expand Down

0 comments on commit a310ad6

Please sign in to comment.