From c347549cef996df2f6815b403306f8505af185e2 Mon Sep 17 00:00:00 2001 From: Artur Date: Mon, 5 Jun 2023 09:31:28 +0300 Subject: [PATCH] fix: Theme injection in Safari with polyfill (#16954) The polyfill does not support push or splice, see https://github.com/calebdwilliams/construct-style-sheets/issues/124 --- .../resources/META-INF/frontend/theme-util.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/flow-server/src/main/resources/META-INF/frontend/theme-util.js b/flow-server/src/main/resources/META-INF/frontend/theme-util.js index 75a9a6e9a69..5e4ed19588a 100644 --- a/flow-server/src/main/resources/META-INF/frontend/theme-util.js +++ b/flow-server/src/main/resources/META-INF/frontend/theme-util.js @@ -1,5 +1,8 @@ import stripCssComments from 'strip-css-comments'; +// Safari 15 - 16.3, polyfilled +const polyfilledSafari = CSSStyleSheet.toString().includes('document.createElement'); + const createLinkReferences = (css, target) => { // Unresolved urls are written as '@import url(text);' or '@import "text";' to the css // media query can be present on @media tag or on @import directive after url @@ -42,9 +45,23 @@ const createLinkReferences = (css, target) => { return styleCss; }; +const addAdoptedStyleSafariPolyfill = (sheet, target, first) => { + if (first) { + target.adoptedStyleSheets = [sheet, ...target.adoptedStyleSheets]; + } else { + target.adoptedStyleSheets = [...target.adoptedStyleSheets, sheet]; + } + return () => { + target.adoptedStyleSheets = target.adoptedStyleSheets.filter((ss) => ss !== sheet); + }; +}; + const addAdoptedStyle = (cssText, target, first) => { const sheet = new CSSStyleSheet(); sheet.replaceSync(cssText); + if (polyfilledSafari) { + return addAdoptedStyleSafariPolyfill(sheet, target, first); + } if (first) { target.adoptedStyleSheets.splice(0, 0, sheet); } else {