benchmark/packages/301/dynamic/createPrefixer.js
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
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; }; }();
exports.default = createPrefixer;
var _getBrowserInformation = require('../utils/getBrowserInformation');
var _getBrowserInformation2 = _interopRequireDefault(_getBrowserInformation);
var _getPrefixedKeyframes = require('../utils/getPrefixedKeyframes');
var _getPrefixedKeyframes2 = _interopRequireDefault(_getPrefixedKeyframes);
var _capitalizeString = require('../utils/capitalizeString');
var _capitalizeString2 = _interopRequireDefault(_capitalizeString);
var _addNewValuesOnly = require('../utils/addNewValuesOnly');
var _addNewValuesOnly2 = _interopRequireDefault(_addNewValuesOnly);
var _isObject = require('../utils/isObject');
var _isObject2 = _interopRequireDefault(_isObject);
var _prefixValue = require('../utils/prefixValue');
var _prefixValue2 = _interopRequireDefault(_prefixValue);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function createPrefixer(_ref) {
var prefixMap = _ref.prefixMap,
plugins = _ref.plugins;
var fallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function (style) {
return style;
};
return function () {
/**
* Instantiante a new prefixer
* @param {string} userAgent - userAgent to gather prefix information according to caniuse.com
* @param {string} keepUnprefixed - keeps unprefixed properties and values
*/
function Prefixer() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_classCallCheck(this, Prefixer);
var defaultUserAgent = typeof navigator !== 'undefined' ? navigator.userAgent : undefined;
this._userAgent = options.userAgent || defaultUserAgent;
this._keepUnprefixed = options.keepUnprefixed || false;
if (this._userAgent) {
this._browserInfo = (0, _getBrowserInformation2.default)(this._userAgent);
}
// Checks if the userAgent was resolved correctly
if (this._browserInfo && this._browserInfo.cssPrefix) {
this.prefixedKeyframes = (0, _getPrefixedKeyframes2.default)(this._browserInfo.browserName, this._browserInfo.browserVersion, this._browserInfo.cssPrefix);
} else {
this._useFallback = true;
return false;
}
var prefixData = this._browserInfo.browserName && prefixMap[this._browserInfo.browserName];
if (prefixData) {
this._requiresPrefix = {};
for (var property in prefixData) {
if (prefixData[property] >= this._browserInfo.browserVersion) {
this._requiresPrefix[property] = true;
}
}
this._hasPropsRequiringPrefix = Object.keys(this._requiresPrefix).length > 0;
} else {
this._useFallback = true;
}
this._metaData = {
browserVersion: this._browserInfo.browserVersion,
browserName: this._browserInfo.browserName,
cssPrefix: this._browserInfo.cssPrefix,
jsPrefix: this._browserInfo.jsPrefix,
keepUnprefixed: this._keepUnprefixed,
requiresPrefix: this._requiresPrefix
};
}
_createClass(Prefixer, [{
key: 'prefix',
value: function prefix(style) {
// use static prefixer as fallback if userAgent can not be resolved
if (this._useFallback) {
return fallback(style);
}
// only add prefixes if needed
if (!this._hasPropsRequiringPrefix) {
return style;
}
return this._prefixStyle(style);
}
}, {
key: '_prefixStyle',
value: function _prefixStyle(style) {
for (var property in style) {
var value = style[property];
// handle nested objects
if ((0, _isObject2.default)(value)) {
style[property] = this.prefix(value);
// handle array values
} else if (Array.isArray(value)) {
var combinedValue = [];
for (var i = 0, len = value.length; i < len; ++i) {
var processedValue = (0, _prefixValue2.default)(plugins, property, value[i], style, this._metaData);
(0, _addNewValuesOnly2.default)(combinedValue, processedValue || value[i]);
}
// only modify the value if it was touched
// by any plugin to prevent unnecessary mutations
if (combinedValue.length > 0) {
style[property] = combinedValue;
}
} else {
var _processedValue = (0, _prefixValue2.default)(plugins, property, value, style, this._metaData);
// only modify the value if it was touched
// by any plugin to prevent unnecessary mutations
if (_processedValue) {
style[property] = _processedValue;
}
// add prefixes to properties
if (this._requiresPrefix.hasOwnProperty(property)) {
style[this._browserInfo.jsPrefix + (0, _capitalizeString2.default)(property)] = value;
if (!this._keepUnprefixed) {
delete style[property];
}
}
}
}
return style;
}
/**
* Returns a prefixed version of the style object using all vendor prefixes
* @param {Object} styles - Style object that gets prefixed properties added
* @returns {Object} - Style object with prefixed properties and values
*/
}], [{
key: 'prefixAll',
value: function prefixAll(styles) {
return fallback(styles);
}
}]);
return Prefixer;
}();
}
module.exports = exports['default'];