diff --git a/swh/web/browse/utils.py b/swh/web/browse/utils.py --- a/swh/web/browse/utils.py +++ b/swh/web/browse/utils.py @@ -280,14 +280,16 @@ if mime_type.startswith('image/'): if mime_type in _browsers_supported_image_mimes: - content_data = base64.b64encode(content_data) - content_data = content_data.decode('utf-8') + content_data = base64.b64encode(content_data).decode('utf-8') else: content_data = None if mime_type.startswith('image/svg'): mime_type = 'image/svg+xml' + if mime_type.startswith('text/'): + content_data = content_data.decode('utf-8') + return {'content_data': content_data, 'language': language, 'mimetype': mime_type} diff --git a/swh/web/tests/browse/views/test_content.py b/swh/web/tests/browse/views/test_content.py --- a/swh/web/tests/browse/views/test_content.py +++ b/swh/web/tests/browse/views/test_content.py @@ -378,5 +378,9 @@ mime_type, encoding, content_data = _re_encode_content( mime_type, encoding, content_data['data']) - return prepare_content_for_display(content_data, mime_type, - content['path']) + content_display = prepare_content_for_display(content_data, mime_type, + content['path']) + + assert type(content_display['content_data']) == str + + return content_display diff --git a/swh/web/tests/browse/views/test_origin.py b/swh/web/tests/browse/views/test_origin.py --- a/swh/web/tests/browse/views/test_origin.py +++ b/swh/web/tests/browse/views/test_origin.py @@ -564,6 +564,8 @@ assert resp.status_code == 200 assert_template_used(resp, 'browse/content.html') + assert type(content['data']) == str + assert_contains(resp, '' % content['hljs_language']) assert_contains(resp, escape(content['data'])) diff --git a/swh/web/tests/data.py b/swh/web/tests/data.py --- a/swh/web/tests/data.py +++ b/swh/web/tests/data.py @@ -24,7 +24,8 @@ from swh.storage.algos.dir_iterators import dir_iterator from swh.web import config from swh.web.browse.utils import ( - get_mimetype_and_encoding_for_content, prepare_content_for_display + get_mimetype_and_encoding_for_content, prepare_content_for_display, + _re_encode_content ) from swh.web.common import service from swh.web.common.highlightjs import get_hljs_language_from_filename @@ -239,6 +240,7 @@ path = content_path[content_metadata['sha1']] cnt = next(storage.content_get([content_metadata['sha1']])) mimetype, encoding = get_mimetype_and_encoding_for_content(cnt['data']) + _, _, cnt['data'] = _re_encode_content(mimetype, encoding, cnt['data']) content_display_data = prepare_content_for_display( cnt['data'], mimetype, path) contents[-1]['path'] = path