Skip to content

Adds the ability to specify attributes via lists #157

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

Merged
merged 1 commit into from
Aug 22, 2019
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
7 changes: 4 additions & 3 deletions lib/slime/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ defmodule Slime.Compiler do
~s[ #{name}="#{quoted}"]
end

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

# NOTE: string with interpolation or strings concatination
defp render_attribute_code(name, content, {op, _, _}, safe) when op in [:<<>>, :<>] do
Expand Down
14 changes: 11 additions & 3 deletions lib/slime/parser/attributes_keyword.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,17 @@ defmodule Slime.Parser.AttributesKeyword do
defp dynamic_value?(_), do: false

defp join_attribute_values(values, join_by) do
values |> Enum.map(&attribute_val/1) |> List.flatten |> Enum.join(join_by)
values |> Enum.map(&attribute_val(&1, join_by)) |> List.flatten |> Enum.join(join_by)
end

defp attribute_val({:eex, content}), do: "\#{" <> content <> "}"
defp attribute_val(value), do: value
defp attribute_val({:eex, content}, join_by) do
case Code.string_to_quoted!(content) do
list when is_list(list) ->
list |> List.flatten |> Enum.join(join_by)
_ -> "\#{" <> content <> "}"
end
end

defp attribute_val(value, _), do: value

end
10 changes: 10 additions & 0 deletions test/rendering/attributes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ defmodule RenderAttributesTest do
) == ~s(<meta content="1,2,3">)
end

test "parses attributes with lists" do
assert render(~S{meta content=["a", "b", "c"]}) == ~s{<meta content="a b c">}
end

test "attributes values can contain `=` character" do
template = ~s(meta content="width=device-width, initial-scale=1")
html = ~s(<meta content="width=device-width, initial-scale=1">)
Expand All @@ -112,6 +116,12 @@ defmodule RenderAttributesTest do
assert render(template) == ~s(<div class="class-one class-two"></div>)
end

test "shorthand and literal class attributes are merged with list awareness" do

template = ~s(.class-one class=["class-two", "class-three"])
assert render(template) == ~s(<div class="class-one class-two class-three"></div>)
end

test "attributes can have dynamic values" do
assert render("div a=meta", meta: true) == ~s(<div a></div>)
assert render("div a=meta", meta: "test") == ~s(<div a="test"></div>)
Expand Down