183 lines
5.5 KiB
JavaScript
183 lines
5.5 KiB
JavaScript
'use strict';
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
|
|
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
|
|
|
|
exports.default = jssExpand;
|
|
|
|
var _props = require('./props');
|
|
|
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
|
|
/**
|
|
* Map values by given prop.
|
|
*
|
|
* @param {Array} array of values
|
|
* @param {String} original property
|
|
* @param {String} original rule
|
|
* @return {String} mapped values
|
|
*/
|
|
function mapValuesByProp(value, prop, rule) {
|
|
return value.map(function (item) {
|
|
return objectToArray(item, prop, rule, false, true);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Convert array to nested array, if needed
|
|
*
|
|
* @param {Array} array of values
|
|
* @param {String} original property
|
|
* @param {Object} sheme, for converting arrays in strings
|
|
* @param {Object} original rule
|
|
* @return {String} converted string
|
|
*/
|
|
function processArray(value, prop, scheme, rule) {
|
|
if (scheme[prop] == null) return value;
|
|
if (value.length === 0) return [];
|
|
if (Array.isArray(value[0])) return processArray(value[0], prop, scheme);
|
|
if (_typeof(value[0]) === 'object') {
|
|
return mapValuesByProp(value, prop, rule);
|
|
}
|
|
|
|
return [value];
|
|
}
|
|
|
|
/**
|
|
* Convert object to array.
|
|
*
|
|
* @param {Object} object of values
|
|
* @param {String} original property
|
|
* @param {Object} original rule
|
|
* @param {Boolean} is fallback prop
|
|
* @param {Boolean} object is inside array
|
|
* @return {String} converted string
|
|
*/
|
|
function objectToArray(value, prop, rule, isFallback, isInArray) {
|
|
if (!(_props.propObj[prop] || _props.customPropObj[prop])) return [];
|
|
|
|
var result = [];
|
|
|
|
// Check if exists any non-standart property
|
|
if (_props.customPropObj[prop]) {
|
|
value = customPropsToStyle(value, rule, _props.customPropObj[prop], isFallback);
|
|
}
|
|
|
|
// Pass throught all standart props
|
|
if (Object.keys(value).length) {
|
|
for (var baseProp in _props.propObj[prop]) {
|
|
if (value[baseProp]) {
|
|
if (Array.isArray(value[baseProp])) {
|
|
result.push(_props.propArrayInObj[baseProp] === null ? value[baseProp] : value[baseProp].join(' '));
|
|
} else result.push(value[baseProp]);
|
|
continue;
|
|
}
|
|
|
|
// Add default value from props config.
|
|
if (_props.propObj[prop][baseProp] != null) {
|
|
result.push(_props.propObj[prop][baseProp]);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!result.length || isInArray) return result;
|
|
return [result];
|
|
}
|
|
|
|
/**
|
|
* Convert custom properties values to styles adding them to rule directly
|
|
*
|
|
* @param {Object} object of values
|
|
* @param {Object} original rule
|
|
* @param {String} property, that contain partial custom properties
|
|
* @param {Boolean} is fallback prop
|
|
* @return {Object} value without custom properties, that was already added to rule
|
|
*/
|
|
function customPropsToStyle(value, rule, customProps, isFallback) {
|
|
for (var prop in customProps) {
|
|
var propName = customProps[prop];
|
|
|
|
// If current property doesn't exist already in rule - add new one
|
|
if (typeof value[prop] !== 'undefined' && (isFallback || !rule.prop(propName))) {
|
|
var appendedValue = styleDetector(_defineProperty({}, propName, value[prop]), rule)[propName];
|
|
|
|
// Add style directly in rule
|
|
if (isFallback) rule.style.fallbacks[propName] = appendedValue;else rule.style[propName] = appendedValue;
|
|
}
|
|
// Delete converted property to avoid double converting
|
|
delete value[prop];
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* Detect if a style needs to be converted.
|
|
*
|
|
* @param {Object} style
|
|
* @param {Object} rule
|
|
* @param {Boolean} is fallback prop
|
|
* @return {Object} convertedStyle
|
|
*/
|
|
function styleDetector(style, rule, isFallback) {
|
|
for (var prop in style) {
|
|
var value = style[prop];
|
|
|
|
if (Array.isArray(value)) {
|
|
// Check double arrays to avoid recursion.
|
|
if (!Array.isArray(value[0])) {
|
|
if (prop === 'fallbacks') {
|
|
for (var index = 0; index < style.fallbacks.length; index++) {
|
|
style.fallbacks[index] = styleDetector(style.fallbacks[index], rule, true);
|
|
}
|
|
continue;
|
|
}
|
|
|
|
style[prop] = processArray(value, prop, _props.propArray);
|
|
// Avoid creating properties with empty values
|
|
if (!style[prop].length) delete style[prop];
|
|
}
|
|
} else if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object') {
|
|
if (prop === 'fallbacks') {
|
|
style.fallbacks = styleDetector(style.fallbacks, rule, true);
|
|
continue;
|
|
}
|
|
|
|
style[prop] = objectToArray(value, prop, rule, isFallback);
|
|
// Avoid creating properties with empty values
|
|
if (!style[prop].length) delete style[prop];
|
|
}
|
|
|
|
// Maybe a computed value resulting in an empty string
|
|
else if (style[prop] === '') delete style[prop];
|
|
}
|
|
|
|
return style;
|
|
}
|
|
|
|
/**
|
|
* Adds possibility to write expanded styles.
|
|
*
|
|
* @param {Rule} rule
|
|
* @api public
|
|
*/
|
|
function jssExpand() {
|
|
function onProcessStyle(style, rule) {
|
|
if (!style || rule.type !== 'style') return style;
|
|
|
|
if (Array.isArray(style)) {
|
|
// Pass rules one by one and reformat them
|
|
for (var index = 0; index < style.length; index++) {
|
|
style[index] = styleDetector(style[index], rule);
|
|
}
|
|
return style;
|
|
}
|
|
|
|
return styleDetector(style, rule);
|
|
}
|
|
|
|
return { onProcessStyle: onProcessStyle };
|
|
} |