Skip to content

Commit

Permalink
fix(cli): cli output crashes if script closing tag is in the spec
Browse files Browse the repository at this point in the history
fixes #563
  • Loading branch information
RomanHotsiy committed Jul 17, 2018
1 parent 32df776 commit 76906eb
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.${
Expand Down Expand Up @@ -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'));
Expand Down

0 comments on commit 76906eb

Please sign in to comment.