@@ -4,40 +4,60 @@ defmodule EpochtalkServerWeb.Helpers.Sanitize do
44 """
55
66 @ doc """
7- Used to sanitize html from `Thread` or `Post` title
7+ Used to sanitize html and entities from `Thread` or `Post` title
88
99 ## Example
1010 iex> alias EpochtalkServerWeb.Helpers.Sanitize
11- iex> attrs = %{"title" => "<strong>Hello World</strong><br /><script></script><a href='google.com'></a>"}
12- iex> Sanitize.html_from_title(attrs["title"], attrs)
13- %{"title" => "Hello World"}
11+ iex> attrs = %{"title" => " <strong>Hello World</strong><br /><script></script><a href='google.com'></a>"}
12+ iex> Sanitize.html_and_entities_from_title( attrs)
13+ %{"title" => "&nbsp;<strong> Hello World</strong><br /><script></script><a href='google.com'></a> "}
1414 """
15- @ spec html_from_title ( title :: String . t ( ) , attrs :: map ) :: map ( )
16- def html_from_title ( title , attrs ) when is_binary ( title ) and is_map ( attrs ) ,
17- do: Map . put ( attrs , "title" , HtmlSanitizeEx . strip_tags ( title ) )
15+ @ spec html_and_entities_from_title ( attrs :: map ) :: map ( )
16+ def html_and_entities_from_title ( attrs ) when is_map ( attrs ) do
17+ sanitized_title =
18+ attrs [ "title" ]
19+ |> String . replace ( ~r/ (?:&)/ , "&" )
20+ |> String . replace ( ~r/ (?:<)/ , "<" )
21+ |> String . replace ( ~r/ (?:>)/ , ">" )
22+
23+ Map . put ( attrs , "title" , sanitized_title )
24+ end
1825
1926 @ doc """
20- Used to sanitize html from `Message` subject
27+ Used to sanitize html and entities from `Message` subject
2128
2229 ## Example
2330 iex> alias EpochtalkServerWeb.Helpers.Sanitize
24- iex> attrs = %{"subject" => "<strong>Hey this is </strong><br /> <script>a </script> <a href='google.com'>message </a>"}
25- iex> Sanitize.html_from_subject(attrs["subject"], attrs)
26- %{"subject" => "Hey this is a message "}
31+ iex> attrs = %{"subject" => " <strong>Hello World </strong><br /><script></script><a href='google.com'></a>"}
32+ iex> Sanitize.html_and_entities_from_subject( attrs)
33+ %{"subject" => "&nbsp;<strong>Hello World</strong><br /><script></script><a href='google.com'></a> "}
2734 """
28- @ spec html_from_subject ( subject :: String . t ( ) , attrs :: map ) :: map ( )
29- def html_from_subject ( subject , attrs ) when is_binary ( subject ) and is_map ( attrs ) ,
30- do: Map . put ( attrs , "subject" , HtmlSanitizeEx . strip_tags ( subject ) )
35+ @ spec html_and_entities_from_subject ( attrs :: map ) :: map ( )
36+ def html_and_entities_from_subject ( attrs ) when is_map ( attrs ) do
37+ sanitized_subject =
38+ attrs [ "subject" ]
39+ |> String . replace ( ~r/ (?:&)/ , "&" )
40+ |> String . replace ( ~r/ (?:<)/ , "<" )
41+ |> String . replace ( ~r/ (?:>)/ , ">" )
42+
43+ Map . put ( attrs , "subject" , sanitized_subject )
44+ end
3145
3246 @ doc """
33- Used to sanitize all html except basic formatting html from `Thread` or `Post` body
47+ Used to sanitize html and entities from `Thread` or `Post` body, store sanitized body in `body_html`
3448 ## Example
3549 iex> alias EpochtalkServerWeb.Helpers.Sanitize
36- iex> attrs = %{"body" => "<i>Hey <b>this</b> is</i><br /> <h1><script>a</script></h1> <a href='google.com'>post</a>"}
37- iex> Sanitize.html_from_body(attrs["body"], attrs)
38- %{"body" => "<i>Hey <b>this</b> is</i><br /> <h1>a</h1> <a href=\\ " google.com\\ " >post</a>"}
50+ iex> attrs = %{"body" => "<i>Hey <b>this</b> is</i><br /> <h1><script>a</script></h1> <a href='google.com'>post</a> "}
51+ iex> Sanitize.html_and_entities_from_body( attrs)
52+ %{"body" => "<i>Hey <b>this</b> is</i><br /> <h1><script> a</script></ h1> <a href=' google.com' >post</a> ", "body_html" => "<i>Hey <b>this</b> is</i><br /> <h1><script>a</script></h1> <a href='google.com'>post</a> &nbsp; "}
3953 """
40- @ spec html_from_body ( body :: String . t ( ) , attrs :: map ) :: map ( )
41- def html_from_body ( body , attrs ) when is_binary ( body ) and is_map ( attrs ) ,
42- do: Map . put ( attrs , "body" , HtmlSanitizeEx . basic_html ( body ) )
54+ @ spec html_and_entities_from_body ( attrs :: map ) :: map ( )
55+ def html_and_entities_from_body ( attrs ) when is_map ( attrs ) do
56+ sanitized_body =
57+ attrs [ "body" ]
58+ |> String . replace ( ~r/ (?:&)/ , "&" )
59+ |> String . replace ( ~r/ (?:<)/ , "<" )
60+
61+ Map . put ( attrs , "body_html" , sanitized_body )
62+ end
4363end
0 commit comments