Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Commit

Permalink
Expose browser unescape (#16)
Browse files Browse the repository at this point in the history
* expose unescape in browser
  • Loading branch information
lhorie committed Nov 10, 2017
1 parent 128c7c3 commit 4ecade8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/__tests__/__browser__/sanitization.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import test from 'tape-cup';
import {html} from '../../sanitization';
import {html, unescape} from '../../sanitization';

test('sanitization api is not bundled', t => {
t.equals(html, void 0);
t.equals(typeof unescape, 'function');
t.end();
});
16 changes: 8 additions & 8 deletions src/sanitization.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Instead, they should use html`<div>{stuff}</div>` so interpolated data gets auto
We trust the markup outside of interpolation because it's code written by a developer with commit permissions,
which can be audited via code reviews
*/
let html, dangerouslySetHTML, consumeSanitizedHTML, escape, unescape;
let html, dangerouslySetHTML, consumeSanitizedHTML, escape;
if (__NODE__) {
const forbiddenChars = {
'<': '\\u003C',
Expand All @@ -16,7 +16,6 @@ if (__NODE__) {
'\u2029': '\\u2029',
};
const replaceForbidden = c => forbiddenChars[c];
const replaceEscaped = c => String.fromCodePoint(parseInt(c.slice(2), 16));

const key = Symbol('sanitized html');
html = ([head, ...rest], ...values) => {
Expand All @@ -33,18 +32,19 @@ if (__NODE__) {
if (str && str[key]) return consumeSanitizedHTML(str);
return String(str).replace(/[<>"/\u2028\u2029]/g, replaceForbidden);
};
unescape = str => {
return str.replace(
/\\u003C|\\u003E|\\u0022|\\u002F|\\u2028|\\u2029/g,
replaceEscaped
);
};
consumeSanitizedHTML = h => {
if (typeof h === 'string') {
throw new Error(`Unsanitized html. Use html\`${h}\``);
}
return h[key];
};
}
const replaceEscaped = c => String.fromCodePoint(parseInt(c.slice(2), 16));
const unescape = str => {
return str.replace(
/\\u003C|\\u003E|\\u0022|\\u002F|\\u2028|\\u2029/g,
replaceEscaped
);
};

export {html, dangerouslySetHTML, consumeSanitizedHTML, escape, unescape};

0 comments on commit 4ecade8

Please sign in to comment.