From f6c3ecd4d9e356715e5b2c20f3090dabb8a667be Mon Sep 17 00:00:00 2001 From: Brian Cardarella Date: Tue, 7 May 2024 10:00:13 -0400 Subject: [PATCH] Fix attribute encoding Ensure deep map encodings --- lib/live_view_native/template/safe.ex | 14 +------------- test/live_view_native/template_test.exs | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/lib/live_view_native/template/safe.ex b/lib/live_view_native/template/safe.ex index b2f24e0..1258c5e 100644 --- a/lib/live_view_native/template/safe.ex +++ b/lib/live_view_native/template/safe.ex @@ -13,19 +13,7 @@ defimpl LiveViewNative.Template.Safe, for: [List, Map] do def to_iodata(data) do data |> Jason.encode!() - |> escape() - end - - defp escape(<>) do - [~S(\") | escape(rest)] - end - - defp escape(<>) do - [char | escape(rest)] - end - - defp escape(<<>>) do - [] + |> Phoenix.HTML.Engine.html_escape() end end diff --git a/test/live_view_native/template_test.exs b/test/live_view_native/template_test.exs index 9959e00..cce9b02 100644 --- a/test/live_view_native/template_test.exs +++ b/test/live_view_native/template_test.exs @@ -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""" - - """ - |> render() =~ ~S() + doc = + ~LVN""" + + """ + |> render() + |> Floki.parse_document!() + + [json_data] = Floki.attribute(doc, "data") + assert assigns.data == Jason.decode!(json_data) end test "json encode list" do