Commit 275d4fd2 authored by kaniini's avatar kaniini

make the underlying parser behave more like mochiweb

parent 7faa60e9
......@@ -2,7 +2,8 @@ defmodule FastSanitize.Fragment do
require Logger
def to_tree(bin) do
with {"html", _, [{"head", _, _}, {"body", _, fragment}]} <- Myhtmlex.decode(bin) do
with {:html, _, [{:head, _, _}, {:body, _, fragment}]} <-
Myhtmlex.decode(bin, format: [:html_atoms, :nil_self_closing, :comment_tuple3]) do
{:ok, fragment}
else
e -> {:error, e}
......@@ -28,14 +29,14 @@ defmodule FastSanitize.Fragment do
defp fragment_to_html(text) when is_binary(text), do: text
# comment node
defp fragment_to_html({:comment, text}), do: "<!-- #{text} -->"
defp fragment_to_html({:comment, _, text}), do: "<!-- #{text} -->"
# tags like <link> - useful attributes, no terminator
defp fragment_to_html({"link", attrs, _}), do: build_start_tag("link", attrs)
defp fragment_to_html({:link, attrs, _}), do: build_start_tag("link", attrs)
# tags like <hr> and <br> - no useful attributes, no terminator
defp fragment_to_html({"hr", _, _}), do: "<hr>"
defp fragment_to_html({"br", _, _}), do: "<br>"
defp fragment_to_html({:hr, _, _}), do: "<hr>"
defp fragment_to_html({:br, _, _}), do: "<br>"
defp fragment_to_html({tag, attrs, subtree}) do
with start_tag <- build_start_tag(tag, attrs),
......
......@@ -3,27 +3,35 @@ defmodule FastSanitize.Fragment.Test do
describe "to_tree/1" do
test "it works for simple fragments" do
{:ok, [{"h1", [], ["test"]}]} = FastSanitize.Fragment.to_tree("<h1>test</h1>")
{:ok, [{:h1, [], ["test"]}]} = FastSanitize.Fragment.to_tree("<h1>test</h1>")
end
end
describe "to_html/1" do
test "it works for simple fragment trees" do
tree = [{"h1", [], ["test"]}]
tree = [{:h1, [], ["test"]}]
{:ok, "<h1>test</h1>"} = FastSanitize.Fragment.to_html(tree)
end
test "it works for simple fragment trees with atypical tags" do
tree = [{"br", [], []}, {"hr", [], []}]
tree = [{:br, [], []}, {:hr, [], []}]
{:ok, "<br><hr>"} = FastSanitize.Fragment.to_html(tree)
end
test "it works for simple fragment trees with non-terminating tags" do
tree = [{"link", [{"rel", "stylesheet"}, {"type", "text/css"}, {"href", "http://example.com/example.css"}], []}]
tree = [
{:link,
[
{"rel", "stylesheet"},
{"type", "text/css"},
{"href", "http://example.com/example.css"}
], []}
]
{:ok, "<link rel=\"stylesheet\" type=\"text/css\" href=\"http://example.com/example.css\">"} = FastSanitize.Fragment.to_html(tree)
{:ok, "<link rel=\"stylesheet\" type=\"text/css\" href=\"http://example.com/example.css\">"} =
FastSanitize.Fragment.to_html(tree)
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment