From 2c018d19875ced30caafdc40f84ca531de6e6f91 Mon Sep 17 00:00:00 2001 From: Paul Thompson <43618141+PaulJThompson@users.noreply.github.com> Date: Sun, 7 Apr 2019 15:17:43 -0400 Subject: [PATCH] fix: wrap .sheet.cssRules access in try...catch. (#1693) --- src/Clone.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Clone.js b/src/Clone.js index 2d8058b..5a173e1 100644 --- a/src/Clone.js +++ b/src/Clone.js @@ -234,21 +234,25 @@ export class DocumentCloner { return tempIframe; } - if (node instanceof HTMLStyleElement && node.sheet && node.sheet.cssRules) { - const css = [].slice.call(node.sheet.cssRules, 0).reduce((css, rule) => { - try { + try { + if (node instanceof HTMLStyleElement && node.sheet && node.sheet.cssRules) { + const css = [].slice.call(node.sheet.cssRules, 0).reduce((css, rule) => { if (rule && rule.cssText) { return css + rule.cssText; } return css; - } catch (err) { - this.logger.log('Unable to access cssText property', rule.name); - return css; - } - }, ''); - const style = node.cloneNode(false); - style.textContent = css; - return style; + }, ''); + const style = node.cloneNode(false); + style.textContent = css; + return style; + } + } catch (e) { + // accessing node.sheet.cssRules throws a DOMException + this.logger.log('Unable to access cssRules property'); + if (e.name !== 'SecurityError') { + this.logger.log(e); + throw e; + } } return node.cloneNode(false);