mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Add warning for NaN in style object
This commit is contained in:
@@ -50,6 +50,7 @@ if (__DEV__) {
|
||||
|
||||
var warnedStyleNames = {};
|
||||
var warnedStyleValues = {};
|
||||
var warnedForNaNValue = false;
|
||||
|
||||
var warnHyphenatedStyleName = function(name) {
|
||||
if (warnedStyleNames.hasOwnProperty(name) && warnedStyleNames[name]) {
|
||||
@@ -94,6 +95,19 @@ if (__DEV__) {
|
||||
);
|
||||
};
|
||||
|
||||
var warnStyleValueIsNaN = function(name, value) {
|
||||
if (warnedForNaNValue) {
|
||||
return;
|
||||
}
|
||||
|
||||
warnedForNaNValue = true;
|
||||
warning(
|
||||
false,
|
||||
'`NaN` is an invalid value for the `%s` css style property',
|
||||
name
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} name
|
||||
* @param {*} value
|
||||
@@ -106,6 +120,10 @@ if (__DEV__) {
|
||||
} else if (badStyleValueWithSemicolonPattern.test(value)) {
|
||||
warnStyleValueWithSemicolon(name, value);
|
||||
}
|
||||
|
||||
if (typeof value === 'number' && isNaN(value)) {
|
||||
warnStyleValueIsNaN(name, value);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -166,4 +166,17 @@ describe('CSSPropertyOperations', function() {
|
||||
expect(console.error.argsForCall[0][0]).toContain('Try "backgroundColor: blue" instead');
|
||||
expect(console.error.argsForCall[1][0]).toContain('Try "color: red" instead');
|
||||
});
|
||||
|
||||
it('should warn about style containing a NaN value', function() {
|
||||
spyOn(console, 'error');
|
||||
|
||||
CSSPropertyOperations.createMarkupForStyles({
|
||||
fontSize: NaN,
|
||||
});
|
||||
|
||||
expect(console.error.calls.length).toBe(1);
|
||||
expect(console.error.argsForCall[0][0]).toEqual(
|
||||
'Warning: `NaN` is an invalid value for the `fontSize` css style property'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -196,8 +196,11 @@ describe('ReactDOMComponent', function() {
|
||||
ReactDOM.render(<span style={style}></span>, div);
|
||||
ReactDOM.render(<span style={style}></span>, div);
|
||||
|
||||
expect(console.error.argsForCall.length).toBe(1);
|
||||
expect(console.error.argsForCall.length).toBe(2);
|
||||
expect(console.error.argsForCall[0][0]).toEqual(
|
||||
'Warning: `NaN` is an invalid value for the `fontSize` css style property',
|
||||
);
|
||||
expect(console.error.argsForCall[1][0]).toEqual(
|
||||
'Warning: `span` was passed a style object that has previously been ' +
|
||||
'mutated. Mutating `style` is deprecated. Consider cloning it ' +
|
||||
'beforehand. Check the `render` using <span>. Previous style: ' +
|
||||
|
||||
Reference in New Issue
Block a user