8
8
*/
9
9
10
10
import {
11
- getPropertyInfo ,
12
- isAttributeNameSafe ,
13
11
BOOLEAN ,
14
12
OVERLOADED_BOOLEAN ,
15
13
NUMERIC ,
16
14
POSITIVE_NUMERIC ,
17
15
} from '../shared/DOMProperty' ;
16
+
17
+ import isAttributeNameSafe from '../shared/isAttributeNameSafe' ;
18
18
import sanitizeURL from '../shared/sanitizeURL' ;
19
19
import {
20
20
enableTrustedTypesIntegration ,
@@ -38,11 +38,6 @@ export function getValueForProperty(
38
38
propertyInfo : PropertyInfo ,
39
39
) : mixed {
40
40
if ( __DEV__ ) {
41
- if ( propertyInfo . mustUseProperty ) {
42
- const { propertyName} = propertyInfo ;
43
- return ( node : any ) [ propertyName ] ;
44
- }
45
-
46
41
const attributeName = propertyInfo . attributeName ;
47
42
48
43
if ( ! node . hasAttribute ( attributeName ) ) {
@@ -287,152 +282,137 @@ export function getValueForAttributeOnCustomComponent(
287
282
* @param {string } name
288
283
* @param {* } value
289
284
*/
290
- export function setValueForProperty ( node : Element , name : string , value : mixed ) {
291
- if (
292
- // shouldIgnoreAttribute
293
- // We have already filtered out reserved words.
294
- name . length > 2 &&
295
- ( name [ 0 ] === 'o' || name [ 0 ] === 'O' ) &&
296
- ( name [ 1 ] === 'n' || name [ 1 ] === 'N' )
297
- ) {
285
+ export function setValueForProperty (
286
+ node : Element ,
287
+ propertyInfo : PropertyInfo ,
288
+ value : mixed ,
289
+ ) {
290
+ const attributeName = propertyInfo . attributeName ;
291
+
292
+ if ( value === null ) {
293
+ node . removeAttribute ( attributeName ) ;
298
294
return ;
299
295
}
300
296
301
- const propertyInfo = getPropertyInfo ( name ) ;
302
- if ( propertyInfo !== null ) {
303
- if ( propertyInfo . mustUseProperty ) {
304
- // We assume mustUseProperty are of BOOLEAN type because that's the only way we use it
305
- // right now.
306
- ( node : any ) [ propertyInfo . propertyName ] =
307
- value && typeof value !== 'function' && typeof value !== 'symbol' ;
297
+ // shouldRemoveAttribute
298
+ switch ( typeof value ) {
299
+ case 'undefined' :
300
+ case 'function' :
301
+ case 'symbol' : // eslint-disable-line
302
+ node . removeAttribute ( attributeName ) ;
308
303
return ;
304
+ case 'boolean' : {
305
+ if ( ! propertyInfo . acceptsBooleans ) {
306
+ node . removeAttribute ( attributeName ) ;
307
+ return ;
308
+ }
309
309
}
310
-
311
- // The rest are treated as attributes with special cases.
312
-
313
- const attributeName = propertyInfo . attributeName ;
314
-
315
- if ( value === null ) {
310
+ }
311
+ if ( enableFilterEmptyStringAttributesDOM ) {
312
+ if ( propertyInfo . removeEmptyString && value === '' ) {
313
+ if ( __DEV__ ) {
314
+ if ( attributeName === 'src' ) {
315
+ console . error (
316
+ 'An empty string ("") was passed to the %s attribute. ' +
317
+ 'This may cause the browser to download the whole page again over the network. ' +
318
+ 'To fix this, either do not render the element at all ' +
319
+ 'or pass null to %s instead of an empty string.' ,
320
+ attributeName ,
321
+ attributeName ,
322
+ ) ;
323
+ } else {
324
+ console . error (
325
+ 'An empty string ("") was passed to the %s attribute. ' +
326
+ 'To fix this, either do not render the element at all ' +
327
+ 'or pass null to %s instead of an empty string.' ,
328
+ attributeName ,
329
+ attributeName ,
330
+ ) ;
331
+ }
332
+ }
316
333
node . removeAttribute ( attributeName ) ;
317
334
return ;
318
335
}
336
+ }
319
337
320
- // shouldRemoveAttribute
321
- switch ( typeof value ) {
322
- case 'undefined' :
323
- case 'function' :
324
- case 'symbol' : // eslint-disable-line
338
+ switch ( propertyInfo . type ) {
339
+ case BOOLEAN :
340
+ if ( value ) {
341
+ node . setAttribute ( attributeName , '' ) ;
342
+ } else {
325
343
node . removeAttribute ( attributeName ) ;
326
344
return ;
327
- case 'boolean' : {
328
- if ( ! propertyInfo . acceptsBooleans ) {
329
- node . removeAttribute ( attributeName ) ;
330
- return ;
345
+ }
346
+ break ;
347
+ case OVERLOADED_BOOLEAN :
348
+ if ( value === true ) {
349
+ node . setAttribute ( attributeName , '' ) ;
350
+ } else if ( value === false ) {
351
+ node . removeAttribute ( attributeName ) ;
352
+ } else {
353
+ if ( __DEV__ ) {
354
+ checkAttributeStringCoercion ( value , attributeName ) ;
331
355
}
356
+ node . setAttribute ( attributeName , ( value : any ) ) ;
332
357
}
333
- }
334
- if ( enableFilterEmptyStringAttributesDOM ) {
335
- if ( propertyInfo . removeEmptyString && value === '' ) {
358
+ return ;
359
+ case NUMERIC :
360
+ if ( ! isNaN ( value ) ) {
336
361
if ( __DEV__ ) {
337
- if ( name === 'src' ) {
338
- console . error (
339
- 'An empty string ("") was passed to the %s attribute. ' +
340
- 'This may cause the browser to download the whole page again over the network. ' +
341
- 'To fix this, either do not render the element at all ' +
342
- 'or pass null to %s instead of an empty string.' ,
343
- name ,
344
- name ,
345
- ) ;
346
- } else {
347
- console . error (
348
- 'An empty string ("") was passed to the %s attribute. ' +
349
- 'To fix this, either do not render the element at all ' +
350
- 'or pass null to %s instead of an empty string.' ,
351
- name ,
352
- name ,
353
- ) ;
354
- }
362
+ checkAttributeStringCoercion ( value , attributeName ) ;
355
363
}
364
+ node . setAttribute ( attributeName , ( value : any ) ) ;
365
+ } else {
356
366
node . removeAttribute ( attributeName ) ;
357
- return ;
358
367
}
359
- }
360
-
361
- switch ( propertyInfo . type ) {
362
- case BOOLEAN :
363
- if ( value ) {
364
- node . setAttribute ( attributeName , '' ) ;
365
- } else {
366
- node . removeAttribute ( attributeName ) ;
367
- return ;
368
- }
369
- break ;
370
- case OVERLOADED_BOOLEAN :
371
- if ( value === true ) {
372
- node . setAttribute ( attributeName , '' ) ;
373
- } else if ( value === false ) {
374
- node . removeAttribute ( attributeName ) ;
375
- } else {
376
- if ( __DEV__ ) {
377
- checkAttributeStringCoercion ( value , attributeName ) ;
378
- }
379
- node . setAttribute ( attributeName , ( value : any ) ) ;
380
- }
381
- return ;
382
- case NUMERIC :
383
- if ( ! isNaN ( value ) ) {
384
- if ( __DEV__ ) {
385
- checkAttributeStringCoercion ( value , attributeName ) ;
386
- }
387
- node . setAttribute ( attributeName , ( value : any ) ) ;
388
- } else {
389
- node . removeAttribute ( attributeName ) ;
390
- }
391
- break ;
392
- case POSITIVE_NUMERIC :
393
- if ( ! isNaN ( value ) && ( value : any ) >= 1 ) {
394
- if ( __DEV__ ) {
395
- checkAttributeStringCoercion ( value , attributeName ) ;
396
- }
397
- node . setAttribute ( attributeName , ( value : any ) ) ;
398
- } else {
399
- node . removeAttribute ( attributeName ) ;
400
- }
401
- break ;
402
- default : {
368
+ break ;
369
+ case POSITIVE_NUMERIC :
370
+ if ( ! isNaN ( value ) && ( value : any ) >= 1 ) {
403
371
if ( __DEV__ ) {
404
372
checkAttributeStringCoercion ( value , attributeName ) ;
405
373
}
406
- let attributeValue ;
407
- // `setAttribute` with objects becomes only `[object]` in IE8/9,
408
- // ('' + value) makes it output the correct toString()-value.
409
- if ( enableTrustedTypesIntegration ) {
410
- if ( propertyInfo . sanitizeURL ) {
411
- attributeValue = ( sanitizeURL ( value ) : any ) ;
412
- } else {
413
- attributeValue = ( value : any ) ;
414
- }
374
+ node . setAttribute ( attributeName , ( value : any ) ) ;
375
+ } else {
376
+ node . removeAttribute ( attributeName ) ;
377
+ }
378
+ break ;
379
+ default : {
380
+ if ( __DEV__ ) {
381
+ checkAttributeStringCoercion ( value , attributeName ) ;
382
+ }
383
+ let attributeValue ;
384
+ // `setAttribute` with objects becomes only `[object]` in IE8/9,
385
+ // ('' + value) makes it output the correct toString()-value.
386
+ if ( enableTrustedTypesIntegration ) {
387
+ if ( propertyInfo . sanitizeURL ) {
388
+ attributeValue = ( sanitizeURL ( value ) : any ) ;
415
389
} else {
416
- // We have already verified this above.
417
- // eslint-disable-next-line react-internal/safe-string-coercion
418
- attributeValue = '' + ( value : any ) ;
419
- if ( propertyInfo . sanitizeURL ) {
420
- attributeValue = sanitizeURL ( attributeValue ) ;
421
- }
390
+ attributeValue = ( value : any ) ;
422
391
}
423
- const attributeNamespace = propertyInfo . attributeNamespace ;
424
- if ( attributeNamespace ) {
425
- node . setAttributeNS (
426
- attributeNamespace ,
427
- attributeName ,
428
- attributeValue ,
429
- ) ;
430
- } else {
431
- node . setAttribute ( attributeName , attributeValue ) ;
392
+ } else {
393
+ // We have already verified this above.
394
+ // eslint-disable-next-line react-internal/safe-string-coercion
395
+ attributeValue = '' + ( value : any ) ;
396
+ if ( propertyInfo . sanitizeURL ) {
397
+ attributeValue = sanitizeURL ( attributeValue ) ;
432
398
}
433
399
}
400
+ const attributeNamespace = propertyInfo . attributeNamespace ;
401
+ if ( attributeNamespace ) {
402
+ node . setAttributeNS ( attributeNamespace , attributeName , attributeValue ) ;
403
+ } else {
404
+ node . setAttribute ( attributeName , attributeValue ) ;
405
+ }
434
406
}
435
- } else if ( isAttributeNameSafe ( name ) ) {
407
+ }
408
+ }
409
+
410
+ export function setValueForAttribute (
411
+ node : Element ,
412
+ name : string ,
413
+ value : mixed ,
414
+ ) {
415
+ if ( isAttributeNameSafe ( name ) ) {
436
416
// If the prop isn't in the special list, treat it as a simple attribute.
437
417
// shouldRemoveAttribute
438
418
if ( value === null ) {
0 commit comments