Completely updated React, fixed #11, (hopefully)

This commit is contained in:
2018-03-04 19:11:49 -05:00
parent 6e0afd6e2a
commit 34e5f5139a
13674 changed files with 333464 additions and 473223 deletions

View File

@@ -18,6 +18,68 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'd
var isObject = function isObject(obj) {
return obj && (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object' && !Array.isArray(obj);
};
var valueNs = 'extendCurrValue' + Date.now();
function mergeExtend(style, rule, sheet, newStyle) {
var extendType = _typeof(style.extend);
// Extend using a rule name.
if (extendType === 'string') {
if (!sheet) return;
var refRule = sheet.getRule(style.extend);
if (!refRule) return;
if (refRule === rule) {
_get__('warning')(false, '[JSS] A rule tries to extend itself \r\n%s', rule);
return;
}
var parent = refRule.options.parent;
if (parent) {
var originalStyle = parent.rules.raw[style.extend];
_get__('extend')(originalStyle, rule, sheet, newStyle);
}
return;
}
// Extend using an array of objects.
if (Array.isArray(style.extend)) {
for (var index = 0; index < style.extend.length; index++) {
_get__('extend')(style.extend[index], rule, sheet, newStyle);
}
return;
}
// Extend is a style object.
for (var prop in style.extend) {
if (prop === 'extend') {
_get__('extend')(style.extend.extend, rule, sheet, newStyle);
continue;
}
if (_get__('isObject')(style.extend[prop])) {
if (!(prop in newStyle)) newStyle[prop] = {};
_get__('extend')(style.extend[prop], rule, sheet, newStyle[prop]);
continue;
}
newStyle[prop] = style.extend[prop];
}
}
function mergeRest(style, rule, sheet, newStyle) {
// Copy base style.
for (var prop in style) {
if (prop === 'extend') continue;
if (_get__('isObject')(newStyle[prop]) && _get__('isObject')(style[prop])) {
_get__('extend')(style[prop], rule, sheet, newStyle[prop]);
continue;
}
if (_get__('isObject')(style[prop])) {
newStyle[prop] = _get__('extend')(style[prop], rule, sheet);
continue;
}
newStyle[prop] = style[prop];
}
}
/**
* Recursively extend styles.
@@ -25,44 +87,8 @@ var isObject = function isObject(obj) {
function extend(style, rule, sheet) {
var newStyle = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
if (typeof style.extend === 'string') {
if (sheet) {
var refRule = sheet.getRule(style.extend);
if (refRule) {
if (refRule === rule) _get__('warning')(false, '[JSS] A rule tries to extend itself \r\n%s', rule);else if (refRule.options.parent) {
var originalStyle = refRule.options.parent.rules.raw[style.extend];
_get__('extend')(originalStyle, rule, sheet, newStyle);
}
}
}
} else if (Array.isArray(style.extend)) {
for (var index = 0; index < style.extend.length; index++) {
_get__('extend')(style.extend[index], rule, sheet, newStyle);
}
} else {
for (var prop in style.extend) {
if (prop === 'extend') {
_get__('extend')(style.extend.extend, rule, sheet, newStyle);
} else if (_get__('isObject')(style.extend[prop])) {
if (!newStyle[prop]) newStyle[prop] = {};
_get__('extend')(style.extend[prop], rule, sheet, newStyle[prop]);
} else {
newStyle[prop] = style.extend[prop];
}
}
}
// Copy base style.
for (var _prop in style) {
if (_prop === 'extend') continue;
if (_get__('isObject')(newStyle[_prop]) && _get__('isObject')(style[_prop])) {
_get__('extend')(style[_prop], rule, sheet, newStyle[_prop]);
} else if (_get__('isObject')(style[_prop])) {
newStyle[_prop] = _get__('extend')(style[_prop], rule, sheet);
} else {
newStyle[_prop] = style[_prop];
}
}
_get__('mergeExtend')(style, rule, sheet, newStyle);
_get__('mergeRest')(style, rule, sheet, newStyle);
return newStyle;
}
@@ -74,10 +100,32 @@ function extend(style, rule, sheet) {
*/
function jssExtend() {
function onProcessStyle(style, rule, sheet) {
return style.extend ? _get__('extend')(style, rule, sheet) : style;
if ('extend' in style) return _get__('extend')(style, rule, sheet);
return style;
}
return { onProcessStyle: onProcessStyle };
function onChangeValue(value, prop, rule) {
if (prop !== 'extend') return value;
// Value is empty, remove properties set previously.
if (value == null || value === false) {
for (var key in rule[valueNs]) {
rule.prop(key, null);
}
rule[valueNs] = null;
return null;
}
for (var _key in value) {
rule.prop(_key, value[_key]);
}
rule[valueNs] = value;
// Make sure we don't set the value in the core.
return null;
}
return { onProcessStyle: onProcessStyle, onChangeValue: onChangeValue };
}
function _getGlobalObject() {
@@ -195,6 +243,12 @@ function _get_original__(variableName) {
case 'isObject':
return isObject;
case 'mergeExtend':
return mergeExtend;
case 'mergeRest':
return mergeRest;
}
return undefined;

View File

@@ -251,7 +251,7 @@ describe('jss-extend', function () {
});
});
describe('extend using rule name', function () {
describe('extend using rule name with cyclic warning', function () {
var sheet = void 0;
beforeEach(function () {
@@ -269,6 +269,66 @@ describe('jss-extend', function () {
_get__('expect')(sheet.toString()).to.be('.a-id {\n' + ' width: 1px;\n' + '}');
});
});
describe('extend inside of a function rule', function () {
var sheet = void 0;
beforeEach(function () {
var styles = {
a: function a(data) {
return {
height: '200px',
extend: data.redContainer
};
}
};
sheet = jss.createStyleSheet(styles, { link: true }).attach();
sheet.update({
redContainer: {
background: 'red'
}
});
});
it('should have correct output', function () {
_get__('expect')(sheet.getRule('a')).to.not.be(undefined);
_get__('expect')(sheet.toString()).to.be('.a-id {\n' + ' height: 200px;\n' + ' background: red;\n' + '}');
});
});
describe('extend function', function () {
var sheet = void 0;
beforeEach(function () {
var b = { display: 'block' };
sheet = jss.createStyleSheet({
a: {
extend: function extend(data) {
return data.block && b;
},
color: 'red',
'& span': {
extend: function extend(data) {
return data.block && b;
},
color: 'blue'
}
}
});
});
it('should have correct output', function () {
_get__('expect')(sheet.getRule('a')).to.not.be(undefined);
sheet.update({ block: true });
_get__('expect')(sheet.toString()).to.be('.a-id {\n' + ' color: red;\n' + ' display: block;\n' + '}\n' + '.a-id span {\n' + ' color: blue;\n' + ' display: block;\n' + '}');
sheet.update({ block: false });
_get__('expect')(sheet.toString()).to.be('.a-id {\n' + ' color: red;\n' + '}\n' + '.a-id span {\n' + ' color: blue;\n' + '}');
});
});
});
function _getGlobalObject() {