@@ -211,36 +211,151 @@ def test_has_changed_richtext(self):
211211 self .assertIsInstance (comparison .htmldiff (), SafeString )
212212 self .assertTrue (comparison .has_changed ())
213213
214- def test_htmldiff_escapes_value (self ):
214+ def test_htmldiff_escapes_value_on_change (self ):
215215 field = StreamPage ._meta .get_field ('body' )
216216
217217 comparison = self .comparison_class (
218218 field ,
219219 StreamPage (body = StreamValue (field .stream_block , [
220- ('text' , "Original content" , '1' ),
220+ ('text' , "I <b>really</b> like original<i>ish</i> content" , '1' ),
221+ ])),
222+ StreamPage (body = StreamValue (field .stream_block , [
223+ ('text' , 'I <b>really</b> like evil code <script type="text/javascript">doSomethingBad();</script>' , '1' ),
224+ ])),
225+ )
226+
227+ self .assertEqual (comparison .htmldiff (), '<div class="comparison__child-object">I <b>really</b> like <span class="deletion">original<i>ish</i> content</span><span class="addition">evil code <script type="text/javascript">doSomethingBad();</script></span></div>' )
228+ self .assertIsInstance (comparison .htmldiff (), SafeString )
229+
230+ def test_htmldiff_escapes_value_on_addition (self ):
231+ field = StreamPage ._meta .get_field ('body' )
232+
233+ comparison = self .comparison_class (
234+ field ,
235+ StreamPage (body = StreamValue (field .stream_block , [
236+ ('text' , "Original <em>and unchanged</em> content" , '1' ),
237+ ])),
238+ StreamPage (body = StreamValue (field .stream_block , [
239+ ('text' , "Original <em>and unchanged</em> content" , '1' ),
240+ ('text' , '<script type="text/javascript">doSomethingBad();</script>' , '2' ),
241+ ])),
242+ )
243+
244+ self .assertEqual (comparison .htmldiff (), '<div class="comparison__child-object">Original <em>and unchanged</em> content</div>\n <div class="comparison__child-object addition"><script type="text/javascript">doSomethingBad();</script></div>' )
245+ self .assertIsInstance (comparison .htmldiff (), SafeString )
246+
247+ def test_htmldiff_escapes_value_on_deletion (self ):
248+ field = StreamPage ._meta .get_field ('body' )
249+
250+ comparison = self .comparison_class (
251+ field ,
252+ StreamPage (body = StreamValue (field .stream_block , [
253+ ('text' , "Original <em>and unchanged</em> content" , '1' ),
254+ ('text' , '<script type="text/javascript">doSomethingBad();</script>' , '2' ),
221255 ])),
222256 StreamPage (body = StreamValue (field .stream_block , [
223- ('text' , '<script type="text/javascript">doSomethingBad();</script>' , '1' ),
257+ ('text' , "Original <em>and unchanged</em> content" , '1' ),
224258 ])),
225259 )
226260
227- self .assertEqual (comparison .htmldiff (), '<div class="comparison__child-object"><span class="deletion"> Original content</span><span class="addition "><script type="text/javascript">doSomethingBad();</script></span> </div>' )
261+ self .assertEqual (comparison .htmldiff (), '<div class="comparison__child-object">Original <em>and unchanged</em> content</div> \n <div class="comparison__child-object deletion "><script type="text/javascript">doSomethingBad();</script></div>' )
228262 self .assertIsInstance (comparison .htmldiff (), SafeString )
229263
230- def test_htmldiff_escapes_value_richtext (self ):
264+ def test_htmldiff_richtext_strips_tags_on_change (self ):
231265 field = StreamPage ._meta .get_field ('body' )
232266
233267 comparison = self .comparison_class (
234268 field ,
235269 StreamPage (body = StreamValue (field .stream_block , [
236- ('rich_text' , "Original content " , '1' ),
270+ ('rich_text' , "I <b>really</b> like Wagtail <3 " , '1' ),
237271 ])),
238272 StreamPage (body = StreamValue (field .stream_block , [
239- ('rich_text' , '<script type="text/javascript">doSomethingBad();</script>' , '1' ),
273+ ('rich_text' , 'I <b>really</b> like evil code >_< <script type="text/javascript">doSomethingBad();</script>' , '1' ),
240274 ])),
241275 )
242276
243- self .assertEqual (comparison .htmldiff (), '<div class="comparison__child-object"><span class="deletion">Original content</span><span class="addition">doSomethingBad();</span></div>' )
277+ self .assertEqual (comparison .htmldiff (), '<div class="comparison__child-object">I really like <span class="deletion">Wagtail <3</span><span class="addition">evil code >_< doSomethingBad();</span></div>' )
278+ self .assertIsInstance (comparison .htmldiff (), SafeString )
279+
280+ def test_htmldiff_richtext_strips_tags_on_addition (self ):
281+ field = StreamPage ._meta .get_field ('body' )
282+
283+ comparison = self .comparison_class (
284+ field ,
285+ StreamPage (body = StreamValue (field .stream_block , [
286+ ('rich_text' , "Original <em>and unchanged</em> content" , '1' ),
287+ ])),
288+ StreamPage (body = StreamValue (field .stream_block , [
289+ ('rich_text' , "Original <em>and unchanged</em> content" , '1' ),
290+ ('rich_text' , 'I <b>really</b> like evil code >_< <script type="text/javascript">doSomethingBad();</script>' , '2' ),
291+ ])),
292+ )
293+
294+ self .assertEqual (comparison .htmldiff (), '<div class="comparison__child-object">Original and unchanged content</div>\n <div class="comparison__child-object addition">I really like evil code >_< doSomethingBad();</div>' )
295+ self .assertIsInstance (comparison .htmldiff (), SafeString )
296+
297+ def test_htmldiff_richtext_strips_tags_on_deletion (self ):
298+ field = StreamPage ._meta .get_field ('body' )
299+
300+ comparison = self .comparison_class (
301+ field ,
302+ StreamPage (body = StreamValue (field .stream_block , [
303+ ('rich_text' , "Original <em>and unchanged</em> content" , '1' ),
304+ ('rich_text' , 'I <b>really</b> like evil code >_< <script type="text/javascript">doSomethingBad();</script>' , '2' ),
305+ ])),
306+ StreamPage (body = StreamValue (field .stream_block , [
307+ ('rich_text' , "Original <em>and unchanged</em> content" , '1' ),
308+ ])),
309+ )
310+
311+ self .assertEqual (comparison .htmldiff (), '<div class="comparison__child-object">Original and unchanged content</div>\n <div class="comparison__child-object deletion">I really like evil code >_< doSomethingBad();</div>' )
312+ self .assertIsInstance (comparison .htmldiff (), SafeString )
313+
314+ def test_htmldiff_raw_html_escapes_value_on_change (self ):
315+ field = StreamPage ._meta .get_field ('body' )
316+
317+ comparison = self .comparison_class (
318+ field ,
319+ StreamPage (body = StreamValue (field .stream_block , [
320+ ('raw_html' , "Original<i>ish</i> content" , '1' ),
321+ ])),
322+ StreamPage (body = StreamValue (field .stream_block , [
323+ ('raw_html' , '<script type="text/javascript">doSomethingBad();</script>' , '1' ),
324+ ])),
325+ )
326+ self .assertEqual (comparison .htmldiff (), '<div class="comparison__child-object"><span class="deletion">Original<i>ish</i> content</span><span class="addition"><script type="text/javascript">doSomethingBad();</script></span></div>' )
327+ self .assertIsInstance (comparison .htmldiff (), SafeString )
328+
329+ def test_htmldiff_raw_html_escapes_value_on_addition (self ):
330+ field = StreamPage ._meta .get_field ('body' )
331+
332+ comparison = self .comparison_class (
333+ field ,
334+ StreamPage (body = StreamValue (field .stream_block , [
335+ ('raw_html' , "Original <em>and unchanged</em> content" , '1' ),
336+ ])),
337+ StreamPage (body = StreamValue (field .stream_block , [
338+ ('raw_html' , "Original <em>and unchanged</em> content" , '1' ),
339+ ('raw_html' , '<script type="text/javascript">doSomethingBad();</script>' , '2' ),
340+ ])),
341+ )
342+ self .assertEqual (comparison .htmldiff (), '<div class="comparison__child-object">Original <em>and unchanged</em> content</div>\n <div class="comparison__child-object addition"><script type="text/javascript">doSomethingBad();</script></div>' )
343+ self .assertIsInstance (comparison .htmldiff (), SafeString )
344+
345+ def test_htmldiff_raw_html_escapes_value_on_deletion (self ):
346+ field = StreamPage ._meta .get_field ('body' )
347+
348+ comparison = self .comparison_class (
349+ field ,
350+ StreamPage (body = StreamValue (field .stream_block , [
351+ ('raw_html' , "Original <em>and unchanged</em> content" , '1' ),
352+ ('raw_html' , '<script type="text/javascript">doSomethingBad();</script>' , '2' ),
353+ ])),
354+ StreamPage (body = StreamValue (field .stream_block , [
355+ ('raw_html' , "Original <em>and unchanged</em> content" , '1' ),
356+ ])),
357+ )
358+ self .assertEqual (comparison .htmldiff (), '<div class="comparison__child-object">Original <em>and unchanged</em> content</div>\n <div class="comparison__child-object deletion"><script type="text/javascript">doSomethingBad();</script></div>' )
244359 self .assertIsInstance (comparison .htmldiff (), SafeString )
245360
246361 def test_compare_structblock (self ):
0 commit comments