Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn when a style object has NaN for a value #5811

Merged
merged 1 commit into from
Jan 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/renderers/dom/shared/CSSPropertyOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ if (__DEV__) {

var warnedStyleNames = {};
var warnedStyleValues = {};
var warnedForNaNValue = false;

var warnHyphenatedStyleName = function(name) {
if (warnedStyleNames.hasOwnProperty(name) && warnedStyleNames[name]) {
Expand Down Expand Up @@ -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
Expand All @@ -106,6 +120,10 @@ if (__DEV__) {
} else if (badStyleValueWithSemicolonPattern.test(value)) {
warnStyleValueWithSemicolon(name, value);
}

if (typeof value === 'number' && isNaN(value)) {
warnStyleValueIsNaN(name, value);
}
};
}

Expand Down
13 changes: 13 additions & 0 deletions src/renderers/dom/shared/__tests__/CSSPropertyOperations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
});
});
5 changes: 4 additions & 1 deletion src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: ' +
Expand Down