Commit 5ba47cf7 authored by rinpatch's avatar rinpatch

use fast_html's fragment parsing mode

test was removed because myhtml will treat a fragment without non-broken
top level tag or children as a text node
parent b8d0cb33
Pipeline #21009 passed with stage
in 1 minute and 4 seconds
...@@ -4,8 +4,10 @@ defmodule FastSanitize.Fragment do ...@@ -4,8 +4,10 @@ defmodule FastSanitize.Fragment do
import Plug.HTML, only: [html_escape_to_iodata: 1] import Plug.HTML, only: [html_escape_to_iodata: 1]
def to_tree(bin) do def to_tree(bin) do
with {:ok, [{:html, _, [{:head, _, _}, {:body, _, fragment}]}]} <- with {:ok, [{:html, _, fragment}]} <-
:fast_html.decode(bin, format: [:nil_self_closing, :comment_tuple3, :html_atoms]) do :fast_html.decode_fragment(bin,
format: [:nil_self_closing, :comment_tuple3, :html_atoms]
) do
{:ok, fragment} {:ok, fragment}
else else
e -> e ->
......
...@@ -27,10 +27,10 @@ defmodule FastSanitize.Sanitizer do ...@@ -27,10 +27,10 @@ defmodule FastSanitize.Sanitizer do
def scrub(nil, _), do: {:ok, ""} def scrub(nil, _), do: {:ok, ""}
def scrub(doc, scrubber) when is_binary(doc) do def scrub(doc, scrubber) when is_binary(doc) do
with wrapped_doc <- "<body>" <> doc <> "</body>", case Fragment.to_tree(doc) do
{:ok, subtree} <- Fragment.to_tree(wrapped_doc) do {:ok, subtree} ->
Fragment.to_html(subtree, scrubber) Fragment.to_html(subtree, scrubber)
else
e -> e ->
{:error, e} {:error, e}
end end
......
...@@ -4,7 +4,7 @@ defmodule FastSanitize.MixProject do ...@@ -4,7 +4,7 @@ defmodule FastSanitize.MixProject do
def project do def project do
[ [
app: :fast_sanitize, app: :fast_sanitize,
version: "0.1.5", version: "0.1.6",
elixir: "~> 1.7", elixir: "~> 1.7",
start_permanent: Mix.env() == :prod, start_permanent: Mix.env() == :prod,
deps: deps(), deps: deps(),
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
"earmark": {:hex, :earmark, "1.4.2", "3aa0bd23bc4c61cf2f1e5d752d1bb470560a6f8539974f767a38923bb20e1d7f", [:mix], [], "hexpm"}, "earmark": {:hex, :earmark, "1.4.2", "3aa0bd23bc4c61cf2f1e5d752d1bb470560a6f8539974f767a38923bb20e1d7f", [:mix], [], "hexpm"},
"erlex": {:hex, :erlex, "0.2.5", "e51132f2f472e13d606d808f0574508eeea2030d487fc002b46ad97e738b0510", [:mix], [], "hexpm"}, "erlex": {:hex, :erlex, "0.2.5", "e51132f2f472e13d606d808f0574508eeea2030d487fc002b46ad97e738b0510", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.21.2", "caca5bc28ed7b3bdc0b662f8afe2bee1eedb5c3cf7b322feeeb7c6ebbde089d6", [:mix], [{:earmark, "~> 1.3.3 or ~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"}, "ex_doc": {:hex, :ex_doc, "0.21.2", "caca5bc28ed7b3bdc0b662f8afe2bee1eedb5c3cf7b322feeeb7c6ebbde089d6", [:mix], [{:earmark, "~> 1.3.3 or ~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
"fast_html": {:hex, :fast_html, "1.0.0", "eb73ab6dc4d1498b3760489629487fc6b82e0df71c316171a425a71deff2fdcf", [:make, :mix], [], "hexpm"}, "fast_html": {:hex, :fast_html, "1.0.1", "5bc7df4dc4607ec2c314c16414e4111d79a209956c4f5df96602d194c61197f9", [:make, :mix], [], "hexpm"},
"html_sanitize_ex": {:hex, :html_sanitize_ex, "1.3.0", "f005ad692b717691203f940c686208aa3d8ffd9dd4bb3699240096a51fa9564e", [:mix], [{:mochiweb, "~> 2.15", [hex: :mochiweb, repo: "hexpm", optional: false]}], "hexpm"}, "html_sanitize_ex": {:hex, :html_sanitize_ex, "1.3.0", "f005ad692b717691203f940c686208aa3d8ffd9dd4bb3699240096a51fa9564e", [:mix], [{:mochiweb, "~> 2.15", [hex: :mochiweb, repo: "hexpm", optional: false]}], "hexpm"},
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"}, "jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"makeup": {:hex, :makeup, "1.0.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"}, "makeup": {:hex, :makeup, "1.0.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
......
...@@ -102,10 +102,6 @@ defmodule FastSanitize.Sanitizer.BasicHTMLTest do ...@@ -102,10 +102,6 @@ defmodule FastSanitize.Sanitizer.BasicHTMLTest do
assert expected == basic_html_sanitize(input) assert expected == basic_html_sanitize(input)
end end
test "test_strip_links_with_unclosed_tags" do
assert "" == basic_html_sanitize("<a<a")
end
test "test_strip_links_with_plaintext" do test "test_strip_links_with_plaintext" do
assert "Dont touch me" == basic_html_sanitize("Dont touch me") assert "Dont touch me" == basic_html_sanitize("Dont touch me")
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