Skip to content

Commit

Permalink
Merge pull request #179 from liveview-native/bc-style-attr
Browse files Browse the repository at this point in the history
Enable style attribute handling
  • Loading branch information
bcardarella authored May 4, 2024
2 parents a84a8d6 + 2c4a38a commit b8f52de
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
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

0 comments on commit b8f52de

Please sign in to comment.