@@ -78,50 +78,53 @@ def test_remove_xpaths_called_with_enumerable_xpaths
78
78
79
79
def test_strip_tags_with_quote
80
80
input = '<" <img src="trollface.gif" onload="alert(1)"> hi'
81
- result = full_sanitize ( input )
82
81
acceptable_results = [
83
82
# libxml2 >= 2.9.14 and xerces+neko
84
83
%{<" hi} ,
85
84
# other libxml2
86
85
%{ hi} ,
86
+ # preserve_whitespace: true
87
+ "<" hi" ,
87
88
]
88
89
89
- assert_includes ( acceptable_results , result )
90
+ assert_full_sanitized ( acceptable_results , input )
90
91
end
91
92
92
93
def test_strip_invalid_html
93
- assert_equal "<<" , full_sanitize ( "<<<bad html" )
94
+ assert_full_sanitized "<<" , "<<<bad html"
94
95
end
95
96
96
97
def test_strip_nested_tags
97
98
expected = "Wei<a onclick='alert(document.cookie);'/>rdos"
98
99
input = "Wei<<a>a onclick='alert(document.cookie);'</a>/>rdos"
99
- assert_equal expected , full_sanitize ( input )
100
+ assert_full_sanitized expected , input
100
101
end
101
102
102
103
def test_strip_tags_multiline
103
- expected = %{This is a test.\n \n \n \n It no longer contains any HTML.\n }
104
104
input = %{<h1>This is <b>a <a href="" target="_blank">test</a></b>.</h1>\n \n <!-- it has a comment -->\n \n <p>It no <b>longer <strong>contains <em>any <strike>HTML</strike></em>.</strong></b></p>\n }
105
+ acceptable_results = [
106
+ %{This is a test.\n \n \n \n It no longer contains any HTML.\n } ,
107
+ # preserve_whitespace: true
108
+ %{\n This is a test.\n \n It no longer contains any HTML.\n \n }
109
+ ]
105
110
106
- assert_equal expected , full_sanitize ( input )
111
+ assert_full_sanitized acceptable_results , input
107
112
end
108
113
109
114
def test_remove_unclosed_tags
110
115
input = "This is <-- not\n a comment here."
111
- result = full_sanitize ( input )
112
116
acceptable_results = [
113
117
# libxml2 >= 2.9.14 and xerces+neko
114
118
%{This is <-- not\n a comment here.} ,
115
119
# other libxml2
116
120
%{This is } ,
117
121
]
118
122
119
- assert_includes ( acceptable_results , result )
123
+ assert_full_sanitized ( acceptable_results , input )
120
124
end
121
125
122
126
def test_strip_cdata
123
127
input = "This has a <![CDATA[<section>]]> here."
124
- result = full_sanitize ( input )
125
128
acceptable_results = [
126
129
# libxml2 = 2.9.14
127
130
%{This has a <![CDATA[]]> here.} ,
@@ -131,7 +134,7 @@ def test_strip_cdata
131
134
%{This has a here.} ,
132
135
]
133
136
134
- assert_includes ( acceptable_results , result )
137
+ assert_full_sanitized ( acceptable_results , input )
135
138
end
136
139
137
140
def test_strip_unclosed_cdata
@@ -153,40 +156,52 @@ def test_strip_unclosed_cdata
153
156
154
157
def test_strip_blank_string
155
158
assert_nil full_sanitize ( nil )
156
- assert_equal "" , full_sanitize ( "" )
157
- assert_equal " " , full_sanitize ( " " )
159
+ assert_nil full_sanitize ( nil , preserve_whitespace : true )
160
+ assert_full_sanitized "" , ""
161
+ assert_full_sanitized " " , " "
158
162
end
159
163
160
164
def test_strip_tags_with_plaintext
161
- assert_equal "Don't touch me" , full_sanitize ( "Don't touch me" )
165
+ assert_full_sanitized "Don't touch me" , "Don't touch me"
162
166
end
163
167
164
168
def test_strip_tags_with_tags
165
- assert_equal "This is a test." , full_sanitize ( "<p >This <u>is<u> a <a href='test.html'><strong>test</strong></a>.</p>" )
169
+ assert_full_sanitized "This is a test." , "<b >This <u>is<u> a <a href='test.html'><strong>test</strong></a>.</b>"
166
170
end
167
171
168
172
def test_escape_tags_with_many_open_quotes
169
- assert_equal "<<" , full_sanitize ( "<<<bad html>" )
173
+ assert_full_sanitized "<<" , "<<<bad html>"
170
174
end
171
175
172
176
def test_strip_tags_with_sentence
173
- assert_equal "This is a test." , full_sanitize ( "This is a test." )
177
+ assert_full_sanitized "This is a test." , "This is a test."
174
178
end
175
179
176
180
def test_strip_tags_with_comment
177
- assert_equal "This has a here." , full_sanitize ( "This has a <!-- comment --> here." )
181
+ assert_full_sanitized "This has a here." , "This has a <!-- comment --> here."
178
182
end
179
183
180
184
def test_strip_tags_with_frozen_string
181
- assert_equal "Frozen string with no tags" , full_sanitize ( "Frozen string with no tags" )
185
+ assert_full_sanitized "Frozen string with no tags" , "Frozen string with no tags"
182
186
end
183
187
184
188
def test_full_sanitize_respect_html_escaping_of_the_given_string
185
- assert_equal 'test\r\nstring' , full_sanitize ( 'test\r\nstring' )
186
- assert_equal "&" , full_sanitize ( "&" )
187
- assert_equal "&" , full_sanitize ( "&" )
188
- assert_equal "&amp;" , full_sanitize ( "&amp;" )
189
- assert_equal "omg <script>BOM</script>" , full_sanitize ( "omg <script>BOM</script>" )
189
+ assert_full_sanitized 'test\r\nstring' , 'test\r\nstring'
190
+ assert_full_sanitized "&" , "&"
191
+ assert_full_sanitized "&" , "&"
192
+ assert_full_sanitized "&amp;" , "&amp;"
193
+ assert_full_sanitized "omg <script>BOM</script>" , "omg <script>BOM</script>"
194
+ end
195
+
196
+ def test_full_sanitize_preserve_whitespace
197
+ assert_equal "\n a\n \n b\n " , full_sanitize ( "<p>a</p><p>b</p>" , preserve_whitespace : true )
198
+ end
199
+
200
+ def test_full_sanitize_preserve_whitespace_ascii_8bit_string
201
+ full_sanitize ( "<a>hello</a>" . encode ( "ASCII-8BIT" ) ) . tap do |sanitized |
202
+ assert_equal "hello" , sanitized
203
+ assert_equal Encoding ::UTF_8 , sanitized . encoding
204
+ end
190
205
end
191
206
192
207
def test_strip_links_with_tags_in_tags
@@ -917,6 +932,11 @@ def assert_sanitized(input, expected = nil)
917
932
assert_equal ( ( expected || input ) , safe_list_sanitize ( input ) )
918
933
end
919
934
935
+ def assert_full_sanitized ( acceptable_results , input )
936
+ assert_includes ( Array ( acceptable_results ) , full_sanitize ( input ) )
937
+ assert_includes ( Array ( acceptable_results ) , full_sanitize ( input , preserve_whitespace : true ) )
938
+ end
939
+
920
940
def sanitize_css ( input )
921
941
Rails ::Html ::SafeListSanitizer . new . sanitize_css ( input )
922
942
end
0 commit comments