-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from doomspork/attribute_merging
Add attribute merging support
- Loading branch information
Showing
5 changed files
with
96 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule SlimFast.Parser.AttributesKeyword do | ||
@doc """ | ||
Merges multiply attributes values for keys specified in merge_rules. | ||
Attribute value may be given by string, list, or {:eex, args} node | ||
`merge_rules` should me an `%{attribute_name: joining_character}` map | ||
## Examples | ||
iex> SlimFast.Parser.AttributesKeyword.merge( | ||
...> [class: "a", class: ["b", "c"], class: "d"], | ||
...> %{class: " "} | ||
...> ) | ||
[class: "a b c d"] | ||
iex> SlimFast.Parser.AttributesKeyword.merge( | ||
...> [class: "a", class: ["b", "c"], class: {:eex, content: "d"}], | ||
...> %{class: " "} | ||
...> ) | ||
[class: {:eex, content: ~S("a b c \#{d}"), inline: true}] | ||
""" | ||
def merge(keyword_list, merge_rules) do | ||
Enum.reduce(merge_rules, keyword_list, fn ({attr, join}, result) -> | ||
case Keyword.get_values(result, attr) do | ||
[] -> result | ||
values -> Keyword.put(result, attr, merge_attribute_values(values, join)) | ||
end | ||
end) | ||
end | ||
|
||
defp merge_attribute_values(values, join_by) do | ||
result = join_attribute_values(values, join_by) | ||
if Enum.any?(values, &dynamic_value?/1) do | ||
{:eex, content: ~s("#{result}"), inline: true} | ||
else | ||
result | ||
end | ||
end | ||
|
||
defp dynamic_value?({:eex, _}), do: true | ||
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) | ||
end | ||
|
||
defp attribute_val({:eex, args}), do: "\#{" <> args[:content] <> "}" | ||
defp attribute_val(value), do: value | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
defmodule ParserAttributesKeywordTest do | ||
use ExUnit.Case | ||
doctest SlimFast.Parser.AttributesKeyword | ||
|
||
test "handles multiple eex nodes" do | ||
result = SlimFast.Parser.AttributesKeyword.merge( | ||
[class: "a", class: {:eex, content: "b"}, class: {:eex, content: "c"}], | ||
%{class: " "} | ||
) | ||
assert result == [class: {:eex, content: ~S("a #{b} #{c}"), inline: true}] | ||
end | ||
|
||
test "supports custom delimiter" do | ||
result = SlimFast.Parser.AttributesKeyword.merge( | ||
[class: "a", class: "b"], | ||
%{class: "--"} | ||
) | ||
assert result == [class: "a--b"] | ||
end | ||
|
||
test "leaves unspecified attributes as is" do | ||
result = SlimFast.Parser.AttributesKeyword.merge( | ||
[class: "a", id: "id", class: "b", id: "id1"], | ||
%{class: " "} | ||
) | ||
assert result == [class: "a b", id: "id", id: "id1"] | ||
end | ||
|
||
test "handles all attributes specified in merge rules" do | ||
result = SlimFast.Parser.AttributesKeyword.merge( | ||
[class: "a", id: "id", class: "b", id: "id1"], | ||
%{class: " ", id: "+"} | ||
) | ||
assert result == [id: "id+id1", class: "a b"] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters