From 903c3c08b2f51a052ab4b422cdd6c8fb5cf27116 Mon Sep 17 00:00:00 2001 From: Matthew Roberts Date: Wed, 31 Jul 2024 16:30:20 -0400 Subject: [PATCH 1/2] add nonce support --- src/NewWindow.js | 12 ++++++++---- types/NewWindow.d.ts | 5 +++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/NewWindow.js b/src/NewWindow.js index 19dbfb2..b5c8e6c 100644 --- a/src/NewWindow.js +++ b/src/NewWindow.js @@ -25,7 +25,8 @@ class NewWindow extends React.PureComponent { onUnload: null, center: 'parent', copyStyles: true, - closeOnUnmount: true + closeOnUnmount: true, + nonce: null } /** @@ -134,7 +135,7 @@ class NewWindow extends React.PureComponent { // If specified, copy styles from parent window's document. if (this.props.copyStyles) { - setTimeout(() => copyStyles(document, this.window.document), 0) + setTimeout(() => copyStyles(document, this.window.document, this.props.nonce), 0) } if (typeof onOpen === 'function') { @@ -206,7 +207,8 @@ NewWindow.propTypes = { onOpen: PropTypes.func, center: PropTypes.oneOf(['parent', 'screen']), copyStyles: PropTypes.bool, - closeOnUnmount: PropTypes.bool + closeOnUnmount: PropTypes.bool, + nonce: PropTypes.string } /** @@ -221,7 +223,7 @@ NewWindow.propTypes = { * @private */ -function copyStyles(source, target) { +function copyStyles(source, target, nonce) { // Store style tags, avoid reflow in the loop const headFrag = target.createDocumentFragment() @@ -275,6 +277,7 @@ function copyStyles(source, target) { const newStyleEl = target.createElement('style') newStyleEl.textContent = ruleText.join('\n') + if (nonce) newStyleEl.setAttribute('nonce', nonce); headFrag.appendChild(newStyleEl) } else if (styleSheet.href) { // for elements loading CSS from a URL @@ -282,6 +285,7 @@ function copyStyles(source, target) { newLinkEl.rel = 'stylesheet' newLinkEl.href = styleSheet.href + if (nonce) newLinkEl.setAttribute('nonce', nonce); headFrag.appendChild(newLinkEl) } }) diff --git a/types/NewWindow.d.ts b/types/NewWindow.d.ts index 7a32ce8..84461f5 100644 --- a/types/NewWindow.d.ts +++ b/types/NewWindow.d.ts @@ -76,6 +76,11 @@ declare module 'react-new-window' { * If specified, close the new window on unmount. */ closeOnUnmount?: boolean + + /** + * If specified, adds nonce to style elements. + */ + nonce?: string | null } export default class NewWindow extends React.PureComponent { From 324acad03553a0032e77332da0e1e9274e0f4bc0 Mon Sep 17 00:00:00 2001 From: Matthew Roberts Date: Wed, 31 Jul 2024 16:44:45 -0400 Subject: [PATCH 2/2] format --- types/NewWindow.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/NewWindow.d.ts b/types/NewWindow.d.ts index 84461f5..bc9310f 100644 --- a/types/NewWindow.d.ts +++ b/types/NewWindow.d.ts @@ -71,13 +71,13 @@ declare module 'react-new-window' { * If specified, copy styles from parent window's document. */ copyStyles?: boolean - + /** * If specified, close the new window on unmount. */ closeOnUnmount?: boolean - /** + /** * If specified, adds nonce to style elements. */ nonce?: string | null