Skip to content

Commit

Permalink
Add warning for NaN in style object
Browse files Browse the repository at this point in the history
  • Loading branch information
jontewks committed Jan 16, 2016
1 parent 909cba2 commit 8ca7e93
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
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'
);
});
});
7 changes: 2 additions & 5 deletions src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,9 @@ 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: `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: ' +
'{fontSize: NaN}. Mutated style: {fontSize: NaN}.'
'Warning: `NaN` is an invalid value for the `fontSize` css style property'
);
});

Expand Down

0 comments on commit 8ca7e93

Please sign in to comment.