JSON.jl
JSON.jl copied to clipboard
add escapes suitable for HTML embedding
Often, I've seen JSON
used to encode content so that it could be included in a <script
tag within Pluto.jl
notebooks. However, within a HTML <script>
tag, additional escaping is needed:
The easiest and safest way to avoid the rather strange restrictions described in this section is to always escape an ASCII case-insensitive match for "<!--" as "<\!--", "<script" as "<\script", and "</script" as "<\/script" when these sequences appear in literals in scripts (e.g. in strings, regular expressions, or comments), and to avoid writing code that uses such constructs in expressions.
The issue here is that JSON doesn't like \\!
, \\s
, and \\S
, so I suppose one would have to use the equivalent unicode escape sequences: "<!"
as "<\\u0021"
, "<s"
as "<\\u0073"
, "<S"
as "<\\u0053"
, and "</"
as "<\\/"
.
It'd be perhaps useful to additionally escape these sequences. It won't hurt normal JSON encodings, but would make JSON safe for making Javascript content to be embedded in HTML.