Skip to content

Commit 4fe3708

Browse files
committed
Add warning about css shorthand properties collision(facebook#6348)
1 parent 0af8199 commit 4fe3708

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

packages/react-dom/src/__tests__/ReactDOMComponent-test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2698,5 +2698,34 @@ describe('ReactDOMComponent', () => {
26982698
ReactDOM.render(<Component />, container);
26992699
expect(typeof container.onclick).not.toBe('function');
27002700
});
2701+
2702+
it('should warn about css shorthand properties collision', () => {
2703+
const container = document.createElement('div');
2704+
const oldStyle = {
2705+
background: 'url(http://example.org/image/a.jpg) no-repeat center',
2706+
backgroundSize: '150px',
2707+
backgroundColor: 'red',
2708+
};
2709+
2710+
ReactDOM.render(<div style={oldStyle} />, container);
2711+
const stubStyle = container.firstChild.style;
2712+
2713+
expect(stubStyle.background).toEqual(
2714+
'url(http://example.org/image/a.jpg) no-repeat center',
2715+
);
2716+
expect(stubStyle.backgroundSize).toEqual('150px');
2717+
expect(stubStyle.backgroundColor).toEqual('red');
2718+
2719+
const newStyle = {
2720+
background: 'url(http://example.org/image/b.jpg) no-repeat center',
2721+
};
2722+
2723+
expect(() =>
2724+
ReactDOM.render(<div style={newStyle} />, container),
2725+
).toWarnDev(
2726+
'Warning: Css shorthand properties collision:' +
2727+
' background properties are being overridden. (backgroundSize,backgroundColor)',
2728+
);
2729+
});
27012730
});
27022731
});

packages/react-dom/src/client/ReactDOMComponent.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {registrationNameModules} from 'events/EventPluginRegistry';
1313
import warning from 'shared/warning';
1414
import {canUseDOM} from 'shared/ExecutionEnvironment';
1515
import warningWithoutStack from 'shared/warningWithoutStack';
16+
import {shorthandProperties} from '../shared/CSSProperty';
1617

1718
import * as DOMPropertyOperations from './DOMPropertyOperations';
1819
import * as ReactDOMInput from './ReactDOMInput';
@@ -709,6 +710,26 @@ export function diffProperties(
709710
if (!styleUpdates) {
710711
styleUpdates = {};
711712
}
713+
// Check shorthand properties collision
714+
if (__DEV__) {
715+
if (shorthandProperties.hasOwnProperty(styleName)) {
716+
const propertyGroup = shorthandProperties[styleName];
717+
const overriddenProperties = [];
718+
for (let lastStyleName in lastProp) {
719+
if (propertyGroup.includes(lastStyleName)) {
720+
overriddenProperties.push(lastStyleName);
721+
}
722+
}
723+
if (overriddenProperties.length > 0) {
724+
warning(
725+
false,
726+
'Css shorthand properties collision: %s properties are being overridden. (%s)',
727+
styleName,
728+
overriddenProperties,
729+
);
730+
}
731+
}
732+
}
712733
styleUpdates[styleName] = nextProp[styleName];
713734
}
714735
}

packages/react-dom/src/shared/CSSProperty.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ export const isUnitlessNumber = {
5555
strokeWidth: true,
5656
};
5757

58+
export const shorthandProperties = {
59+
background: [
60+
'backgroundAttachment',
61+
'backgroundClip',
62+
'backgroundColor',
63+
'backgroundImage',
64+
'backgroundOrigin',
65+
'backgroundPosition',
66+
'backgroundRepeat',
67+
'backgroundSize',
68+
],
69+
};
5870
/**
5971
* @param {string} prefix vendor-specific prefix, eg: Webkit
6072
* @param {string} key style name, eg: transitionDuration

0 commit comments

Comments
 (0)