Skip to content

Commit

Permalink
Fix facebook#1357. Don't append "px" suffix to strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
pluma committed Oct 12, 2015
1 parent 01817c1 commit 3176377
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
7 changes: 7 additions & 0 deletions docs/tips/06-style-props-value-px.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@ Sometimes you _do_ want to keep the CSS properties unitless. Here's a list of pr
- `widows`
- `zIndex`
- `zoom`

Alternatively you can simply pass the unitless value as a string:

```js
var divStyle = {WebkitMagic: "10"}; // rendered as "-webkit-magic:10"
ReactDOM.render(<div style={divStyle}>Hello World!</div>, mountNode);
```
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 string values', function() {
expect(CSSPropertyOperations.createMarkupForStyles({
margin: '16 ',
opacity: 0.5,
padding: ' 4 ',
})).toBe('margin:16px;opacity:0.5;padding:4px;');
xmagic: ' 4 ',
})).toBe('margin:16;opacity:0.5;xmagic:4;');
});

it('should not append `px` to styles that might need a number', function() {
Expand Down
4 changes: 2 additions & 2 deletions src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ describe('ReactDOMComponent', function() {
var stubStyle = container.firstChild.style;

// set initial style
var setup = {display: 'block', left: '1', top: 2, fontFamily: 'Arial'};
var setup = {display: 'block', WebkitMagic: '1', top: 2, fontFamily: 'Arial'};
ReactDOM.render(<div style={setup} />, container);
expect(stubStyle.display).toEqual('block');
expect(stubStyle.left).toEqual('1px');
expect(stubStyle.WebkitMagic).toEqual('1');
expect(stubStyle.fontFamily).toEqual('Arial');

// reset the style to their default state
Expand Down
2 changes: 1 addition & 1 deletion src/renderers/dom/shared/dangerousStyleValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function dangerousStyleValue(name, value) {
}

if (typeof value === 'string') {
value = value.trim();
return value.trim();
}
return value + 'px';
}
Expand Down

0 comments on commit 3176377

Please sign in to comment.