UNPKG

2.75 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2013-present, Facebook, Inc.
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
9'use strict';
10
11var ExecutionEnvironment = require('fbjs/lib/ExecutionEnvironment');
12
13/**
14 * Generate a mapping of standard vendor prefixes using the defined style property and event name.
15 *
16 * @param {string} styleProp
17 * @param {string} eventName
18 * @returns {object}
19 */
20function makePrefixMap(styleProp, eventName) {
21 var prefixes = {};
22
23 prefixes[styleProp.toLowerCase()] = eventName.toLowerCase();
24 prefixes['Webkit' + styleProp] = 'webkit' + eventName;
25 prefixes['Moz' + styleProp] = 'moz' + eventName;
26 prefixes['ms' + styleProp] = 'MS' + eventName;
27 prefixes['O' + styleProp] = 'o' + eventName.toLowerCase();
28
29 return prefixes;
30}
31
32/**
33 * A list of event names to a configurable list of vendor prefixes.
34 */
35var vendorPrefixes = {
36 animationend: makePrefixMap('Animation', 'AnimationEnd'),
37 animationiteration: makePrefixMap('Animation', 'AnimationIteration'),
38 animationstart: makePrefixMap('Animation', 'AnimationStart'),
39 transitionend: makePrefixMap('Transition', 'TransitionEnd')
40};
41
42/**
43 * Event names that have already been detected and prefixed (if applicable).
44 */
45var prefixedEventNames = {};
46
47/**
48 * Element to check for prefixes on.
49 */
50var style = {};
51
52/**
53 * Bootstrap if a DOM exists.
54 */
55if (ExecutionEnvironment.canUseDOM) {
56 style = document.createElement('div').style;
57
58 // On some platforms, in particular some releases of Android 4.x,
59 // the un-prefixed "animation" and "transition" properties are defined on the
60 // style object but the events that fire will still be prefixed, so we need
61 // to check if the un-prefixed events are usable, and if not remove them from the map.
62 if (!('AnimationEvent' in window)) {
63 delete vendorPrefixes.animationend.animation;
64 delete vendorPrefixes.animationiteration.animation;
65 delete vendorPrefixes.animationstart.animation;
66 }
67
68 // Same as above
69 if (!('TransitionEvent' in window)) {
70 delete vendorPrefixes.transitionend.transition;
71 }
72}
73
74/**
75 * Attempts to determine the correct vendor prefixed event name.
76 *
77 * @param {string} eventName
78 * @returns {string}
79 */
80function getVendorPrefixedEventName(eventName) {
81 if (prefixedEventNames[eventName]) {
82 return prefixedEventNames[eventName];
83 } else if (!vendorPrefixes[eventName]) {
84 return eventName;
85 }
86
87 var prefixMap = vendorPrefixes[eventName];
88
89 for (var styleProp in prefixMap) {
90 if (prefixMap.hasOwnProperty(styleProp) && styleProp in style) {
91 return prefixedEventNames[eventName] = prefixMap[styleProp];
92 }
93 }
94
95 return '';
96}
97
98module.exports = getVendorPrefixedEventName;
\No newline at end of file