Page MenuHomePhorge

No OneTemporary

Size
3 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/fast_sanitize/sanitizer.ex b/lib/fast_sanitize/sanitizer.ex
index 9492b38..0dff4f9 100644
--- a/lib/fast_sanitize/sanitizer.ex
+++ b/lib/fast_sanitize/sanitizer.ex
@@ -1,38 +1,63 @@
defmodule FastSanitize.Sanitizer do
+ require Logger
+
alias FastSanitize.Fragment
@moduledoc """
Defines the contract that Sanitizer modules must follow.
"""
@doc """
Scrubs a document node.
"""
@callback scrub({atom(), list(), list()}) :: tuple()
@doc """
Scrubs an unknown node.
"""
@callback scrub({binary(), list(), list()}) :: tuple()
@doc """
Scrubs a text node.
"""
@callback scrub(binary()) :: binary()
# fallbacks
- def scrub("", _), do: ""
- def scrub(nil, _), do: ""
-
- def scrub(doc, scrubber) do
- with {:ok, subtree} <- Fragment.to_tree(doc) do
- Enum.map(subtree, fn fragment ->
- scrubber.scrub(fragment)
- end)
+ def scrub("", _), do: {:ok, ""}
+ def scrub(nil, _), do: {:ok, ""}
+
+ def scrub(doc, scrubber) when is_binary(doc) do
+ with wrapped_doc <- "<body>" <> doc <> "</body>",
+ {:ok, subtree} <- Fragment.to_tree(wrapped_doc) do
+ scrub(subtree, scrubber)
|> Fragment.to_html()
else
e ->
{:error, e}
end
end
+
+ def scrub(subtree, scrubber) when is_list(subtree) do
+ Logger.debug("Pre-process: #{inspect(subtree)}")
+
+ Enum.map(subtree, fn fragment ->
+ case scrubber.scrub(fragment) do
+ {tag, attrs, nil} ->
+ Logger.debug("Post-process closure: #{inspect({tag, attrs, nil})}")
+ {tag, attrs, nil}
+
+ {tag, attrs, children} ->
+ Logger.debug("Post-process tag: #{inspect({tag, attrs, children})}")
+ {tag, attrs, scrub(children, scrubber)}
+
+ subtree when is_list(subtree) ->
+ Logger.debug("Post-process subtree: #{inspect(subtree)}")
+ scrub(subtree, scrubber)
+
+ other ->
+ Logger.debug("Post-process other: #{inspect(other)}")
+ other
+ end
+ end)
+ end
end
diff --git a/test/fragment_test.exs b/test/fragment_test.exs
index ed49eaf..330660d 100644
--- a/test/fragment_test.exs
+++ b/test/fragment_test.exs
@@ -1,37 +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>")
end
end
describe "to_html/1" do
test "it works for simple fragment trees" do
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, [], nil}, {:hr, [], nil}]
- {:ok, "<br><hr>"} = FastSanitize.Fragment.to_html(tree)
+ {: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"}
], nil}
]
- {:ok, "<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)
end
end
end

File Metadata

Mime Type
text/x-diff
Expires
Sat, Nov 30, 2:46 PM (1 d, 17 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41471
Default Alt Text
(3 KB)

Event Timeline