Commit afd98368 authored by rinpatch's avatar rinpatch

Introduce a lookup table for known tags

parent 0aa210c3
Pipeline #19242 failed with stage
in 1 minute and 22 seconds
defmodule FastSanitize.Fragment do
import Plug.HTML, only: [html_escape_to_iodata: 1]
# Generate a lookup table from atoms to binaries
@known_tags [
for tag <- @known_tags do
string_tag = to_string(tag)
def tag_to_string(unquote(tag)), do: unquote(string_tag)
def tag_to_string("" <> binary), do: binary
def tag_to_string(atom), do: to_string(atom)
def to_tree(bin) do
with {:html, _, [{:head, _, _}, {:body, _, fragment}]} <-
Myhtmlex.decode(bin, format: [:nil_self_closing, :comment_tuple3, :html_atoms]) do
......@@ -19,13 +149,14 @@ defmodule FastSanitize.Fragment do
defp build_start_tag(tag, attrs, nil), do: ["<", to_string(tag), build_attr_chunks(attrs), "/>"]
defp build_start_tag(tag, attrs, nil),
do: ["<", tag_to_string(tag), build_attr_chunks(attrs), "/>"]
defp build_start_tag(tag, attrs, _children) when length(attrs) == 0,
do: ["<", to_string(tag), ">"]
do: ["<", tag_to_string(tag), ">"]
defp build_start_tag(tag, attrs, _children),
do: ["<", to_string(tag), build_attr_chunks(attrs), ">"]
do: ["<", tag_to_string(tag), build_attr_chunks(attrs), ">"]
# empty tuple - fragment was clobbered, return nothing
defp fragment_to_html(nil, _), do: ""
......@@ -49,7 +180,7 @@ defmodule FastSanitize.Fragment do
# 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), ">"],
end_tag <- ["</", tag_to_string(tag), ">"],
subtree <- subtree_to_iodata(subtree, scrubber) do
[start_tag, subtree, end_tag]
......@@ -10,7 +10,6 @@ defmodule Mix.Tasks.FastSanitize.Bench do
input =!(Path.join(@input_dir, input_name))
Map.put(acc, input_name, input)
|> IO.inspect()
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