 
     iex> inject_attribute_to_last_opened "<tag 1><tag 2><tag 3></tag></tag></tag>", "attr"
     {:not_found, "<tag 1><tag 2><tag 3></tag></tag></tag>"}
-  """
-  def inject_attribute_to_last_opened(html, attribute) when is_binary(html) do
-    tokenized = tokenize(html) |> Enum.reverse()
-    injected = do_inject(tokenized, attribute, []) |> List.flatten() |> Enum.reverse() |> tokenized_to_html()
-    ret = if injected == html, do: :not_found, else: :ok
-    {ret, injected}
-  end
-
-  defp do_inject([], _, _) do
-    []
-  end
-  defp do_inject([head | tail] = tokenized, attribute, opened) do
+
+    iex> inject_attribute_to_last_opened ["<tag 1><tag 2><tag 3></tag>", "</tag>"], "attr"
+    {:ok, ["<tag attr 1><tag 2><tag 3></tag>", "</tag>"]}
+
+    iex> inject_attribute_to_last_opened ["<tag 1><tag 2>", "<tag 3></tag>", "</tag>"], "attr"
+    {:ok, ["<tag attr 1><tag 2>", "<tag 3></tag>", "</tag>"]}
+
+    iex> inject_attribute_to_last_opened ["<tag 1><tag 2>", ["<tag 3>", "</tag>"]], "attr"
+    {:ok, ["<tag 1><tag attr 2>", ["<tag 3>", "</tag>"]]}
+  """
+  def inject_attribute_to_last_opened(html, attribute) do
+    {_, found, acc} = html
+      |> tokenize()
+      |> deep_reverse()
+      |> do_inject(attribute, [], false, [])
+    acc = tokenized_to_html(acc)
+    if found, do: {:ok, acc}, else: {:not_found, acc}
+  end
+
+  @non_closing_tags ~w{area base br col embed hr img input keygen link meta param source track wbr}
+  defp do_inject([], _, opened, found, acc) do
+    {opened, found, acc}
+  end
+  defp do_inject([head | tail], attribute, opened, found, acc) do
     case head do
       {:naked, tag} -> # naked can be only at the end
-        [{:naked, add_attribute(tag, attribute)} | tail]
+        do_inject(tail, attribute, opened, true, [{:naked, add_attribute(tag, attribute)} | acc])
       {:tag, "/" <> tag} ->
-        [head, do_inject(tail, attribute, [tag_name(tag) | opened])]
+        do_inject(tail, attribute, [tag_name(tag) | opened], found, [head | acc])
       {:tag, tag} ->
         if Enum.find(opened, fn x -> tag_name(tag) == x end) do
-          [head, do_inject(tail, attribute, opened -- [tag_name(tag)])]
+          do_inject(tail, attribute, opened -- [tag_name(tag)], found, [head | acc])
         else
-          [{:tag, add_attribute(tag, attribute)}] ++ tail
+          if found || Enum.member?(@non_closing_tags, tag_name(tag)) do
+            do_inject(tail, attribute, opened, found, [head | acc])
+          else
+            do_inject(tail, attribute, opened, true, [{:tag, add_attribute(tag, attribute)} | acc])
+          end
         end
-      other -> [head | do_inject(tail, attribute, opened)]
+      list when is_list(list) ->
+        {op, fd, ac} = do_inject(list, attribute, opened, found, [])
+        do_inject(tail, attribute, op, fd, [ac | acc])
+      _ ->
+        do_inject(tail, attribute, opened, found, [head | acc])
     end
   end
 

      iex> tokenize("some")
      ["some"]

      iex> tokenize("<tag> and more")
      [{:tag, "tag"}, " and more"]


  def add_attribute(tag, attribute) do
    String.replace(tag, tag_name(tag), "#{tag_name(tag)} #{attribute}", global: false)
  end

  @doc """
  Deep reverse of the list

      iex> deep_reverse [1,2,3]
      [3,2,1]

      iex> deep_reverse [[1,2],[3,4]]
      [[4,3], [2,1]]

      iex> deep_reverse [[[1,2], [3,4]], [5,6]]
      [[6,5], [[4,3], [2,1]]]

      iex> deep_reverse [1, [2, 3], 4]
      [4, [3, 2], 1]
  """
  def deep_reverse(list) do
    list |> Enum.reverse() |> Enum.map(fn
      x when is_list(x) -> deep_reverse(x)
      x -> x
    end)
  end
end
