From 171c4dc2023eb78edbfc18087d9057dc287b6860 Mon Sep 17 00:00:00 2001 From: Kate Hedgpeth Date: Fri, 31 Aug 2018 13:08:22 -0400 Subject: [PATCH] Check type of attribute values fixes https://github.com/philss/floki/issues/188 --- lib/floki/raw_html.ex | 8 ++++++-- test/floki_test.exs | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/floki/raw_html.ex b/lib/floki/raw_html.ex index 84c7539c..b6c362eb 100644 --- a/lib/floki/raw_html.ex +++ b/lib/floki/raw_html.ex @@ -83,7 +83,11 @@ defmodule Floki.RawHTML do defp close_end_tag(type, []) when type in @self_closing_tags, do: "" defp close_end_tag(type, _), do: "" - 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, <>}, attrs) do if String.contains?(value, "\"") do ~s(#{attrs} #{attr}='#{value}') else @@ -91,7 +95,7 @@ defmodule Floki.RawHTML do end end - defp build_attrs(attr, attrs), do: "#{attrs} #{attr}" + defp build_attrs(<>, attrs), do: "#{attrs} #{attr}" defp tag_for(type, attrs, children, encoder) do encoder = diff --git a/test/floki_test.exs b/test/floki_test.exs index 2254ac85..ab6cf0cb 100644 --- a/test/floki_test.exs +++ b/test/floki_test.exs @@ -273,6 +273,11 @@ defmodule FlokiTest do assert raw_html == "" end + test "raw_html with attribute as iodata" do + raw_html = Floki.raw_html({"div", [{"class", ["class1", " ", ["class", "2"]]}], []}) + assert raw_html == ~s(
) + end + test "raw_html (with self closing tag without content)" do raw_html = Floki.raw_html({"link", [{"href", "www.example.com"}], []})