1+ 'use strict' ;
2+
3+ Object . defineProperty ( exports , "__esModule" , {
4+ value : true
5+ } ) ;
6+
7+ var _createClass = function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ( ) ;
8+
9+ exports . default = createPrefixer ;
10+
11+ var _getBrowserInformation = require ( '../utils/getBrowserInformation' ) ;
12+
13+ var _getBrowserInformation2 = _interopRequireDefault ( _getBrowserInformation ) ;
14+
15+ var _getPrefixedKeyframes = require ( '../utils/getPrefixedKeyframes' ) ;
16+
17+ var _getPrefixedKeyframes2 = _interopRequireDefault ( _getPrefixedKeyframes ) ;
18+
19+ var _capitalizeString = require ( '../utils/capitalizeString' ) ;
20+
21+ var _capitalizeString2 = _interopRequireDefault ( _capitalizeString ) ;
22+
23+ var _addNewValuesOnly = require ( '../utils/addNewValuesOnly' ) ;
24+
25+ var _addNewValuesOnly2 = _interopRequireDefault ( _addNewValuesOnly ) ;
26+
27+ var _isObject = require ( '../utils/isObject' ) ;
28+
29+ var _isObject2 = _interopRequireDefault ( _isObject ) ;
30+
31+ var _prefixValue = require ( '../utils/prefixValue' ) ;
32+
33+ var _prefixValue2 = _interopRequireDefault ( _prefixValue ) ;
34+
35+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
36+
37+ function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
38+
39+ function createPrefixer ( _ref ) {
40+ var prefixMap = _ref . prefixMap ,
41+ plugins = _ref . plugins ;
42+ var fallback = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : function ( style ) {
43+ return style ;
44+ } ;
45+
46+ return function ( ) {
47+ /**
48+ * Instantiante a new prefixer
49+ * @param {string } userAgent - userAgent to gather prefix information according to caniuse.com
50+ * @param {string } keepUnprefixed - keeps unprefixed properties and values
51+ */
52+ function Prefixer ( ) {
53+ var options = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : { } ;
54+
55+ _classCallCheck ( this , Prefixer ) ;
56+
57+ var defaultUserAgent = typeof navigator !== 'undefined' ? navigator . userAgent : undefined ;
58+
59+ this . _userAgent = options . userAgent || defaultUserAgent ;
60+ this . _keepUnprefixed = options . keepUnprefixed || false ;
61+
62+ if ( this . _userAgent ) {
63+ this . _browserInfo = ( 0 , _getBrowserInformation2 . default ) ( this . _userAgent ) ;
64+ }
65+
66+ // Checks if the userAgent was resolved correctly
67+ if ( this . _browserInfo && this . _browserInfo . cssPrefix ) {
68+ this . prefixedKeyframes = ( 0 , _getPrefixedKeyframes2 . default ) ( this . _browserInfo . browserName , this . _browserInfo . browserVersion , this . _browserInfo . cssPrefix ) ;
69+ } else {
70+ this . _useFallback = true ;
71+ return false ;
72+ }
73+
74+ var prefixData = this . _browserInfo . browserName && prefixMap [ this . _browserInfo . browserName ] ;
75+ if ( prefixData ) {
76+ this . _requiresPrefix = { } ;
77+
78+ for ( var property in prefixData ) {
79+ if ( prefixData [ property ] >= this . _browserInfo . browserVersion ) {
80+ this . _requiresPrefix [ property ] = true ;
81+ }
82+ }
83+
84+ this . _hasPropsRequiringPrefix = Object . keys ( this . _requiresPrefix ) . length > 0 ;
85+ } else {
86+ this . _useFallback = true ;
87+ }
88+
89+ this . _metaData = {
90+ browserVersion : this . _browserInfo . browserVersion ,
91+ browserName : this . _browserInfo . browserName ,
92+ cssPrefix : this . _browserInfo . cssPrefix ,
93+ jsPrefix : this . _browserInfo . jsPrefix ,
94+ keepUnprefixed : this . _keepUnprefixed ,
95+ requiresPrefix : this . _requiresPrefix
96+ } ;
97+ }
98+
99+ _createClass ( Prefixer , [ {
100+ key : 'prefix' ,
101+ value : function prefix ( style ) {
102+ // use static prefixer as fallback if userAgent can not be resolved
103+ if ( this . _useFallback ) {
104+ return fallback ( style ) ;
105+ }
106+
107+ // only add prefixes if needed
108+ if ( ! this . _hasPropsRequiringPrefix ) {
109+ return style ;
110+ }
111+
112+ return this . _prefixStyle ( style ) ;
113+ }
114+ } , {
115+ key : '_prefixStyle' ,
116+ value : function _prefixStyle ( style ) {
117+ for ( var property in style ) {
118+ var value = style [ property ] ;
119+
120+ // handle nested objects
121+ if ( ( 0 , _isObject2 . default ) ( value ) ) {
122+ style [ property ] = this . prefix ( value ) ;
123+ // handle array values
124+ } else if ( Array . isArray ( value ) ) {
125+ var combinedValue = [ ] ;
126+
127+ for ( var i = 0 , len = value . length ; i < len ; ++ i ) {
128+ var processedValue = ( 0 , _prefixValue2 . default ) ( plugins , property , value [ i ] , style , this . _metaData ) ;
129+ ( 0 , _addNewValuesOnly2 . default ) ( combinedValue , processedValue || value [ i ] ) ;
130+ }
131+
132+ // only modify the value if it was touched
133+ // by any plugin to prevent unnecessary mutations
134+ if ( combinedValue . length > 0 ) {
135+ style [ property ] = combinedValue ;
136+ }
137+ } else {
138+ var _processedValue = ( 0 , _prefixValue2 . default ) ( plugins , property , value , style , this . _metaData ) ;
139+
140+ // only modify the value if it was touched
141+ // by any plugin to prevent unnecessary mutations
142+ if ( _processedValue ) {
143+ style [ property ] = _processedValue ;
144+ }
145+
146+ // add prefixes to properties
147+ if ( this . _requiresPrefix . hasOwnProperty ( property ) ) {
148+ style [ this . _browserInfo . jsPrefix + ( 0 , _capitalizeString2 . default ) ( property ) ] = value ;
149+ if ( ! this . _keepUnprefixed ) {
150+ delete style [ property ] ;
151+ }
152+ }
153+ }
154+ }
155+
156+ return style ;
157+ }
158+
159+ /**
160+ * Returns a prefixed version of the style object using all vendor prefixes
161+ * @param {Object } styles - Style object that gets prefixed properties added
162+ * @returns {Object } - Style object with prefixed properties and values
163+ */
164+
165+ } ] , [ {
166+ key : 'prefixAll' ,
167+ value : function prefixAll ( styles ) {
168+ return fallback ( styles ) ;
169+ }
170+ } ] ) ;
171+
172+ return Prefixer ;
173+ } ( ) ;
174+ }
175+ module . exports = exports [ 'default' ] ;
0 commit comments