-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
Fix #1357. Don't append "px" suffix to CSS string values. #5140
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,10 @@ | |
'use strict'; | ||
|
||
var CSSProperty = require('CSSProperty'); | ||
var warning = require('warning'); | ||
|
||
var isUnitlessNumber = CSSProperty.isUnitlessNumber; | ||
var styleWarnings = {}; | ||
|
||
/** | ||
* Convert a value into the proper css writable value. The style name `name` | ||
|
@@ -23,9 +25,10 @@ var isUnitlessNumber = CSSProperty.isUnitlessNumber; | |
* | ||
* @param {string} name CSS property name such as `topMargin`. | ||
* @param {*} value CSS property value such as `10px`. | ||
* @param {ReactDOMComponent} component | ||
* @return {string} Normalized style value with dimensions applied. | ||
*/ | ||
function dangerousStyleValue(name, value) { | ||
function dangerousStyleValue(name, value, component) { | ||
// Note that we've removed escapeTextForBrowser() calls here since the | ||
// whole string will be escaped when the attribute is injected into | ||
// the markup. If you provide unsafe user data here they can inject | ||
|
@@ -48,6 +51,35 @@ function dangerousStyleValue(name, value) { | |
} | ||
|
||
if (typeof value === 'string') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this check is enough; we should not fire the warning if the string already contains the 'px' at the end. Or actually, it might not always be px (could be 'em' or something). We should only print the warning if the string value can be parsed as an integer. |
||
if (__DEV__) { | ||
if (component) { | ||
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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should only append 'px' if the value can be parsed as an integer. If they specify units, we should honor those units to avoid printing '5pxpx' or '5empx' which would make no sense. We can make that change in 0.15 (ie. now), since it should break any old code since presumably no one is doing that yet (because the old code would break). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That behaviour hasn't actually changed. Otherwise the tests would be falling, anyway. If you look at my first commit you'll see that the only actual change in logic is that the reassignment has been replaced with a |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably take in a component name, since some components will be stateless and won't have instances but should still have names (I think).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would I need to change to do that? Just pass in
component._currentElement._owner.getName()
directly?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eeeh, now I'm having mixed feelings about this one. If we're going to make a change, we might as well add the name of the current component in addition to the owner component (to provide more context). So directly passing in something like
component._currentElement._owner.getName()
andcomponent.._tag
. We could do that, but it also means wiring it throughcreateMarkupForStyles
and I'm not sure it's worth it at this point.In a perfect world, we would have the full path, which would require merging #5167 first, but we need to decide on the fate of that PR before we can merge it, and it might not be worth delaying this PR even longer.
I'm tempted to say that this is fine for now, and we can make it better after #5167. Unless @spicyj wants it changed now (or unless we merge #5167 real fast), my intuition is that maybe you just ignore this comment and we deal with stateless components and/or giving the full parent path when those things actually happen.