Skip to content

Commit

Permalink
Update handling of spaces between nodes
Browse files Browse the repository at this point in the history
Fixes #14
  • Loading branch information
rrrene committed Apr 30, 2017
1 parent c4bbac6 commit fdfe0a9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
11 changes: 8 additions & 3 deletions lib/html_sanitize_ex/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ defmodule HtmlSanitizeEx.Parser do @doc """
@type html_tree :: tuple | list

@my_root_node "html_sanitize_ex"
@linebreak [239, 188, 191]
@replacement_linebreak [239, 188, 191]
@replacement_space [239, 189, 191]

@spec parse(binary) :: html_tree

Expand All @@ -22,7 +23,9 @@ defmodule HtmlSanitizeEx.Parser do @doc """
end

defp before_parse(html) do
String.replace(html, ~r/(>)(\r?\n)/, "\\1 #{@linebreak} \\2")
html
|> String.replace(~r/(>)(\r?\n)/, "\\1 #{@replacement_linebreak} \\2")
|> String.replace(~r/(>)(\ +)(<)/, "\\1 #{@replacement_space}\\2\\3")
end

def to_html(tokens) do
Expand All @@ -36,7 +39,9 @@ defmodule HtmlSanitizeEx.Parser do @doc """
end

defp after_to_html(html) do
String.replace(html, ~r/(\ ?#{@linebreak} )(\r?\n)/, "\\2")
html
|> String.replace(~r/(\ ?#{@replacement_linebreak} )(\r?\n)/, "\\2")
|> String.replace(~r/(\&gt\;|>)(\ +)(#{@replacement_space})(\ +)(\&lt\;|<)/, "\\1\\4\\5")
end

defp ensure_list(list) do
Expand Down
6 changes: 0 additions & 6 deletions lib/html_sanitize_ex/scrubber.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,9 @@ 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
6 changes: 6 additions & 0 deletions test/html5_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,10 @@ defmodule HtmlSanitizeExScrubberHTML5Test do
expected = ~s(<a href="mailto:[email protected]">Email Us</a>)
assert expected == full_html_sanitize(input)
end

test "does encode script in textarea, but preserves white-space" do
input = ~s(<textarea> <script></script></textarea>)
expected = ~s(<textarea> &lt;script&gt;&lt;/script&gt;</textarea>)
assert expected == full_html_sanitize(input)
end
end

0 comments on commit fdfe0a9

Please sign in to comment.