fix(cli): cli output crashes if script closing tag is in the spec

fixes #563
This commit is contained in:
Roman Hotsiy 2018-07-17 15:16:06 +03:00
parent 32df7767fd
commit 76906eb126
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0

View File

@ -236,7 +236,7 @@ async function getPageHTML(
redocHTML: `
<div id="redoc">${(ssr && html) || ''}</div>
<script>
${(ssr && `const __redoc_state = ${escapeUnicode(JSON.stringify(state))};`) || ''}
${(ssr && `const __redoc_state = ${sanitizeJSONString(JSON.stringify(state))};`) || ''}
var container = document.getElementById('redoc');
Redoc.${
@ -306,6 +306,15 @@ function isURL(str: string): boolean {
return /^(https?:)\/\//m.test(str);
}
function sanitizeJSONString(str: string) {
return escapeClosingScriptTag(escapeUnicode(str));
}
// see http://www.thespanner.co.uk/2011/07/25/the-json-specification-is-now-wrong/
function escapeClosingScriptTag(str) {
return str.replace(/<\/script>/g, '<\\/script>');
}
// see http://www.thespanner.co.uk/2011/07/25/the-json-specification-is-now-wrong/
function escapeUnicode(str) {
return str.replace(/\u2028|\u2029/g, m => '\\u202' + (m === '\u2028' ? '8' : '9'));