UNPKG

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