|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2016 Google Inc. |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +enum StandardCssPropertyName { |
| 25 | + ANIMATION = 'animation', |
| 26 | + TRANSFORM = 'transform', |
| 27 | + TRANSITION = 'transition', |
| 28 | +} |
| 29 | + |
| 30 | +enum PrefixedCssPropertyName { |
| 31 | + WEBKIT_ANIMATION = '-webkit-animation', |
| 32 | + WEBKIT_TRANSFORM = '-webkit-transform', |
| 33 | + WEBKIT_TRANSITION = '-webkit-transition', |
| 34 | +} |
| 35 | + |
| 36 | +enum StandardJsEventType { |
| 37 | + ANIMATION_END = 'animationend', |
| 38 | + ANIMATION_ITERATION = 'animationiteration', |
| 39 | + ANIMATION_START = 'animationstart', |
| 40 | + TRANSITION_END = 'transitionend', |
| 41 | +} |
| 42 | + |
| 43 | +enum PrefixedJsEventType { |
| 44 | + WEBKIT_ANIMATION_END = 'webkitAnimationEnd', |
| 45 | + WEBKIT_ANIMATION_ITERATION = 'webkitAnimationIteration', |
| 46 | + WEBKIT_ANIMATION_START = 'webkitAnimationStart', |
| 47 | + WEBKIT_TRANSITION_END = 'webkitTransitionEnd', |
| 48 | +} |
| 49 | + |
| 50 | +interface CssVendorPropertyMap { |
| 51 | + [key: string]: CssVendorProperty; |
| 52 | +} |
| 53 | + |
| 54 | +interface JsVendorPropertyMap { |
| 55 | + [key: string]: JsVendorProperty; |
| 56 | +} |
| 57 | + |
| 58 | +interface CssVendorProperty { |
| 59 | + prefixed: PrefixedCssPropertyName; |
| 60 | + standard: StandardCssPropertyName; |
| 61 | +} |
| 62 | + |
| 63 | +interface JsVendorProperty { |
| 64 | + cssProperty: StandardCssPropertyName; |
| 65 | + prefixed: PrefixedJsEventType; |
| 66 | + standard: StandardJsEventType; |
| 67 | +} |
| 68 | + |
| 69 | +const transformStyleProperties = ['transform', 'WebkitTransform', 'MozTransform', 'OTransform', 'MSTransform']; |
| 70 | + |
| 71 | +// Destructure enum members to make their usages more readable. |
| 72 | +const {WEBKIT_ANIMATION, WEBKIT_TRANSFORM, WEBKIT_TRANSITION} = PrefixedCssPropertyName; |
| 73 | +const {ANIMATION, TRANSFORM, TRANSITION} = StandardCssPropertyName; |
| 74 | +const { |
| 75 | + WEBKIT_ANIMATION_END, |
| 76 | + WEBKIT_ANIMATION_ITERATION, |
| 77 | + WEBKIT_ANIMATION_START, |
| 78 | + WEBKIT_TRANSITION_END, |
| 79 | +} = PrefixedJsEventType; |
| 80 | +const {ANIMATION_END, ANIMATION_ITERATION, ANIMATION_START, TRANSITION_END} = StandardJsEventType; |
| 81 | + |
| 82 | +const cssPropertyNameMap: CssVendorPropertyMap = { |
| 83 | + [ANIMATION]: { |
| 84 | + prefixed: WEBKIT_ANIMATION, |
| 85 | + standard: ANIMATION, |
| 86 | + }, |
| 87 | + [TRANSFORM]: { |
| 88 | + prefixed: WEBKIT_TRANSFORM, |
| 89 | + standard: TRANSFORM, |
| 90 | + }, |
| 91 | + [TRANSITION]: { |
| 92 | + prefixed: WEBKIT_TRANSITION, |
| 93 | + standard: TRANSITION, |
| 94 | + }, |
| 95 | +}; |
| 96 | + |
| 97 | +const jsEventTypeMap: JsVendorPropertyMap = { |
| 98 | + [ANIMATION_END]: { |
| 99 | + cssProperty: ANIMATION, |
| 100 | + prefixed: WEBKIT_ANIMATION_END, |
| 101 | + standard: ANIMATION_END, |
| 102 | + }, |
| 103 | + [ANIMATION_ITERATION]: { |
| 104 | + cssProperty: ANIMATION, |
| 105 | + prefixed: WEBKIT_ANIMATION_ITERATION, |
| 106 | + standard: ANIMATION_ITERATION, |
| 107 | + }, |
| 108 | + [ANIMATION_START]: { |
| 109 | + cssProperty: ANIMATION, |
| 110 | + prefixed: WEBKIT_ANIMATION_START, |
| 111 | + standard: ANIMATION_START, |
| 112 | + }, |
| 113 | + [TRANSITION_END]: { |
| 114 | + cssProperty: TRANSITION, |
| 115 | + prefixed: WEBKIT_TRANSITION_END, |
| 116 | + standard: TRANSITION_END, |
| 117 | + }, |
| 118 | +}; |
| 119 | + |
| 120 | +function isWindow(windowObj: Window): boolean { |
| 121 | + return Boolean(windowObj.document) && typeof windowObj.document.createElement === 'function'; |
| 122 | +} |
| 123 | + |
| 124 | +function getCorrectPropertyName(windowObj: Window, cssProperty: StandardCssPropertyName): |
| 125 | + StandardCssPropertyName | PrefixedCssPropertyName { |
| 126 | + if (isWindow(windowObj) && cssProperty in cssPropertyNameMap) { |
| 127 | + const el = windowObj.document.createElement('div'); |
| 128 | + const {standard, prefixed} = cssPropertyNameMap[cssProperty]; |
| 129 | + const isStandard = standard in el.style; |
| 130 | + return isStandard ? standard : prefixed; |
| 131 | + } |
| 132 | + return cssProperty; |
| 133 | +} |
| 134 | + |
| 135 | +function getCorrectEventName(windowObj: Window, eventType: StandardJsEventType): |
| 136 | + StandardJsEventType | PrefixedJsEventType { |
| 137 | + if (isWindow(windowObj) && eventType in jsEventTypeMap) { |
| 138 | + const el = windowObj.document.createElement('div'); |
| 139 | + const {standard, prefixed, cssProperty} = jsEventTypeMap[eventType]; |
| 140 | + const isStandard = cssProperty in el.style; |
| 141 | + return isStandard ? standard : prefixed; |
| 142 | + } |
| 143 | + return eventType; |
| 144 | +} |
| 145 | + |
| 146 | +export { |
| 147 | + PrefixedCssPropertyName, |
| 148 | + StandardCssPropertyName, |
| 149 | + PrefixedJsEventType, |
| 150 | + StandardJsEventType, |
| 151 | + getCorrectEventName, |
| 152 | + getCorrectPropertyName, |
| 153 | + transformStyleProperties, |
| 154 | +}; |
0 commit comments