Skip to content

Commit 1e92c0b

Browse files
authored
fix: Theme injection in Safari with polyfill (#16954) (#16955)
The polyfill does not support push or splice, see calebdwilliams/construct-style-sheets#124 Fixes #16956
1 parent 970eb95 commit 1e92c0b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

flow-server/src/main/resources/META-INF/frontend/theme-util.js

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import stripCssComments from 'strip-css-comments';
22

3+
// Safari 15 - 16.3, polyfilled
4+
const polyfilledSafari = CSSStyleSheet.toString().includes('document.createElement');
5+
36
const createLinkReferences = (css, target) => {
47
// Unresolved urls are written as '@import url(text);' or '@import "text";' to the css
58
// media query can be present on @media tag or on @import directive after url
@@ -42,9 +45,23 @@ const createLinkReferences = (css, target) => {
4245
return styleCss;
4346
};
4447

48+
const addAdoptedStyleSafariPolyfill = (sheet, target, first) => {
49+
if (first) {
50+
target.adoptedStyleSheets = [sheet, ...target.adoptedStyleSheets];
51+
} else {
52+
target.adoptedStyleSheets = [...target.adoptedStyleSheets, sheet];
53+
}
54+
return () => {
55+
target.adoptedStyleSheets = target.adoptedStyleSheets.filter((ss) => ss !== sheet);
56+
};
57+
};
58+
4559
const addAdoptedStyle = (cssText, target, first) => {
4660
const sheet = new CSSStyleSheet();
4761
sheet.replaceSync(cssText);
62+
if (polyfilledSafari) {
63+
return addAdoptedStyleSafariPolyfill(sheet, target, first);
64+
}
4865
if (first) {
4966
target.adoptedStyleSheets.splice(0, 0, sheet);
5067
} else {

0 commit comments

Comments
 (0)