@@ -7,26 +7,11 @@ function init() {
7
7
return events ;
8
8
}
9
9
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
+ } ) ;
30
15
}
31
16
32
17
function main ( ) {
@@ -83,9 +68,16 @@ function init() {
83
68
84
69
if ( _entry . join ( ',' ) === 'events,query,classes' ) {
85
70
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 + ',' ) ;
87
72
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
+ }
88
79
const elements = document . querySelectorAll ( query ) ;
80
+ element . removeAttribute ( 'id' ) ;
89
81
for ( const element of elements ) {
90
82
for ( const event of events ) {
91
83
if ( event === 'hover' ) {
@@ -115,17 +107,18 @@ function init() {
115
107
}
116
108
117
109
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 + ',' ) ;
119
113
const classes = Array . from ( split [ entry . classes ] . slice ( 1 , - 1 ) . matchAll ( regularExpressions [ 0 ] ) ) . map ( e => e . slice ( 1 ) ) ;
120
114
if ( query . startsWith ( '>' ) ) {
121
- console . log ( 'before : ' , query ) ;
122
- query = getFullPathToElement ( element ) + query ;
123
- console . log ( 'after : ' , query ) ;
115
+ query = '#' + uuid + query ;
124
116
}
125
117
if ( query . endsWith ( ',' ) ) {
126
118
query = query . slice ( 0 , - 1 ) ;
127
119
}
128
120
const elements = document . querySelectorAll ( query ) ;
121
+ element . removeAttribute ( 'id' ) ;
129
122
for ( const element of elements ) {
130
123
for ( const [ property , value ] of classes ) {
131
124
element . style . setProperty ( property , value ) ;
@@ -164,22 +157,20 @@ function init() {
164
157
let observer ;
165
158
window . addEventListener ( 'css-in-js-in-html-ready' , function ( ) {
166
159
if ( ! observer ) {
167
- observer = new MutationObserver ( ( mutationsList ) => {
160
+ observer = new MutationObserver ( function ( mutationsList ) {
168
161
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
+ ) {
174
166
init ( ) ;
175
- break ;
176
167
}
177
168
}
178
169
} ) ;
179
170
180
171
observer . observe ( document . body , {
181
- attributes : true ,
182
172
childList : true ,
173
+ attributes : true ,
183
174
subtree : true ,
184
175
attributeFilter : [ 'class' ]
185
176
} ) ;
0 commit comments