From 2c4a38a4296936f515682c405be73a83c1e08025 Mon Sep 17 00:00:00 2001 From: Brian Cardarella Date: Sat, 4 May 2024 14:05:31 -0400 Subject: [PATCH] Enable style attribute handling Ensures that lists of styles are properly joined and encoded --- lib/live_view_native/tag_engine.ex | 2 ++ lib/live_view_native/template.ex | 28 +++++++++++++++++++++++-- test/live_view_native/template_test.exs | 21 +++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/lib/live_view_native/tag_engine.ex b/lib/live_view_native/tag_engine.ex index 2d1a6ac..aafa0d8 100644 --- a/lib/live_view_native/tag_engine.ex +++ b/lib/live_view_native/tag_engine.ex @@ -5,6 +5,7 @@ defmodule LiveViewNative.TagEngine do @behaviour Phoenix.LiveView.TagEngine + @doc false @impl true def handle_attributes(ast, meta) do if is_list(ast) and literal_keys?(ast) do @@ -179,6 +180,7 @@ defmodule LiveViewNative.TagEngine do # specially handled, so we keep them as strings shape. defp safe_unless_special("aria"), do: :aria defp safe_unless_special("class"), do: :class + defp safe_unless_special("style"), do: :style defp safe_unless_special(name), do: {:safe, name} @doc false diff --git a/lib/live_view_native/template.ex b/lib/live_view_native/template.ex index a79776f..8ad05ee 100644 --- a/lib/live_view_native/template.ex +++ b/lib/live_view_native/template.ex @@ -24,6 +24,9 @@ defmodule LiveViewNative.Template do defp build_attrs([{:class, v} | t]), do: [" class=\"", class_value(v), ?" | build_attrs(t)] + defp build_attrs([{:style, v} | t]), + do: [" style=\"", style_value(v), ?" | build_attrs(t)] + defp build_attrs([{:aria, v} | t]) when is_list(v), do: nested_attrs(v, " aria", t) @@ -36,6 +39,9 @@ defmodule LiveViewNative.Template do defp build_attrs([{"class", v} | t]), do: [" class=\"", class_value(v), ?" | build_attrs(t)] + defp build_attrs([{"style", v} | t]), + do: [" style=\"", style_value(v), ?" | build_attrs(t)] + defp build_attrs([{"aria", v} | t]) when is_list(v), do: nested_attrs(v, " aria", t) @@ -76,14 +82,32 @@ defmodule LiveViewNative.Template do end defp list_class_value(value) do + list_value(value, " ") + end + + def style_value(value) when is_list(value) do + value + |> list_style_value() + |> attr_escape() + end + + def style_value(value) do + attr_escape(value) + end + + defp list_style_value(value) do + list_value(value, ";") + end + + def list_value(value, joiner) do value |> Enum.flat_map(fn nil -> [] false -> [] - inner when is_list(inner) -> [list_class_value(inner)] + inner when is_list(inner) -> [list_value(inner, joiner)] other -> [other] end) - |> Enum.join(" ") + |> Enum.join(joiner) end defp key_escape(value) when is_atom(value), do: String.replace(Atom.to_string(value), "_", "-") diff --git a/test/live_view_native/template_test.exs b/test/live_view_native/template_test.exs index 5c1b5d8..9959e00 100644 --- a/test/live_view_native/template_test.exs +++ b/test/live_view_native/template_test.exs @@ -95,6 +95,27 @@ defmodule LiveViewNative.TemplateTest do """ |> render() =~ ~S() end + + test "style will encode quotes" do + assigns = %{} + + assert ~LVN""" + + """ + |> render() =~ ~S'' + end + + test "style as a list" do + assigns = %{} + + assert ~LVN""" + + """ + |> render() =~ ~S'' + end end describe "tag name" do