Skip to content

Commit ed43236

Browse files
committed
fix: contructable stylesheets with older immutable spec (chrome <99)
Resolves #6326
1 parent f018c73 commit ed43236

File tree

6 files changed

+35
-4
lines changed

6 files changed

+35
-4
lines changed

src/client/client-window.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,10 @@ export const supportsConstructableStylesheets = BUILD.constructableCSS
6060
})()
6161
: false;
6262

63+
// https://github.com/salesforce/lwc/blob/5af18fdd904bc6cfcf7b76f3c539490ff11515b2/packages/%40lwc/engine-dom/src/renderer.ts#L41-L43
64+
export const supportsMutableAdoptedStyleSheets = supportsConstructableStylesheets
65+
? /*@__PURE__*/ (() =>
66+
!!win.document && Object.getOwnPropertyDescriptor(win.document.adoptedStyleSheets, 'length')!.writable)()
67+
: false;
68+
6369
export { H as HTMLElement };

src/hydrate/platform/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export const supportsShadow = BUILD.shadowDom;
124124
export const supportsListenerOptions = false;
125125

126126
export const supportsConstructableStylesheets = false;
127+
export const supportsMutableAdoptedStyleSheets = false;
127128

128129
export const getHostRef = (ref: d.RuntimeRef) => {
129130
if (ref.__stencil__getHostRef) {

src/runtime/styles.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { BUILD } from '@app-data';
2-
import { plt, styles, supportsConstructableStylesheets, supportsShadow, win } from '@platform';
2+
import {
3+
plt,
4+
styles,
5+
supportsConstructableStylesheets,
6+
supportsMutableAdoptedStyleSheets,
7+
supportsShadow,
8+
win,
9+
} from '@platform';
310
import { CMP_FLAGS, queryNonceMetaTagContent } from '@utils';
411

512
import type * as d from '../declarations';
@@ -125,7 +132,11 @@ export const addStyle = (styleContainerNode: any, cmpMeta: d.ComponentRuntimeMet
125132
* > If the array needs to be modified, use in-place mutations like push().
126133
* https://developer.mozilla.org/en-US/docs/Web/API/Document/adoptedStyleSheets
127134
*/
128-
styleContainerNode.adoptedStyleSheets.unshift(stylesheet);
135+
if (supportsMutableAdoptedStyleSheets) {
136+
styleContainerNode.adoptedStyleSheets.unshift(stylesheet);
137+
} else {
138+
styleContainerNode.adoptedStyleSheets = [stylesheet, ...styleContainerNode.adoptedStyleSheets];
139+
}
129140
} else {
130141
/**
131142
* If a scoped component is used within a shadow root and constructable stylesheets are
@@ -171,7 +182,11 @@ export const addStyle = (styleContainerNode: any, cmpMeta: d.ComponentRuntimeMet
171182
* > If the array needs to be modified, use in-place mutations like push().
172183
* https://developer.mozilla.org/en-US/docs/Web/API/Document/adoptedStyleSheets
173184
*/
174-
styleContainerNode.adoptedStyleSheets.push(style);
185+
if (supportsMutableAdoptedStyleSheets) {
186+
styleContainerNode.adoptedStyleSheets.push(style);
187+
} else {
188+
styleContainerNode.adoptedStyleSheets = [...styleContainerNode.adoptedStyleSheets, style];
189+
}
175190
}
176191
}
177192
return scopeId;

src/testing/platform/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export {
1414
stopAutoApplyChanges,
1515
supportsConstructableStylesheets,
1616
supportsListenerOptions,
17+
supportsMutableAdoptedStyleSheets,
1718
supportsShadow,
1819
} from './testing-platform';
1920
export { flushAll, flushLoadModule, flushQueue, loadModule, nextTick, readTask, writeTask } from './testing-task-queue';

src/testing/platform/testing-platform.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const setPlatformHelpers = (helpers: {
2828

2929
export const supportsListenerOptions = true;
3030
export const supportsConstructableStylesheets = false;
31+
export const supportsMutableAdoptedStyleSheets = false;
3132

3233
/**
3334
* Helper function to programmatically set shadow DOM support in testing scenarios.

src/utils/shadow-root.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { BUILD } from '@app-data';
22
import { globalStyles } from '@app-globals';
3+
import { supportsMutableAdoptedStyleSheets } from '@platform';
34
import { CMP_FLAGS } from '@utils';
45

56
import type * as d from '../declarations';
@@ -19,5 +20,11 @@ export function createShadowRoot(this: HTMLElement, cmpMeta: d.ComponentRuntimeM
1920
if (globalStyleSheet === undefined) globalStyleSheet = createStyleSheetIfNeededAndSupported(globalStyles) ?? null;
2021

2122
// Use initialized global stylesheet if available
22-
if (globalStyleSheet) shadowRoot.adoptedStyleSheets.push(globalStyleSheet);
23+
if (globalStyleSheet) {
24+
if (supportsMutableAdoptedStyleSheets) {
25+
shadowRoot.adoptedStyleSheets.push(globalStyleSheet);
26+
} else {
27+
shadowRoot.adoptedStyleSheets = [...shadowRoot.adoptedStyleSheets, globalStyleSheet];
28+
}
29+
}
2330
}

0 commit comments

Comments
 (0)