Skip to content

Commit

Permalink
Merge pull request #157 from paradox460/master
Browse files Browse the repository at this point in the history
Adds the ability to specify attributes via lists
  • Loading branch information
Rakoth authored Aug 22, 2019
2 parents 6a713d4 + 76b5fcd commit 83854d3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
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

0 comments on commit 83854d3

Please sign in to comment.