@@ -11,39 +11,13 @@ import {
1111 hasOwnProperty ,
1212 htmlPropertyToAttribute ,
1313 globalThis ,
14- isFunction ,
1514 isUndefined ,
16- isArray ,
1715 KEY__IS_NATIVE_SHADOW_ROOT_DEFINED ,
1816 KEY__SHADOW_TOKEN ,
1917 setPrototypeOf ,
2018 StringToLowerCase ,
21- getOwnPropertyDescriptor ,
2219} from '@lwc/shared' ;
23-
24- const globalStylesheets : { [ content : string ] : true } = create ( null ) ;
25-
26- if ( process . env . NODE_ENV === 'development' ) {
27- // @ts -ignore
28- window . __lwcResetGlobalStylesheets = ( ) => {
29- for ( const key of Object . keys ( globalStylesheets ) ) {
30- delete globalStylesheets [ key ] ;
31- }
32- } ;
33- }
34-
35- const globalStylesheetsParentElement : Element = document . head || document . body || document ;
36- // This check for constructable stylesheets is similar to Fast's:
37- // https://github.com/microsoft/fast/blob/d49d1ec/packages/web-components/fast-element/src/dom.ts#L51-L53
38- // See also: https://github.com/whatwg/webidl/issues/1027#issuecomment-934510070
39- const supportsConstructableStyleSheets =
40- isFunction ( CSSStyleSheet . prototype . replaceSync ) && isArray ( document . adoptedStyleSheets ) ;
41- const supportsMutableAdoptedStyleSheets =
42- supportsConstructableStyleSheets &&
43- getOwnPropertyDescriptor ( document . adoptedStyleSheets , 'length' ) ! . writable ;
44- const styleElements : { [ content : string ] : HTMLStyleElement } = create ( null ) ;
45- const styleSheets : { [ content : string ] : CSSStyleSheet } = create ( null ) ;
46- const shadowRootsToStyleSheets = new WeakMap < ShadowRoot , { [ content : string ] : true } > ( ) ;
20+ export { insertStylesheet } from './styles' ;
4721
4822export let getCustomElement : any ;
4923export let defineCustomElement : any ;
@@ -72,53 +46,6 @@ function isCustomElementRegistryAvailable() {
7246 }
7347}
7448
75- function insertConstructableStyleSheet ( content : string , target : ShadowRoot ) {
76- // It's important for CSSStyleSheets to be unique based on their content, so that
77- // `shadowRoot.adoptedStyleSheets.includes(sheet)` works.
78- let styleSheet = styleSheets [ content ] ;
79- if ( isUndefined ( styleSheet ) ) {
80- styleSheet = new CSSStyleSheet ( ) ;
81- styleSheet . replaceSync ( content ) ;
82- styleSheets [ content ] = styleSheet ;
83- }
84- const { adoptedStyleSheets } = target ;
85- if ( ! adoptedStyleSheets . includes ( styleSheet ) ) {
86- if ( supportsMutableAdoptedStyleSheets ) {
87- // This is only supported in later versions of Chromium:
88- // https://chromestatus.com/feature/5638996492288000
89- adoptedStyleSheets . push ( styleSheet ) ;
90- } else {
91- target . adoptedStyleSheets = [ ...adoptedStyleSheets , styleSheet ] ;
92- }
93- }
94- }
95-
96- function insertStyleElement ( content : string , target : ShadowRoot ) {
97- // Avoid inserting duplicate `<style>`s
98- let sheets = shadowRootsToStyleSheets . get ( target ) ;
99- if ( isUndefined ( sheets ) ) {
100- sheets = create ( null ) ;
101- shadowRootsToStyleSheets . set ( target , sheets ! ) ;
102- }
103- if ( sheets ! [ content ] ) {
104- return ;
105- }
106- sheets ! [ content ] = true ;
107-
108- // This `<style>` may be repeated multiple times in the DOM, so cache it. It's a bit
109- // faster to call `cloneNode()` on an existing node than to recreate it every time.
110- let elm = styleElements [ content ] ;
111- if ( isUndefined ( elm ) ) {
112- elm = document . createElement ( 'style' ) ;
113- elm . type = 'text/css' ;
114- elm . textContent = content ;
115- styleElements [ content ] = elm ;
116- } else {
117- elm = elm . cloneNode ( true ) as HTMLStyleElement ;
118- }
119- target . appendChild ( elm ) ;
120- }
121-
12249if ( isCustomElementRegistryAvailable ( ) ) {
12350 getCustomElement = customElements . get . bind ( customElements ) ;
12451 defineCustomElement = customElements . define . bind ( customElements ) ;
@@ -349,29 +276,6 @@ export function isConnected(node: Node): boolean {
349276 return node . isConnected ;
350277}
351278
352- export function insertGlobalStylesheet ( content : string ) : void {
353- if ( ! isUndefined ( globalStylesheets [ content ] ) ) {
354- return ;
355- }
356-
357- globalStylesheets [ content ] = true ;
358-
359- const elm = document . createElement ( 'style' ) ;
360- elm . type = 'text/css' ;
361- elm . textContent = content ;
362-
363- globalStylesheetsParentElement . appendChild ( elm ) ;
364- }
365-
366- export function insertStylesheet ( content : string , target : ShadowRoot ) : void {
367- if ( supportsConstructableStyleSheets ) {
368- insertConstructableStyleSheet ( content , target ) ;
369- } else {
370- // Fall back to <style> element
371- insertStyleElement ( content , target ) ;
372- }
373- }
374-
375279export function assertInstanceOfHTMLElement ( elm : any , msg : string ) {
376280 assert . invariant ( elm instanceof HTMLElement , msg ) ;
377281}
0 commit comments