Convert underscores to dashes in attributes

This commit is contained in:
Max Veytsman
2021-03-19 12:30:56 -04:00
parent 605b52e662
commit 88f0ab6d49
2 changed files with 18 additions and 2 deletions

View File

@ -48,7 +48,15 @@ defmodule Heroicons do
@doc unquote(doc)
@spec unquote(name)(keyword(binary)) :: binary
def unquote(name)(opts \\ []) do
attrs = for {k, v} <- opts, do: {:safe, [Phoenix.HTML.Safe.to_iodata(k), ?=, ?", Phoenix.HTML.Safe.to_iodata(v), ?"]}
attrs =
for {k, v} <- opts do
safe_k =
k |> Atom.to_string() |> String.replace("_", "-") |> Phoenix.HTML.Safe.to_iodata()
safe_v = v |> Phoenix.HTML.Safe.to_iodata()
{:safe, [safe_k, ?=, ?", safe_v, ?"]}
end
{:safe, [unquote(head), Phoenix.HTML.Safe.to_iodata(attrs), unquote(tail)]}
end
end

View File

@ -17,7 +17,15 @@ defmodule HeroiconsTest do
assert Heroicons.Outline.academic_cap(class: "<> \" ")
|> Phoenix.HTML.safe_to_string() =~
~s(class="&lt;&gt; &quot;)
~s(class="&lt;&gt; &quot; ")
assert Heroicons.Outline.academic_cap(foo: "bar")
|> Phoenix.HTML.safe_to_string() =~
~s(foo="bar")
assert Heroicons.Outline.academic_cap(multiword_key: "foo")
|> Phoenix.HTML.safe_to_string() =~
~s(multiword-key="foo")
end
test "generated docs" do