Skip to content

Commit

Permalink
fix: Theme injection in Safari with polyfill (#16954)
Browse files Browse the repository at this point in the history
The polyfill does not support push or splice, see calebdwilliams/construct-style-sheets#124
  • Loading branch information
Artur- authored Jun 5, 2023
1 parent 21afecd commit e61f39a
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions flow-server/src/main/resources/META-INF/frontend/theme-util.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit e61f39a

Please sign in to comment.