File tree Expand file tree Collapse file tree 2 files changed +28
-2
lines changed Expand file tree Collapse file tree 2 files changed +28
-2
lines changed Original file line number Diff line number Diff line change 88from django .utils .safestring import mark_safe
99
1010from filebrowser .settings import EXTENSIONS , SELECT_FORMATS
11-
11+ from filebrowser . utils import json_for_script
1212
1313register = template .Library ()
1414
@@ -155,7 +155,7 @@ def get_file_extensions(qs):
155155 for item in v :
156156 if item :
157157 extensions .append (item )
158- return mark_safe (extensions )
158+ return json_for_script (extensions )
159159
160160
161161# Django 1.9 auto escapes simple_tag unless marked as safe
Original file line number Diff line number Diff line change 44import os
55import unicodedata
66import math
7+ import json
78
89from django .utils import six
910from django .utils .module_loading import import_string
11+ from django .utils .html import format_html
12+ from django .utils .safestring import mark_safe
1013
1114from filebrowser .settings import STRICT_PIL , NORMALIZE_FILENAME , CONVERT_FILENAME
1215from filebrowser .settings import VERSION_PROCESSORS
1922 except ImportError :
2023 import Image
2124
25+ _json_script_escapes = {
26+ ord ('>' ): '\\ u003E' ,
27+ ord ('<' ): '\\ u003C' ,
28+ ord ('&' ): '\\ u0026' ,
29+ }
30+
31+
32+ def json_for_script (value ):
33+ """
34+ Implementation of json_script from Django 2.1
35+ https://github.com/django/django/commit/8c709d79cbd1a7bb975f58090c17a1178a0efb80
36+
37+ If get_file_extensions is a list of unicode characters, JavaScript is unable to handle it and it will break upload.html
38+ This will convert a list of unicode characters into a regular list, mark it safe, and will escape allthe HTML/XML special
39+ characters with their unicode escapes
40+ """
41+ from django .core .serializers .json import DjangoJSONEncoder
42+ json_str = json .dumps (value , cls = DjangoJSONEncoder )
43+ return format_html (
44+ '{}' ,
45+ mark_safe (json_str )
46+ )
47+
2248
2349def convert_filename (value ):
2450 """
You can’t perform that action at this time.
0 commit comments