Skip to content

Commit

Permalink
Corrige le problème d'encodage dans l'historique d'un contenu (#4826)
Browse files Browse the repository at this point in the history
Corrige le problème d'encodage dans l'historique d'un contenu et ajoute des tests

also Fix templatetag
  • Loading branch information
Anto59290 authored and artragis committed Jan 2, 2018
1 parent d4e1cf4 commit 8aebf47
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
17 changes: 13 additions & 4 deletions zds/utils/templatetags/htmldiff.py
Expand Up @@ -10,13 +10,22 @@

@register.simple_tag
def htmldiff(string1, string2):
txt1 = str(string1).splitlines()
txt2 = str(string2).splitlines()

try:
txt1 = string1.decode('utf-8').splitlines()
# string1 is an empty SafeText from template
except AttributeError:
txt1 = string1.splitlines()

try:
txt2 = string2.decode('utf-8').splitlines()
except AttributeError:
txt2 = string2.splitlines()

diff = HtmlDiff(tabsize=4, wrapcolumn=80)
result = diff.make_table(txt1, txt2, context=True, numlines=2)

if '<td> No Differences Found </td>' in result:
return _('<p>Pas de changements.</p>')
if 'No Differences Found' in result:
return format_html('<p>{}</p>', _('Pas de changements.'))
else:
return format_html('<div class="diff_delta">{}</div>', mark_safe(result))
16 changes: 16 additions & 0 deletions zds/utils/templatetags/tests/test_htmldiff.py
@@ -0,0 +1,16 @@
from django.test import TestCase

from zds.utils.templatetags.htmldiff import htmldiff


class HtmlDiffTests(TestCase):

def test_empty(self):
self.assertEquals(htmldiff('essai'.encode(), 'essai'.encode()), '<p>Pas de changements.</p>')

def test_nominal(self):
self.assertIn('Agrume', htmldiff('Agrume'.encode(), ''.encode()))

def test_encoding(self):
# Regression test for issue #4824
self.assertIn('Étrange&nbsp;caractère', htmldiff('Étrange caractère'.encode(), ''.encode()))

0 comments on commit 8aebf47

Please sign in to comment.