@@ -127,17 +127,8 @@ export function setClass(el: TargetElement, value: any): void {
127
127
( __DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__ ) &&
128
128
isHydrating
129
129
) {
130
- const actual = el . getAttribute ( 'class' )
131
- const actualClassSet = toClassSet ( actual || '' )
132
130
const expected = normalizeClass ( value )
133
- const expectedClassSet = toClassSet ( expected )
134
- if ( ! isSetEqual ( actualClassSet , expectedClassSet ) ) {
135
- warnPropMismatch ( el , 'class' , MismatchTypes . CLASS , actual , expected )
136
- logMismatchError ( )
137
- el . className = expected
138
- }
139
-
140
- el . $cls = expected
131
+ handleClassHydration ( el , value , expected , false , '$cls' )
141
132
return
142
133
}
143
134
@@ -151,26 +142,8 @@ function setClassIncremental(el: any, value: any): void {
151
142
const cacheKey = `$clsi${ isApplyingFallthroughProps ? '$' : '' } `
152
143
153
144
if ( ( __DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__ ) && isHydrating ) {
154
- const actual = el . getAttribute ( 'class' )
155
- const actualClassSet = toClassSet ( actual || '' )
156
145
const expected = normalizeClass ( value )
157
- const expectedClassSet = toClassSet ( expected )
158
- // check if the expected classes are present in the actual classes
159
- const hasMismatch = Array . from ( expectedClassSet ) . some (
160
- cls => ! actualClassSet . has ( cls ) ,
161
- )
162
- if ( hasMismatch ) {
163
- warnPropMismatch ( el , 'class' , MismatchTypes . CLASS , actual , expected )
164
- logMismatchError ( )
165
-
166
- const nextList = value . split ( / \s + / )
167
- if ( value ) {
168
- el . classList . add ( ...nextList )
169
- }
170
- } else {
171
- el [ cacheKey ] = expected
172
- }
173
-
146
+ handleClassHydration ( el , value , expected , true , cacheKey )
174
147
return
175
148
}
176
149
@@ -196,24 +169,8 @@ export function setStyle(el: TargetElement, value: any): void {
196
169
( __DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__ ) &&
197
170
isHydrating
198
171
) {
199
- const actual = el . getAttribute ( 'style' )
200
- const actualStyleMap = toStyleMap ( actual || '' )
201
172
const normalizedValue = normalizeStyle ( value )
202
- const expected = stringifyStyle ( normalizedValue )
203
- const expectedStyleMap = toStyleMap ( expected )
204
-
205
- // If `v-show=false`, `display: 'none'` should be added to expected
206
- if ( ( el as any ) [ vShowHidden ] ) {
207
- expectedStyleMap . set ( 'display' , 'none' )
208
- }
209
-
210
- // TODO: handle css vars
211
-
212
- if ( ! isMapEqual ( actualStyleMap , expectedStyleMap ) ) {
213
- warnPropMismatch ( el , 'style' , MismatchTypes . STYLE , actual , expected )
214
- logMismatchError ( )
215
- patchStyle ( el , el . $sty , ( el . $sty = normalizedValue ) )
216
- }
173
+ handleStyleHydration ( el , value , normalizedValue , false , '$sty' )
217
174
return
218
175
}
219
176
@@ -228,27 +185,7 @@ function setStyleIncremental(el: any, value: any): NormalizedStyle | undefined {
228
185
: ( normalizeStyle ( value ) as NormalizedStyle | undefined )
229
186
230
187
if ( ( __DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__ ) && isHydrating ) {
231
- const actual = el . getAttribute ( 'style' )
232
- const actualStyleMap = toStyleMap ( actual || '' )
233
- const expected = isString ( value ) ? value : stringifyStyle ( normalizedValue )
234
- const expectedStyleMap = toStyleMap ( expected )
235
-
236
- // If `v-show=false`, `display: 'none'` should be added to expected
237
- if ( el [ vShowHidden ] ) {
238
- expectedStyleMap . set ( 'display' , 'none' )
239
- }
240
-
241
- // TODO: handle css vars
242
-
243
- // check if the expected styles are present in the actual styles
244
- const hasMismatch = Array . from ( expectedStyleMap . entries ( ) ) . some (
245
- ( [ key , val ] ) => actualStyleMap . get ( key ) !== val ,
246
- )
247
- if ( hasMismatch ) {
248
- warnPropMismatch ( el , 'style' , MismatchTypes . STYLE , actual , expected )
249
- logMismatchError ( )
250
- patchStyle ( el , el [ cacheKey ] , ( el [ cacheKey ] = normalizedValue ) )
251
- }
188
+ handleStyleHydration ( el , value , normalizedValue , true , cacheKey )
252
189
return
253
190
}
254
191
@@ -446,3 +383,71 @@ export function optimizePropertyLookup(): void {
446
383
( Text . prototype as any ) . $txt =
447
384
''
448
385
}
386
+
387
+ function handleClassHydration (
388
+ el : TargetElement | any ,
389
+ value : any ,
390
+ expected : string ,
391
+ isIncremental : boolean ,
392
+ cacheKey : string ,
393
+ ) {
394
+ const actual = el . getAttribute ( 'class' )
395
+ const actualClassSet = toClassSet ( actual || '' )
396
+ const expectedClassSet = toClassSet ( expected )
397
+
398
+ const hasMismatch = isIncremental
399
+ ? // check if the expected classes are present in the actual classes
400
+ Array . from ( expectedClassSet ) . some ( cls => ! actualClassSet . has ( cls ) )
401
+ : ! isSetEqual ( actualClassSet , expectedClassSet )
402
+
403
+ if ( hasMismatch ) {
404
+ warnPropMismatch ( el , 'class' , MismatchTypes . CLASS , actual , expected )
405
+ logMismatchError ( )
406
+
407
+ if ( isIncremental ) {
408
+ const nextList = value . split ( / \s + / )
409
+ if ( value ) {
410
+ el . classList . add ( ...nextList )
411
+ }
412
+ } else {
413
+ el . className = expected
414
+ }
415
+ }
416
+
417
+ el [ cacheKey ] = expected
418
+ }
419
+
420
+ function handleStyleHydration (
421
+ el : TargetElement | any ,
422
+ value : any ,
423
+ normalizedValue : string | NormalizedStyle | undefined ,
424
+ isIncremental : boolean ,
425
+ cacheKey : string ,
426
+ ) {
427
+ const actual = el . getAttribute ( 'style' )
428
+ const actualStyleMap = toStyleMap ( actual || '' )
429
+ const expected = isString ( value ) ? value : stringifyStyle ( normalizedValue )
430
+ const expectedStyleMap = toStyleMap ( expected )
431
+
432
+ // If `v-show=false`, `display: 'none'` should be added to expected
433
+ if ( el [ vShowHidden ] ) {
434
+ expectedStyleMap . set ( 'display' , 'none' )
435
+ }
436
+
437
+ // TODO: handle css vars
438
+
439
+ const hasMismatch = isIncremental
440
+ ? // check if the expected styles are present in the actual styles
441
+ Array . from ( expectedStyleMap . entries ( ) ) . some (
442
+ ( [ key , val ] ) => actualStyleMap . get ( key ) !== val ,
443
+ )
444
+ : ! isMapEqual ( actualStyleMap , expectedStyleMap )
445
+
446
+ if ( hasMismatch ) {
447
+ warnPropMismatch ( el , 'style' , MismatchTypes . STYLE , actual , expected )
448
+ logMismatchError ( )
449
+ patchStyle ( el , el [ cacheKey ] , ( el [ cacheKey ] = normalizedValue ) )
450
+ }
451
+
452
+ el [ cacheKey ] = normalizedValue
453
+ }
0 commit comments