Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F85711648
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/fast_sanitize/fragment.ex b/lib/fast_sanitize/fragment.ex
index 6a7711d..4fa4352 100644
--- a/lib/fast_sanitize/fragment.ex
+++ b/lib/fast_sanitize/fragment.ex
@@ -1,60 +1,61 @@
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}
end
end
defp build_start_tag(tag, attrs) when length(attrs) == 0, do: "<#{tag}>"
defp build_start_tag(tag, attrs) do
attr_chunks =
Enum.map(attrs, fn {k, v} ->
"#{k}=\"#{v}\""
end)
|> Enum.join(" ")
"<#{tag} #{attr_chunks}>"
end
# empty tuple - fragment was clobbered, return nothing
defp fragment_to_html({}), do: ""
# text node
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),
end_tag <- "</#{tag}>",
{:ok, subtree} <- subtree_to_html(subtree) do
[start_tag, subtree, end_tag]
|> Enum.join("")
end
end
defp subtree_to_html([]), do: {:ok, ""}
defp subtree_to_html(tree) do
rendered =
Enum.map(tree, &fragment_to_html/1)
|> Enum.join("")
{:ok, rendered}
end
def to_html(tree), do: subtree_to_html(tree)
end
diff --git a/test/fragment_test.exs b/test/fragment_test.exs
index eab9fa0..d1bd72b 100644
--- a/test/fragment_test.exs
+++ b/test/fragment_test.exs
@@ -1,29 +1,37 @@
defmodule FastSanitize.Fragment.Test do
use ExUnit.Case
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
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Sep 19, 6:48 AM (1 d, 13 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1769044
Default Alt Text
(3 KB)
Attached To
Mode
R15 fast_sanitize
Attached
Detach File
Event Timeline
Log In to Comment