Skip to content

Commit

Permalink
Fix removal of meaningful white-space
Browse files Browse the repository at this point in the history
Fixes #4
  • Loading branch information
rrrene committed May 17, 2016
1 parent ce34fc1 commit 69912a2
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
6 changes: 6 additions & 0 deletions lib/html_sanitize_ex/scrubber.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ defmodule HtmlSanitizeEx.Scrubber do

def scrub(html, scrubber_module) do
html
|> before_scrub
|> scrubber_module.before_scrub
|> HtmlSanitizeEx.Parser.parse
|> HtmlSanitizeEx.Traverser.traverse(scrubber_module)
|> HtmlSanitizeEx.Parser.to_html
end

defp before_scrub(html) do
html
|> String.replace(~r/(>)(\ +)(<)/, "\\1&#32;\\3")
end
end
2 changes: 2 additions & 0 deletions lib/html_sanitize_ex/scrubber/html5.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ defmodule HtmlSanitizeEx.Scrubber.HTML5 do
Meta.allow_tag_with_these_attributes "a", ["accesskey", "class", "contenteditable", "contextmenu", "dir", "draggable", "dropzone", "hidden", "id", "inert", "itemid", "itemprop", "itemref", "itemscope", "itemtype", "lang", "role", "spellcheck", "tabindex", "title", "translate",
"target", "ping", "rel", "media", "hreflang", "type"]

Meta.allow_tag_with_these_attributes "b", ["accesskey", "class", "contenteditable", "contextmenu", "dir", "draggable", "dropzone", "hidden", "id", "inert", "itemid", "itemprop", "itemref", "itemscope", "itemtype", "lang", "role", "spellcheck", "tabindex", "title", "translate"]

Meta.allow_tag_with_these_attributes "h1", ["accesskey", "class", "contenteditable", "contextmenu", "dir", "draggable", "dropzone", "hidden", "id", "inert", "itemid", "itemprop", "itemref", "itemscope", "itemtype", "lang", "role", "spellcheck", "tabindex", "title", "translate"]
Meta.allow_tag_with_these_attributes "h2", ["accesskey", "class", "contenteditable", "contextmenu", "dir", "draggable", "dropzone", "hidden", "id", "inert", "itemid", "itemprop", "itemref", "itemscope", "itemtype", "lang", "role", "spellcheck", "tabindex", "title", "translate"]
Meta.allow_tag_with_these_attributes "h3", ["accesskey", "class", "contenteditable", "contextmenu", "dir", "draggable", "dropzone", "hidden", "id", "inert", "itemid", "itemprop", "itemref", "itemscope", "itemtype", "lang", "role", "spellcheck", "tabindex", "title", "translate"]
Expand Down
6 changes: 3 additions & 3 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
%{"inch_ex": {:hex, :inch_ex, "0.4.0-dev"},
"mochiweb": {:hex, :mochiweb, "2.12.2"},
"poison": {:hex, :poison, "1.4.0"}}
%{"inch_ex": {:hex, :inch_ex, "0.5.1", "c1c18966c935944cbb2d273796b36e44fab3c54fd59f906ff026a686205b4e14", [:mix], [{:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: false]}]},
"mochiweb": {:hex, :mochiweb, "2.12.2", "80804ad342afa3d7f3524040d4eed66ce74b17a555de454ac85b07c479928e46", [:make, :rebar], []},
"poison": {:hex, :poison, "2.1.0", "f583218ced822675e484648fa26c933d621373f01c6c76bd00005d7bd4b82e27", [:mix], []}}
2 changes: 1 addition & 1 deletion test/basic_html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ defmodule HtmlSanitizeExScrubberBasicHTMLTest do
@tag href_scrubbing: true
test "test_strip_links_with_line_feed_and_uppercase_tag" do
input = "<a href='almost'>on my mind</a> <A href='almost'>all day long</A>"
assert "<a href=\"almost\">on my mind</a><a href=\"almost\">all day long</a>" == basic_html_sanitize(input)
assert "<a href=\"almost\">on my mind</a> <a href=\"almost\">all day long</a>" == basic_html_sanitize(input)
end

@tag href_scrubbing: true
Expand Down
5 changes: 5 additions & 0 deletions test/html5_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ defmodule HtmlSanitizeExScrubberHTML5Test do
HtmlSanitizeEx.html5(text)
end

test "strips nothing" do
input = "This <b>is</b> <b>an</b> <i>example</i> of <u>space</u> eating."
assert input == full_html_sanitize(input)
end

test "leaves the allowed tags alone" do
input = "<h1 class=\"heading\">hello world!</h1>"
expected = "<h1 class=\"heading\">hello world!</h1>"
Expand Down
13 changes: 13 additions & 0 deletions test/no_scrub_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule HtmlSanitizeExScrubberNoScrubTest do
use ExUnit.Case

defp no_scrub_sanitize(text) do
HtmlSanitizeEx.noscrub(text)
end

test "strips nothing" do
input = "This <b>is</b> <b>an</b> <i>example</i> of <u>space</u> eating."
expected = "This <b>is</b> <b>an</b> <i>example</i> of <u>space</u> eating."
assert expected == no_scrub_sanitize(input)
end
end

0 comments on commit 69912a2

Please sign in to comment.