4242--------------------------------------------------------------*/
4343
4444var EXT = {
45- url : location . href ,
45+ url : location . hostname ,
46+ styles : [ ] ,
4647 elements : { } ,
4748 values : { } ,
4849 storage : { } ,
@@ -840,6 +841,7 @@ function elementStyle(node) {
840841 ) {
841842 if ( node . sheet ) {
842843 EXT . threads ++ ;
844+ EXT . child ( 'added' , node ) ;
843845
844846 createStyle ( parseRules ( node . sheet . cssRules ) , node . parentNode ) ;
845847 }
@@ -868,6 +870,8 @@ function parseMutations(mutationList) {
868870 if ( node . nodeName === 'LINK' ) {
869871 if ( node . rel === 'stylesheet' ) {
870872 if ( ready && getGlobalOrLocale ( ) [ 'dynamic-theme' ] ) {
873+ EXT . child ( 'added' , node ) ;
874+
871875 elementLink ( node ) ;
872876 }
873877 }
@@ -877,6 +881,12 @@ function parseMutations(mutationList) {
877881 }
878882 }
879883 }
884+
885+ for ( var j = 0 , k = mutation . removedNodes . length ; j < k ; j ++ ) {
886+ var node = mutation . removedNodes [ j ] ;
887+
888+ EXT . child ( 'removed' , node ) ;
889+ }
880890 } else if ( mutation . type === 'attributes' ) {
881891 if ( mutation . attributeName === 'style' ) {
882892 if ( ready && getGlobalOrLocale ( ) [ 'dynamic-theme' ] ) {
@@ -949,14 +959,9 @@ function parseRules(rules, parent, url) {
949959 if ( EXT . threads === 0 && EXT . ready === false ) {
950960 EXT . ready = true ;
951961
952- //queryLinks();
953- //queryStyles();
954-
955962 document . documentElement . classList . add ( 'dark-mode--ready' ) ;
956963 }
957964
958- queryInlines ( ) ;
959-
960965 if ( url ) {
961966 string = string . replace ( EXT . regex . url , function ( match ) {
962967 var result = match . replace ( / u r l \( [ " ' ] ? / , '' ) . replace ( / [ " ' ] ? \) $ / , '' ) ;
@@ -1280,4 +1285,36 @@ EXT.observer.observe(document, {
12801285 ] ,
12811286 childList : true ,
12821287 subtree : true
1283- } ) ;
1288+ } ) ;
1289+
1290+
1291+ /*--------------------------------------------------------------
1292+ # HANDLERS
1293+ --------------------------------------------------------------*/
1294+
1295+ /*--------------------------------------------------------------
1296+ # CHILD
1297+ --------------------------------------------------------------*/
1298+
1299+ EXT . child = function ( type , element ) {
1300+ var children = element . children ,
1301+ index = EXT . styles . indexOf ( element ) ;
1302+
1303+ if ( type === 'added' ) {
1304+ if ( index === - 1 ) {
1305+ EXT . styles . push ( element ) ;
1306+ }
1307+ } else if ( type === 'removed' ) {
1308+ if ( index !== - 1 ) {
1309+ EXT . styles . splice ( index , 1 ) ;
1310+ }
1311+ }
1312+
1313+ if ( children ) {
1314+ for ( var i = 0 , l = children . length ; i < l ; i ++ ) {
1315+ var child = children [ i ] ;
1316+
1317+ EXT . child ( type , child ) ;
1318+ }
1319+ }
1320+ } ;
0 commit comments