Skip to content

Commit

Permalink
Check type of attribute values
Browse files Browse the repository at this point in the history
fixes #188
  • Loading branch information
katehedgpeth committed Sep 10, 2018
1 parent 0cbb684 commit c263c84
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/floki/raw_html.ex
Expand Up @@ -83,15 +83,18 @@ defmodule Floki.RawHTML do
defp close_end_tag(type, []) when type in @self_closing_tags, do: ""
defp close_end_tag(type, _), do: "</#{type}>"

defp build_attrs({attr, value}, attrs) do
defp build_attrs({attr, iodata}, attrs) when is_list(iodata) do
build_attrs({attr, IO.iodata_to_binary(iodata)}, attrs)
end
defp build_attrs({attr, <<value::binary>>}, attrs) do
if String.contains?(value, "\"") do
~s(#{attrs} #{attr}='#{value}')
else
~s(#{attrs} #{attr}="#{value}")
end
end

defp build_attrs(attr, attrs), do: "#{attrs} #{attr}"
defp build_attrs(<<attr::binary>>, attrs), do: "#{attrs} #{attr}"

defp tag_for(type, attrs, children, encoder) do
encoder =
Expand Down
5 changes: 5 additions & 0 deletions test/floki_test.exs
Expand Up @@ -273,6 +273,11 @@ defmodule FlokiTest do
assert raw_html == "<div hidden></div>"
end

test "raw_html with attribute as iodata" do
raw_html = Floki.raw_html({"div", [{"class", ["class1", " ", ["class", "2"]]}], []})
assert raw_html == ~s(<div class="class1 class2"></div>)
end

test "raw_html (with self closing tag without content)" do
raw_html = Floki.raw_html({"link", [{"href", "www.example.com"}], []})

Expand Down

0 comments on commit c263c84

Please sign in to comment.