Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F115010
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 79a7fed..7b314c1 100644
--- a/lib/fast_sanitize/fragment.ex
+++ b/lib/fast_sanitize/fragment.ex
@@ -1,78 +1,80 @@
defmodule FastSanitize.Fragment do
@moduledoc "Processing of HTML fragment trees."
import Plug.HTML, only: [html_escape_to_iodata: 1]
def to_tree(bin) do
with {:ok, [{:html, _, fragment}]} <-
:fast_html.decode_fragment(bin,
format: [:nil_self_closing, :comment_tuple3, :html_atoms]
) do
{:ok, fragment}
else
e ->
{:error, e}
end
end
defp build_attr_chunks([]), do: ""
defp build_attr_chunks(attrs) do
List.foldr(attrs, [], fn {k, v}, iodata ->
[[" ", html_escape_to_iodata(k), "=\"", html_escape_to_iodata(v), "\""] | iodata]
end)
end
- defp build_start_tag(tag, attrs, nil), do: ["<", to_string(tag), build_attr_chunks(attrs), "/>"]
+ defp build_self_closing_tag(tag, attrs), do: ["<", to_string(tag), build_attr_chunks(attrs), "/>"]
- defp build_start_tag(tag, attrs, _children) when attrs == [],
+ defp build_start_tag(tag, []),
do: ["<", to_string(tag), ">"]
- defp build_start_tag(tag, attrs, _children),
+ defp build_start_tag(tag, attrs),
do: ["<", to_string(tag), build_attr_chunks(attrs), ">"]
+ # text node
+ defp fragment_to_html("" <> _ = text, _), do: html_escape_to_iodata(text)
+
# empty tuple - fragment was clobbered, return nothing
defp fragment_to_html(nil, _), do: ""
defp fragment_to_html({}, _), do: ""
- # text node
- defp fragment_to_html(text, _) when is_binary(text), do: html_escape_to_iodata(text)
-
# comment node
- defp fragment_to_html({:comment, _, text}, _), do: ["<!-- ", text, " -->"]
-
- # bare subtree
- defp fragment_to_html(subtree, scrubber) when is_list(subtree) do
- subtree_to_iodata(subtree, scrubber)
- end
+ defp fragment_to_html({:comment, _, text}, _), do: ["<!--", text, "-->"]
# a node which can never accept children will have nil instead of a subtree
- defp fragment_to_html({tag, attrs, nil}, _), do: build_start_tag(tag, attrs, nil)
+ defp fragment_to_html({tag, attrs, nil}, _), do: build_self_closing_tag(tag, attrs)
# every other case, assume a subtree
defp fragment_to_html({tag, attrs, subtree}, scrubber) do
- with start_tag <- build_start_tag(tag, attrs, subtree),
- end_tag <- ["</", to_string(tag), ">"],
- subtree <- subtree_to_iodata(subtree, scrubber) do
- [start_tag, subtree, end_tag]
- end
+ start_tag = build_start_tag(tag, attrs)
+ subtree = subtree_to_iodata(subtree, scrubber)
+ [start_tag, subtree, "</", to_string(tag), ">"]
+ end
+
+ # bare subtree
+ defp fragment_to_html([], _), do: ""
+
+ defp fragment_to_html([_head | _tail] = subtree, scrubber) do
+ subtree_to_iodata(subtree, scrubber)
end
+
defp subtree_to_html([], _), do: {:ok, ""}
+
defp subtree_to_html(tree, scrubber) do
iodata = subtree_to_iodata(tree, scrubber)
rendered = :erlang.iolist_to_binary(iodata)
{:ok, rendered}
end
defp subtree_to_iodata(tree, scrubber) do
List.foldr(tree, [], fn node, iodata ->
[fragment_to_html(scrubber.scrub(node), scrubber) | iodata]
end)
end
def to_html(tree, scrubber \\ FastSanitize.Sanitizer.Dummy),
do: subtree_to_html(tree, scrubber)
end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Wed, Nov 27, 12:02 AM (1 d, 11 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
40516
Default Alt Text
(3 KB)
Attached To
Mode
R15 fast_sanitize
Attached
Detach File
Event Timeline
Log In to Comment