Skip to content

Commit 76b5fcd

Browse files
committed
Join list-attributes
1 parent 6a713d4 commit 76b5fcd

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

lib/slime/compiler.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,11 @@ defmodule Slime.Compiler do
9595
~s[ #{name}="#{quoted}"]
9696
end
9797

98-
defp render_attribute_code(name, _cotnent, quoted, safe) when is_binary(quoted) do
99-
value = if :eex == safe, do: quoted, else: ~s[<%= {:safe, "#{quoted}"} %>]
100-
~s[ #{name}="#{value}"]
98+
defp render_attribute_code(name, _content, quoted, _) when is_list(quoted) do
99+
quoted |> Enum.map_join(" ", &Kernel.to_string/1) |> (& ~s[ #{name}="#{&1}"]).()
101100
end
101+
defp render_attribute_code(name, _content, quoted, :eex) when is_binary(quoted), do: ~s[ #{name}="#{quoted}"]
102+
defp render_attribute_code(name, _content, quoted, _) when is_binary(quoted), do: ~s[ #{name}="<%= {:safe, "#{quoted}"} %>"]
102103

103104
# NOTE: string with interpolation or strings concatination
104105
defp render_attribute_code(name, content, {op, _, _}, safe) when op in [:<<>>, :<>] do

lib/slime/parser/attributes_keyword.ex

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,17 @@ defmodule Slime.Parser.AttributesKeyword do
5151
defp dynamic_value?(_), do: false
5252

5353
defp join_attribute_values(values, join_by) do
54-
values |> Enum.map(&attribute_val/1) |> List.flatten |> Enum.join(join_by)
54+
values |> Enum.map(&attribute_val(&1, join_by)) |> List.flatten |> Enum.join(join_by)
5555
end
5656

57-
defp attribute_val({:eex, content}), do: "\#{" <> content <> "}"
58-
defp attribute_val(value), do: value
57+
defp attribute_val({:eex, content}, join_by) do
58+
case Code.string_to_quoted!(content) do
59+
list when is_list(list) ->
60+
list |> List.flatten |> Enum.join(join_by)
61+
_ -> "\#{" <> content <> "}"
62+
end
63+
end
64+
65+
defp attribute_val(value, _), do: value
66+
5967
end

test/rendering/attributes_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ defmodule RenderAttributesTest do
101101
) == ~s(<meta content="1,2,3">)
102102
end
103103

104+
test "parses attributes with lists" do
105+
assert render(~S{meta content=["a", "b", "c"]}) == ~s{<meta content="a b c">}
106+
end
107+
104108
test "attributes values can contain `=` character" do
105109
template = ~s(meta content="width=device-width, initial-scale=1")
106110
html = ~s(<meta content="width=device-width, initial-scale=1">)
@@ -112,6 +116,12 @@ defmodule RenderAttributesTest do
112116
assert render(template) == ~s(<div class="class-one class-two"></div>)
113117
end
114118

119+
test "shorthand and literal class attributes are merged with list awareness" do
120+
121+
template = ~s(.class-one class=["class-two", "class-three"])
122+
assert render(template) == ~s(<div class="class-one class-two class-three"></div>)
123+
end
124+
115125
test "attributes can have dynamic values" do
116126
assert render("div a=meta", meta: true) == ~s(<div a></div>)
117127
assert render("div a=meta", meta: "test") == ~s(<div a="test"></div>)

0 commit comments

Comments
 (0)