Skip to content

Commit 5018473

Browse files
committed
fix(cli): escape \u2029 \u2028 characters
see http://www.thespanner.co.uk/2011/07/25/the-json-specification-is-now-wrong/ related to #475
1 parent 2654cef commit 5018473

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

cli/index.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ yargs
6767
console.log(e.stack);
6868
}
6969
},
70-
)
70+
)
7171
.command(
7272
'bundle [spec]',
7373
'bundle spec into zero-dependency HTML-file',
@@ -112,7 +112,7 @@ yargs
112112
console.log(e.message);
113113
}
114114
},
115-
)
115+
)
116116
.demandCommand()
117117
.options('t', {
118118
alias: 'template',
@@ -219,20 +219,20 @@ async function getPageHTML(
219219
redocHTML: `
220220
<div id="redoc">${(ssr && html) || ''}</div>
221221
<script>
222-
${(ssr && `const __redoc_state = ${JSON.stringify(state)};`) || ''}
223-
222+
${(ssr && `const __redoc_state = ${escapeUnicode(JSON.stringify(state))};`) || ''}
223+
224224
var container = document.getElementById('redoc');
225225
Redoc.${
226226
ssr
227227
? 'hydrate(__redoc_state, container);'
228228
: `init("spec.json", ${JSON.stringify(redocOptions)}, container)`
229-
};
229+
};
230230
231231
</script>`,
232232
redocHead: ssr
233233
? (cdn
234-
? '<script src="https://unpkg.com/redoc@next/bundles/redoc.standalone.js"></script>'
235-
: `<script>${redocStandaloneSrc}</script>`) + css
234+
? '<script src="https://unpkg.com/redoc@next/bundles/redoc.standalone.js"></script>'
235+
: `<script>${redocStandaloneSrc}</script>`) + css
236236
: '<script src="redoc.standalone.js"></script>',
237237
title: title,
238238
});
@@ -288,3 +288,8 @@ function debounce(callback: Function, time: number) {
288288
function isURL(str: string): boolean {
289289
return /^(https?:)\/\//m.test(str);
290290
}
291+
292+
// see http://www.thespanner.co.uk/2011/07/25/the-json-specification-is-now-wrong/
293+
function escapeUnicode(str) {
294+
return str.replace(/\u2028|\u2029/g, m => '\\u202' + (m === '\u2028' ? '8' : '9'));
295+
}

0 commit comments

Comments
 (0)