Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable style attribute handling #179

Merged
merged 1 commit into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/live_view_native/tag_engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
28 changes: 26 additions & 2 deletions lib/live_view_native/template.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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), "_", "-")
Expand Down
21 changes: 21 additions & 0 deletions test/live_view_native/template_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,27 @@ defmodule LiveViewNative.TemplateTest do
"""
|> render() =~ ~S(<Foo data="[1,2,3]"></Foo>)
end

test "style will encode quotes" do
assigns = %{}

assert ~LVN"""
<Foo style={"foo(bar);foo(\"bar\")"}></Foo>
"""
|> render() =~ ~S'<Foo style="foo(bar);foo(&quot;bar&quot;)"></Foo>'
end

test "style as a list" do
assigns = %{}

assert ~LVN"""
<Foo style={[
"foo(bar)",
~S'foo("bar")'
]}></Foo>
"""
|> render() =~ ~S'<Foo style="foo(bar);foo(&quot;bar&quot;)"></Foo>'
end
end

describe "tag name" do
Expand Down
Loading