Skip to content

Commit

Permalink
Remove css px warning, stop appending px to strings
Browse files Browse the repository at this point in the history
  • Loading branch information
jimfb authored and jim committed May 27, 2016
1 parent ba3bfe3 commit 840afe9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ describe('CSSPropertyOperations', function() {
})).toBe('left:0;margin:16px;opacity:0.5;padding:4px;');
});

it('should trim values so `px` will be appended correctly', function() {
it('should trim values', function() {
expect(CSSPropertyOperations.createMarkupForStyles({
margin: '16 ',
left: '16 ',
opacity: 0.5,
padding: ' 4 ',
})).toBe('margin:16px;opacity:0.5;padding:4px;');
right: ' 4 ',
})).toBe('left:16;opacity:0.5;right:4;');
});

it('should not append `px` to styles that might need a number', function() {
Expand Down
54 changes: 0 additions & 54 deletions src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,60 +163,6 @@ describe('ReactDOMComponent', function() {
);
});

it('should warn about styles with numeric string values for non-unitless properties', function() {
spyOn(console, 'error');

var div = document.createElement('div');
var One = React.createClass({
render: function() {
return this.props.inline ?
<span style={{fontSize: '1'}} /> :
<div style={{fontSize: '1'}} />;
},
});
var Two = React.createClass({
render: function() {
return <div style={{fontSize: '1'}} />;
},
});
ReactDOM.render(<One inline={false} />, div);
expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toBe(
'Warning: a `div` tag (owner: `One`) was passed a numeric string value ' +
'for CSS property `fontSize` (value: `1`) which will be treated ' +
'as a unitless number in a future version of React.'
);

// Don't warn again for the same component
ReactDOM.render(<One inline={true} />, div);
expect(console.error.calls.count()).toBe(1);

// Do warn for different components
ReactDOM.render(<Two />, div);
expect(console.error.calls.count()).toBe(2);
expect(console.error.calls.argsFor(1)[0]).toBe(
'Warning: a `div` tag (owner: `Two`) was passed a numeric string value ' +
'for CSS property `fontSize` (value: `1`) which will be treated ' +
'as a unitless number in a future version of React.'
);

// Really don't warn again for the same component
ReactDOM.render(<One inline={true} />, div);
expect(console.error.calls.count()).toBe(2);
});

it('should not warn for "0" as a unitless style value', function() {
spyOn(console, 'error');
var Component = React.createClass({
render: function() {
return <div style={{margin: '0'}} />;
},
});

ReactTestUtils.renderIntoDocument(<Component />);
expect(console.error.calls.length).toBe(0);
});

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

Expand Down
43 changes: 4 additions & 39 deletions src/renderers/dom/shared/dangerousStyleValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,47 +43,12 @@ function dangerousStyleValue(name, value, component) {
return '';
}

var isNonNumeric = isNaN(value);
if (isNonNumeric || value === 0 ||
isUnitlessNumber.hasOwnProperty(name) && isUnitlessNumber[name]) {
return '' + value; // cast to string
if (typeof value === 'number' && value !== 0 &&
!(isUnitlessNumber.hasOwnProperty(name) && isUnitlessNumber[name])) {
return value+'px'; // Presumes implicit 'px' suffix for unitless numbers
}

if (typeof value === 'string') {
if (__DEV__) {
// Allow '0' to pass through without warning. 0 is already special and
// doesn't require units, so we don't need to warn about it.
if (component && value !== '0') {
var owner = component._currentElement._owner;
var ownerName = owner ? owner.getName() : null;
if (ownerName && !styleWarnings[ownerName]) {
styleWarnings[ownerName] = {};
}
var warned = false;
if (ownerName) {
var warnings = styleWarnings[ownerName];
warned = warnings[name];
if (!warned) {
warnings[name] = true;
}
}
if (!warned) {
warning(
false,
'a `%s` tag (owner: `%s`) was passed a numeric string value ' +
'for CSS property `%s` (value: `%s`) which will be treated ' +
'as a unitless number in a future version of React.',
component._currentElement.type,
ownerName || 'unknown',
name,
value
);
}
}
}
value = value.trim();
}
return value + 'px';
return ('' + value).trim();
}

module.exports = dangerousStyleValue;

0 comments on commit 840afe9

Please sign in to comment.