defmodule Exquery do require Exquery.Helpers import Exquery.Helpers alias Exquery.Tree @spaces [" ", "\n", "\t", "\r"] #yes this doesn't handle all cases..like StYlE #need a better way to deal with cases @styles [" acc _ -> [current | acc] end end ff_until "'", {:attrs, :open_sq}, do: to_attributes(r, push_attrs(current, acc)) ff_until "\"", {:attrs, :open_dq}, do: to_attributes(r, push_attrs(current, acc)) ff_until_in @spaces, {:attrs, :open_sp}, do: to_attributes(r, push_attrs(current, acc)) ff_until "=", {:attrs, :key}, do: ff(r, {{:attrs, :open_sp}, current, nil}, acc) ff_until "=", {:attrs, :close_key}, do: ff(r, {{:attrs, :open_sp}, current, nil}, acc) ff_until_in @spaces, {:attrs, :key}, do: ff(r, {{:attrs, :close_key}, current, nil}, acc) ff_until_in @spaces, {:attrs, :close_key}, do: ff(r, {{:attrs, :close_key}, current, nil}, acc) ff_until_in @spaces, {:attrs, :open_key}, do: ff(r, {{:attrs, :open_key}, current, nil}, acc) ff_until :any, {:attrs, :open_key}, do: ff(all, {{:attrs, :key}, current, nil}, acc) ff_until :any, {:attrs, :close_key}, do: to_attributes(all, push_attrs(current, acc)) ff_until "'" , {:attrs, :open_sp}, do: ff(r, {{:attrs, :open_sq}, current, nil}, acc) ff_until "\"", {:attrs, :open_sp}, do: ff(r, {{:attrs, :open_dq}, current, nil}, acc) ff_until "/>", {:attrs, :any}, do: {">" <> r, push_attrs(current, acc)} ff_until ">", {:attrs, :any}, do: {all, push_attrs(current, acc)} defp ff("", {{:attrs, _}, current, _}, acc), do: {"", push_attrs(current, acc)} ff_until :any, {:attrs, :key} do {key, val} = current ff(r, {{:attrs, :key}, {key <> head, val}, nil}, acc) end ff_until :any, {:attrs, :any} do {key, val} = current ff(r, {mode, {key, val <> head}, nil}, acc) end def to_attributes(r, acc), do: ff(r, {{:attrs, :open_key}, {"", ""}, nil}, acc) defp attributes(r, {tag, text, attrs} = token, acc) do {r, attrs} = ff(r, {{:attrs, :open_key}, {"", ""}, nil}, attrs) ff(r, {{:doc, tag}, text, attrs}, acc) end ## Document parsing defp push_tag({:none, "", []}, acc), do: acc defp push_tag({:text, value, attrs} = tag, acc) do case String.strip(value) do "" -> acc _ -> [tag | acc] end end defp push_tag({_, _, _} = tag, acc), do: [tag | acc] ff_until "-->", {:doc, :comment}, do: ff(r, @new_token, push_tag({:comment, current, attributes}, acc)) #Reached the "