@@ -7,26 +7,11 @@ function init() {
77 return events ;
88 }
99
10- function getFullPathToElement ( element ) {
11- const path = [ ] ;
12- while ( element . tagName !== 'HTML' ) {
13- let selector = element . tagName ;
14- if ( element . id ) {
15- selector += '#' + element . id ;
16- path . unshift ( selector ) ;
17- break ;
18- } else {
19- let sib = element , nth = 1 ;
20- while ( sib = sib . previousElementSibling ) {
21- if ( sib . tagName === element . tagName ) nth ++ ;
22- }
23- if ( nth != 1 ) selector += `:nth-of-type(${ nth } )` ;
24- }
25- path . unshift ( selector ) ;
26- element = element . parentElement ;
27- }
28- path . unshift ( 'HTML' ) ;
29- return path . join ( '>' ) ;
10+ function generateUUID ( prefix = "" ) {
11+ return ( prefix ? prefix + "-" : "" ) + 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' . replace ( / [ x y ] / g, function ( c ) {
12+ const r = Math . random ( ) * 16 | 0 , v = c === 'x' ? r : ( r & 0x3 | 0x8 ) ;
13+ return v . toString ( 16 ) ;
14+ } ) ;
3015 }
3116
3217 function main ( ) {
@@ -83,9 +68,16 @@ function init() {
8368
8469 if ( _entry . join ( ',' ) === 'events,query,classes' ) {
8570 const events = split [ entry . events ] . split ( ',' ) ;
86- const query = split [ entry . query ] . replaceAll ( / c u r r e n t \s * , ? / g, getFullPathToElement ( element ) + ',' ) ;
71+ let query = split [ entry . query ] . replaceAll ( / c u r r e n t \s * , ? / g, '#' + uuid + ',' ) ;
8772 const classes = Array . from ( split [ entry . classes ] . slice ( 1 , - 1 ) . matchAll ( regularExpressions [ 0 ] ) ) . map ( e => e . slice ( 1 ) ) ;
73+ if ( query . startsWith ( '>' ) ) {
74+ query = '#' + uuid + query ;
75+ }
76+ if ( query . endsWith ( ',' ) ) {
77+ query = query . slice ( 0 , - 1 ) ;
78+ }
8879 const elements = document . querySelectorAll ( query ) ;
80+ element . removeAttribute ( 'id' ) ;
8981 for ( const element of elements ) {
9082 for ( const event of events ) {
9183 if ( event === 'hover' ) {
@@ -115,17 +107,18 @@ function init() {
115107 }
116108
117109 if ( _entry . join ( ',' ) === 'query,classes' ) {
118- let query = split [ entry . query ] . replaceAll ( / c u r r e n t \s * , ? / g, getFullPathToElement ( element ) + ',' ) ;
110+ const uuid = generateUUID ( 'cijih' ) ;
111+ element . setAttribute ( 'id' , uuid )
112+ let query = split [ entry . query ] . replaceAll ( / c u r r e n t \s * , ? / g, '#' + uuid + ',' ) ;
119113 const classes = Array . from ( split [ entry . classes ] . slice ( 1 , - 1 ) . matchAll ( regularExpressions [ 0 ] ) ) . map ( e => e . slice ( 1 ) ) ;
120114 if ( query . startsWith ( '>' ) ) {
121- console . log ( 'before : ' , query ) ;
122- query = getFullPathToElement ( element ) + query ;
123- console . log ( 'after : ' , query ) ;
115+ query = '#' + uuid + query ;
124116 }
125117 if ( query . endsWith ( ',' ) ) {
126118 query = query . slice ( 0 , - 1 ) ;
127119 }
128120 const elements = document . querySelectorAll ( query ) ;
121+ element . removeAttribute ( 'id' ) ;
129122 for ( const element of elements ) {
130123 for ( const [ property , value ] of classes ) {
131124 element . style . setProperty ( property , value ) ;
@@ -164,22 +157,20 @@ function init() {
164157let observer ;
165158window . addEventListener ( 'css-in-js-in-html-ready' , function ( ) {
166159 if ( ! observer ) {
167- observer = new MutationObserver ( ( mutationsList ) => {
160+ observer = new MutationObserver ( function ( mutationsList ) {
168161 for ( const mutation of mutationsList ) {
169- if ( mutation . type === 'attributes' && mutation . attributeName === 'class' ) {
170- init ( ) ;
171- break ;
172- }
173- if ( mutation . type === 'childList' && ( mutation . addedNodes . length || mutation . removedNodes . length ) ) {
162+ if (
163+ ( mutation . type === 'childList' && mutation . addedNodes . length > 0 ) ||
164+ ( mutation . type === 'attributes' && mutation . attributeName === 'class' )
165+ ) {
174166 init ( ) ;
175- break ;
176167 }
177168 }
178169 } ) ;
179170
180171 observer . observe ( document . body , {
181- attributes : true ,
182172 childList : true ,
173+ attributes : true ,
183174 subtree : true ,
184175 attributeFilter : [ 'class' ]
185176 } ) ;
0 commit comments