Skip to content

Commit e0ea5ba

Browse files
committed
Fixed js values escaping
1 parent 5274ee9 commit e0ea5ba

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

sanic_graphql/render_graphiql.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,24 @@
127127
</html>'''
128128

129129

130+
def escape_js_value(value):
131+
quotation = False
132+
if value.startswith('"') and value.endswith('"'):
133+
quotation = True
134+
value = value[1:len(value)-1]
135+
136+
value = value.replace('\\\\n', '\\\\\\n').replace('\\n', '\\\\n')
137+
if quotation:
138+
value = '"' + value.replace('\\\\"', '"').replace('\"', '\\\"') + '"'
139+
140+
return value
141+
142+
130143
def process_var(template, name, value, jsonify=False):
131144
pattern = r'{{\s*' + name + r'(\s*|[^}]+)*\s*}}'
132-
if jsonify:
145+
if jsonify and value not in ['null', 'undefined']:
133146
value = json.dumps(value)
134-
if value.startswith('"') and value.endswith('"') and len(value) > 2:
135-
value = ('"' + (value[1:len(value)-1].replace('\\\\n', '\\\\\\n').replace('\\n', '\\\\n')
136-
.replace('\\\\"', '"').replace('\"', '\\\"')) + '"')
137-
else:
138-
value = value.replace('\\\\n', '\\\\\\n').replace('\\n', '\\\\n')
147+
value = escape_js_value(value)
139148

140149
return re.sub(pattern, value, template)
141150

0 commit comments

Comments
 (0)