From 6b5292d342038bb128a69d969294b844a96d030e Mon Sep 17 00:00:00 2001 From: Nordine M'LISSA Date: Thu, 6 Sep 2018 21:03:19 +0200 Subject: [PATCH] Better code structure --- src/IconInner.js | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/IconInner.js b/src/IconInner.js index 4572fa8..70b2ddd 100644 --- a/src/IconInner.js +++ b/src/IconInner.js @@ -8,23 +8,28 @@ import icons from './icons.json'; class IconInner extends PureComponent { createMarkup(markup) { - // sanitize markup first: - let sanitizedMarkup = DOMPurify.sanitize(markup); - - // handling server rendering - if (!window) { - // We create a window out of JSDOM - const window = (new JSDOM('')).window; - // Plug it to DOMPurify - const DOMPurifyServer = DOMPurify(window); - // and then sanitize markup - sanitizedMarkup = DOMPurifyServer.sanitize(markup); - } + const sanitizedMarkup = this.sanitizeMarkup(markup); // now do the weird thing for dangerouslySetInnerHTML return { __html: sanitizedMarkup }; } + + sanitizeMarkup(markup) { + // For server environement + if (!window) { + // We create a window out of JSDOM + const window = (new JSDOM('')).window; + // Then we plug it to DOMPurify + const DOMPurifyServer = DOMPurify(window); + // and finally sanitize the markup + return DOMPurifyServer.sanitize(markup); + } + + // Sanitize the markup + return DOMPurify.sanitize(markup); + } + render() { // is just a wrapper it does nothing except let me use valid JSX markup // icons are based on generated icons.json from feather lib