-
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
Warn about conflicting style values during updates #14181
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 |
---|---|---|
|
@@ -5,9 +5,16 @@ | |
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import { | ||
overlappingShorthandsInDev, | ||
longhandToShorthandInDev, | ||
shorthandToLonghandInDev, | ||
} from './CSSShorthandProperty'; | ||
|
||
import dangerousStyleValue from './dangerousStyleValue'; | ||
import hyphenateStyleName from './hyphenateStyleName'; | ||
import warnValidStyle from './warnValidStyle'; | ||
import warning from 'shared/warning'; | ||
|
||
/** | ||
* Operations for dealing with CSS properties. | ||
|
@@ -78,3 +85,92 @@ export function setValueForStyles(node, styles) { | |
} | ||
} | ||
} | ||
|
||
function isValueEmpty(value) { | ||
return value == null || typeof value === 'boolean' || value === ''; | ||
} | ||
|
||
/** | ||
* When mixing shorthand and longhand property names, we warn during updates if | ||
* we expect an incorrect result to occur. In particular, we warn for: | ||
* | ||
* Updating a shorthand property (longhand gets overwritten): | ||
* {font: 'foo', fontVariant: 'bar'} -> {font: 'baz', fontVariant: 'bar'} | ||
* becomes .style.font = 'baz' | ||
* Removing a shorthand property (longhand gets lost too): | ||
* {font: 'foo', fontVariant: 'bar'} -> {fontVariant: 'bar'} | ||
* becomes .style.font = '' | ||
* Removing a longhand property (should revert to shorthand; doesn't): | ||
* {font: 'foo', fontVariant: 'bar'} -> {font: 'foo'} | ||
* becomes .style.fontVariant = '' | ||
*/ | ||
export function validateShorthandPropertyCollisionInDev( | ||
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. Seems like this could get noisy if we don't do any kind of de-duping. I wonder if this is something you considered and ruled out? Guess de-duping would have a small impact on tests too. 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 thought about it but I don't think this will be very common, it is a bad bug, and it is pretty easy to fix – so I think it's OK not to. |
||
styleUpdates, | ||
nextStyles, | ||
) { | ||
if (!nextStyles) { | ||
return; | ||
} | ||
|
||
for (const key in styleUpdates) { | ||
const isEmpty = isValueEmpty(styleUpdates[key]); | ||
if (isEmpty) { | ||
// Property removal; check if we're removing a longhand property | ||
const shorthands = longhandToShorthandInDev[key]; | ||
if (shorthands) { | ||
const conflicting = shorthands.filter( | ||
s => !isValueEmpty(nextStyles[s]), | ||
); | ||
if (conflicting.length) { | ||
warning( | ||
false, | ||
'Removing a style property during rerender (%s) when a ' + | ||
'conflicting property is set (%s) can lead to styling bugs. To ' + | ||
"avoid this, don't mix shorthand and non-shorthand properties " + | ||
'for the same value; instead, replace the shorthand with ' + | ||
'separate values.', | ||
key, | ||
conflicting.join(', '), | ||
); | ||
} | ||
} | ||
} | ||
|
||
// Updating or removing a property; check if it's a shorthand property | ||
const longhands = shorthandToLonghandInDev[key]; | ||
const overlapping = overlappingShorthandsInDev[key]; | ||
// eslint-disable-next-line no-var | ||
var conflicting = new Set(); | ||
if (longhands) { | ||
longhands.forEach(l => { | ||
if (isValueEmpty(styleUpdates[l]) && !isValueEmpty(nextStyles[l])) { | ||
// ex: key = 'font', l = 'fontStyle' | ||
conflicting.add(l); | ||
} | ||
}); | ||
} | ||
if (overlapping) { | ||
overlapping.forEach(l => { | ||
if (isValueEmpty(styleUpdates[l]) && !isValueEmpty(nextStyles[l])) { | ||
// ex: key = 'borderLeft', l = 'borderStyle' | ||
conflicting.add(l); | ||
} | ||
}); | ||
} | ||
if (conflicting.size) { | ||
warning( | ||
false, | ||
'%s a style property during rerender (%s) when a ' + | ||
'conflicting property is set (%s) can lead to styling bugs. To ' + | ||
"avoid this, don't mix shorthand and non-shorthand properties " + | ||
'for the same value; instead, replace the shorthand with ' + | ||
'separate values.', | ||
isEmpty ? 'Removing' : 'Updating', | ||
key, | ||
Array.from(conflicting) | ||
.sort() | ||
.join(', '), | ||
); | ||
} | ||
} | ||
} |
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.
Ugh, this is subtle. Had to read these tests a couple times. Nice to have a warning for it.