From 88f0ab6d497321f5c5764b3e7bb2d6649927018e Mon Sep 17 00:00:00 2001 From: Max Veytsman Date: Fri, 19 Mar 2021 12:30:56 -0400 Subject: [PATCH] Convert underscores to dashes in attributes --- lib/heroicons.ex | 10 +++++++++- test/heroicons_test.exs | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/heroicons.ex b/lib/heroicons.ex index a8dfa71..0ab05b5 100644 --- a/lib/heroicons.ex +++ b/lib/heroicons.ex @@ -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 diff --git a/test/heroicons_test.exs b/test/heroicons_test.exs index f6bbdf5..bd2ec6b 100644 --- a/test/heroicons_test.exs +++ b/test/heroicons_test.exs @@ -17,7 +17,15 @@ defmodule HeroiconsTest do assert Heroicons.Outline.academic_cap(class: "<> \" ") |> Phoenix.HTML.safe_to_string() =~ - ~s(class="<> ") + ~s(class="<> " ") + + 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