mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 22:13:26 +00:00
zerver: Remove simplejson dependency.
Modified by tabbott to put the third-party code in a new file. Fixes #6970.
This commit is contained in:
28
zerver/lib/json_encoder_for_html.py
Normal file
28
zerver/lib/json_encoder_for_html.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import json
|
||||
|
||||
# Taken from
|
||||
# https://github.com/simplejson/simplejson/blob/8edc82afcf6f7512b05fba32baa536fe756bd273/simplejson/encoder.py#L378-L402
|
||||
# License: MIT
|
||||
class JSONEncoderForHTML(json.JSONEncoder):
|
||||
"""An encoder that produces JSON safe to embed in HTML.
|
||||
To embed JSON content in, say, a script tag on a web page, the
|
||||
characters &, < and > should be escaped. They cannot be escaped
|
||||
with the usual entities (e.g. &) because they are not expanded
|
||||
within <script> tags.
|
||||
"""
|
||||
|
||||
def encode(self, o):
|
||||
# type: (Dict[str, Any]) -> str
|
||||
# Override JSONEncoder.encode because it has hacks for
|
||||
# performance that make things more complicated.
|
||||
chunks = self.iterencode(o, True)
|
||||
return ''.join(chunks)
|
||||
|
||||
def iterencode(self, o, _one_shot=False):
|
||||
# type: (Dict[str, Any], Optional[bool]) -> Iterator[str]
|
||||
chunks = super().iterencode(o, _one_shot)
|
||||
for chunk in chunks:
|
||||
chunk = chunk.replace('&', '\\u0026')
|
||||
chunk = chunk.replace('<', '\\u003c')
|
||||
chunk = chunk.replace('>', '\\u003e')
|
||||
yield chunk
|
||||
Reference in New Issue
Block a user