Skip to content
Closed
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
16 changes: 16 additions & 0 deletions src/renderers/dom/shared/CSSPropertyOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ if (__DEV__) {
// style values shouldn't contain a semicolon
var badStyleValueWithSemicolonPattern = /;\s*$/;

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

Expand Down Expand Up @@ -93,6 +94,19 @@ if (__DEV__) {
value.replace(badStyleValueWithSemicolonPattern, '')
);
};

var warnStyleValueWithNaN = function(name) {
if (warnedForNaNValue) {
return;
}

warnedForNaNValue = true;
warning(
false,
'`NaN` is an invalid value for the `%s` style property.',
name
);
};

/**
* @param {string} name
Expand All @@ -105,6 +119,8 @@ if (__DEV__) {
warnBadVendoredStyleName(name);
} else if (badStyleValueWithSemicolonPattern.test(value)) {
warnStyleValueWithSemicolon(name, value);
} else if (typeof value === 'number' && isNaN(value)) {
warnStyleValueWithNaN(name);
}
};
}
Expand Down
11 changes: 11 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,15 @@ 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 when style contains a NaN value', function() {
spyOn(console, 'error');

CSSPropertyOperations.createMarkupForStyles({
fontSize: NaN,
});

expect(console.error.calls.length).toBe(1);
expect(console.error.argsForCall[0][0]).toContain('`NaN` is an invalid value for the `fontSize` style property.');
});
});
17 changes: 0 additions & 17 deletions src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,23 +188,6 @@ describe('ReactDOMComponent', function() {
expect(console.error.calls.length).toBe(2);
});

it('should warn semi-nicely about NaN in style', function() {
spyOn(console, 'error');

var style = {fontSize: NaN};
var div = document.createElement('div');
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[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}.'
);
});

it('should update styles if initially null', function() {
var styles = null;
var container = document.createElement('div');
Expand Down