1 | /**
|
2 | * Proper escaping of JSON for usage as an object literal inside
|
3 | * of a `<script>` tag.
|
4 | *
|
5 | * js implementation of http://golang.org/pkg/encoding/json/#HTMLEscape
|
6 | *
|
7 | * more info: http://timelessrepo.com/json-isnt-a-javascript-subset
|
8 | */
|
9 |
|
10 | ;
|
11 |
|
12 | var ESCAPE_LOOKUP = {
|
13 | '&': '\\u0026',
|
14 | '>': '\\u003e',
|
15 | '<': '\\u003c',
|
16 | '\u2028': '\\u2028',
|
17 | '\u2029': '\\u2029'
|
18 | };
|
19 |
|
20 | var ESCAPE_REGEX = /[&><\u2028\u2029]/g;
|
21 |
|
22 | function escaper(match) {
|
23 | return ESCAPE_LOOKUP[match];
|
24 | }
|
25 |
|
26 | module.exports = function(obj) {
|
27 | return JSON.stringify(obj).replace(ESCAPE_REGEX, escaper);
|
28 | };
|