1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  */
    
  7. 
    
  8. import {canUseDOM} from 'shared/ExecutionEnvironment';
    
  9. 
    
  10. /**
    
  11.  * Generate a mapping of standard vendor prefixes using the defined style property and event name.
    
  12.  *
    
  13.  * @param {string} styleProp
    
  14.  * @param {string} eventName
    
  15.  * @returns {object}
    
  16.  */
    
  17. function makePrefixMap(styleProp, eventName) {
    
  18.   const prefixes = {};
    
  19. 
    
  20.   prefixes[styleProp.toLowerCase()] = eventName.toLowerCase();
    
  21.   prefixes['Webkit' + styleProp] = 'webkit' + eventName;
    
  22.   prefixes['Moz' + styleProp] = 'moz' + eventName;
    
  23. 
    
  24.   return prefixes;
    
  25. }
    
  26. 
    
  27. /**
    
  28.  * A list of event names to a configurable list of vendor prefixes.
    
  29.  */
    
  30. const vendorPrefixes = {
    
  31.   animationend: makePrefixMap('Animation', 'AnimationEnd'),
    
  32.   animationiteration: makePrefixMap('Animation', 'AnimationIteration'),
    
  33.   animationstart: makePrefixMap('Animation', 'AnimationStart'),
    
  34.   transitionend: makePrefixMap('Transition', 'TransitionEnd'),
    
  35. };
    
  36. 
    
  37. /**
    
  38.  * Event names that have already been detected and prefixed (if applicable).
    
  39.  */
    
  40. const prefixedEventNames = {};
    
  41. 
    
  42. /**
    
  43.  * Element to check for prefixes on.
    
  44.  */
    
  45. let style = {};
    
  46. 
    
  47. /**
    
  48.  * Bootstrap if a DOM exists.
    
  49.  */
    
  50. if (canUseDOM) {
    
  51.   style = document.createElement('div').style;
    
  52. 
    
  53.   // On some platforms, in particular some releases of Android 4.x,
    
  54.   // the un-prefixed "animation" and "transition" properties are defined on the
    
  55.   // style object but the events that fire will still be prefixed, so we need
    
  56.   // to check if the un-prefixed events are usable, and if not remove them from the map.
    
  57.   if (!('AnimationEvent' in window)) {
    
  58.     delete vendorPrefixes.animationend.animation;
    
  59.     delete vendorPrefixes.animationiteration.animation;
    
  60.     delete vendorPrefixes.animationstart.animation;
    
  61.   }
    
  62. 
    
  63.   // Same as above
    
  64.   if (!('TransitionEvent' in window)) {
    
  65.     delete vendorPrefixes.transitionend.transition;
    
  66.   }
    
  67. }
    
  68. 
    
  69. /**
    
  70.  * Attempts to determine the correct vendor prefixed event name.
    
  71.  *
    
  72.  * @param {string} eventName
    
  73.  * @returns {string}
    
  74.  */
    
  75. function getVendorPrefixedEventName(eventName) {
    
  76.   if (prefixedEventNames[eventName]) {
    
  77.     return prefixedEventNames[eventName];
    
  78.   } else if (!vendorPrefixes[eventName]) {
    
  79.     return eventName;
    
  80.   }
    
  81. 
    
  82.   const prefixMap = vendorPrefixes[eventName];
    
  83. 
    
  84.   for (const styleProp in prefixMap) {
    
  85.     if (prefixMap.hasOwnProperty(styleProp) && styleProp in style) {
    
  86.       return (prefixedEventNames[eventName] = prefixMap[styleProp]);
    
  87.     }
    
  88.   }
    
  89. 
    
  90.   return eventName;
    
  91. }
    
  92. 
    
  93. export default getVendorPrefixedEventName;