@@ -7,11 +7,9 @@ import {
77 removeAdopterLocation ,
88 restyleAdopter ,
99} from './ConstructedStyleSheet' ;
10- import { hasShadyCss } from './shared' ;
10+ import { defineProperty , forEach , hasShadyCss } from './shared' ;
1111import {
12- defineProperty ,
1312 diff ,
14- forEach ,
1513 getShadowRoot ,
1614 isElementConnected ,
1715 removeNode ,
@@ -264,80 +262,78 @@ function Location(this: Location, element: Document | ShadowRoot) {
264262 ) ;
265263}
266264
267- const proto = Location . prototype ;
268-
269- proto . isConnected = function isConnected ( ) {
270- const element = $element . get ( this ) ! ;
271-
272- return element instanceof Document
273- ? element . readyState !== 'loading'
274- : isElementConnected ( element . host ) ;
275- } ;
276-
277- proto . connect = function connect ( ) {
278- const container = getAdopterContainer ( this ) ;
279-
280- $observer . get ( this ) ! . observe ( container , defaultObserverOptions ) ;
281-
282- if ( $uniqueSheets . get ( this ) ! . length > 0 ) {
283- adopt ( this ) ;
284- }
285-
286- traverseWebComponents ( container , ( root ) => {
287- getAssociatedLocation ( root ) . connect ( ) ;
288- } ) ;
289- } ;
290-
291- proto . disconnect = function disconnect ( ) {
292- $observer . get ( this ) ! . disconnect ( ) ;
293- } ;
294-
295- proto . update = function update ( sheets : readonly ConstructedStyleSheet [ ] ) {
296- const self = this ;
297- const locationType =
298- $element . get ( self ) === document ? 'Document' : 'ShadowRoot' ;
299-
300- if ( ! Array . isArray ( sheets ) ) {
301- // document.adoptedStyleSheets = new CSSStyleSheet();
302- throw new TypeError (
303- `Failed to set the 'adoptedStyleSheets' property on ${ locationType } : Iterator getter is not callable.` ,
304- ) ;
305- }
306-
307- if ( ! sheets . every ( isCSSStyleSheetInstance ) ) {
308- // document.adoptedStyleSheets = ['non-CSSStyleSheet value'];
309- throw new TypeError (
310- `Failed to set the 'adoptedStyleSheets' property on ${ locationType } : Failed to convert value to 'CSSStyleSheet'` ,
311- ) ;
312- }
313-
314- if ( sheets . some ( isNonConstructedStyleSheetInstance ) ) {
315- // document.adoptedStyleSheets = [document.styleSheets[0]];
316- throw new TypeError (
317- `Failed to set the 'adoptedStyleSheets' property on ${ locationType } : Can't adopt non-constructed stylesheets` ,
318- ) ;
319- }
320-
321- self . sheets = sheets ;
322- const oldUniqueSheets = $uniqueSheets . get ( self ) ! ;
323- const uniqueSheets = unique ( sheets ) ;
324-
325- // Style sheets that existed in the old sheet list but was excluded in the
326- // new one.
327- const removedSheets = diff ( oldUniqueSheets , uniqueSheets ) ;
328-
329- removedSheets . forEach ( ( sheet ) => {
330- // Type Note: any removed sheet is already initialized, so there cannot be
331- // missing adopter for this location.
332- removeNode ( getAdopterByLocation ( sheet , self ) ! ) ;
333- removeAdopterLocation ( sheet , self ) ;
334- } ) ;
335-
336- $uniqueSheets . set ( self , uniqueSheets ) ;
337-
338- if ( self . isConnected ( ) && uniqueSheets . length > 0 ) {
339- adopt ( self ) ;
340- }
265+ // @ts -expect-error: too generic for TypeScript
266+ Location . prototype = {
267+ isConnected ( ) {
268+ const element = $element . get ( this ) ! ;
269+
270+ return element instanceof Document
271+ ? element . readyState !== 'loading'
272+ : isElementConnected ( element . host ) ;
273+ } ,
274+ connect ( ) {
275+ const container = getAdopterContainer ( this ) ;
276+
277+ $observer . get ( this ) ! . observe ( container , defaultObserverOptions ) ;
278+
279+ if ( $uniqueSheets . get ( this ) ! . length > 0 ) {
280+ adopt ( this ) ;
281+ }
282+
283+ traverseWebComponents ( container , ( root ) => {
284+ getAssociatedLocation ( root ) . connect ( ) ;
285+ } ) ;
286+ } ,
287+ disconnect ( ) {
288+ $observer . get ( this ) ! . disconnect ( ) ;
289+ } ,
290+ update ( sheets : readonly ConstructedStyleSheet [ ] ) {
291+ const self = this ;
292+ const locationType =
293+ $element . get ( self ) === document ? 'Document' : 'ShadowRoot' ;
294+
295+ if ( ! Array . isArray ( sheets ) ) {
296+ // document.adoptedStyleSheets = new CSSStyleSheet();
297+ throw new TypeError (
298+ `Failed to set the 'adoptedStyleSheets' property on ${ locationType } : Iterator getter is not callable.` ,
299+ ) ;
300+ }
301+
302+ if ( ! sheets . every ( isCSSStyleSheetInstance ) ) {
303+ // document.adoptedStyleSheets = ['non-CSSStyleSheet value'];
304+ throw new TypeError (
305+ `Failed to set the 'adoptedStyleSheets' property on ${ locationType } : Failed to convert value to 'CSSStyleSheet'` ,
306+ ) ;
307+ }
308+
309+ if ( sheets . some ( isNonConstructedStyleSheetInstance ) ) {
310+ // document.adoptedStyleSheets = [document.styleSheets[0]];
311+ throw new TypeError (
312+ `Failed to set the 'adoptedStyleSheets' property on ${ locationType } : Can't adopt non-constructed stylesheets` ,
313+ ) ;
314+ }
315+
316+ self . sheets = sheets ;
317+ const oldUniqueSheets = $uniqueSheets . get ( self ) ! ;
318+ const uniqueSheets = unique ( sheets ) ;
319+
320+ // Style sheets that existed in the old sheet list but was excluded in the
321+ // new one.
322+ const removedSheets = diff ( oldUniqueSheets , uniqueSheets ) ;
323+
324+ removedSheets . forEach ( ( sheet ) => {
325+ // Type Note: any removed sheet is already initialized, so there cannot be
326+ // missing adopter for this location.
327+ removeNode ( getAdopterByLocation ( sheet , self ) ! ) ;
328+ removeAdopterLocation ( sheet , self ) ;
329+ } ) ;
330+
331+ $uniqueSheets . set ( self , uniqueSheets ) ;
332+
333+ if ( self . isConnected ( ) && uniqueSheets . length > 0 ) {
334+ adopt ( self ) ;
335+ }
336+ } ,
341337} ;
342338
343339export default Location ;
0 commit comments