fix(cli): escape \u2029 \u2028 characters

see http://www.thespanner.co.uk/2011/07/25/the-json-specification-is-now-wrong/
related to #475
This commit is contained in:
Roman Hotsiy 2018-05-17 11:43:53 +03:00
parent 2654cefd75
commit 50184739c7
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0

View File

@ -67,7 +67,7 @@ yargs
console.log(e.stack); console.log(e.stack);
} }
}, },
) )
.command( .command(
'bundle [spec]', 'bundle [spec]',
'bundle spec into zero-dependency HTML-file', 'bundle spec into zero-dependency HTML-file',
@ -112,7 +112,7 @@ yargs
console.log(e.message); console.log(e.message);
} }
}, },
) )
.demandCommand() .demandCommand()
.options('t', { .options('t', {
alias: 'template', alias: 'template',
@ -219,20 +219,20 @@ async function getPageHTML(
redocHTML: ` redocHTML: `
<div id="redoc">${(ssr && html) || ''}</div> <div id="redoc">${(ssr && html) || ''}</div>
<script> <script>
${(ssr && `const __redoc_state = ${JSON.stringify(state)};`) || ''} ${(ssr && `const __redoc_state = ${escapeUnicode(JSON.stringify(state))};`) || ''}
var container = document.getElementById('redoc'); var container = document.getElementById('redoc');
Redoc.${ Redoc.${
ssr ssr
? 'hydrate(__redoc_state, container);' ? 'hydrate(__redoc_state, container);'
: `init("spec.json", ${JSON.stringify(redocOptions)}, container)` : `init("spec.json", ${JSON.stringify(redocOptions)}, container)`
}; };
</script>`, </script>`,
redocHead: ssr redocHead: ssr
? (cdn ? (cdn
? '<script src="https://unpkg.com/redoc@next/bundles/redoc.standalone.js"></script>' ? '<script src="https://unpkg.com/redoc@next/bundles/redoc.standalone.js"></script>'
: `<script>${redocStandaloneSrc}</script>`) + css : `<script>${redocStandaloneSrc}</script>`) + css
: '<script src="redoc.standalone.js"></script>', : '<script src="redoc.standalone.js"></script>',
title: title, title: title,
}); });
@ -288,3 +288,8 @@ function debounce(callback: Function, time: number) {
function isURL(str: string): boolean { function isURL(str: string): boolean {
return /^(https?:)\/\//m.test(str); return /^(https?:)\/\//m.test(str);
} }
// 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'));
}