File tree 3 files changed +62
-0
lines changed 3 files changed +62
-0
lines changed Original file line number Diff line number Diff line change @@ -2698,5 +2698,34 @@ describe('ReactDOMComponent', () => {
2698
2698
ReactDOM . render ( < Component /> , container ) ;
2699
2699
expect ( typeof container . onclick ) . not . toBe ( 'function' ) ;
2700
2700
} ) ;
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
+ } ) ;
2701
2730
} ) ;
2702
2731
} ) ;
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import {registrationNameModules} from 'events/EventPluginRegistry';
13
13
import warning from 'shared/warning' ;
14
14
import { canUseDOM } from 'shared/ExecutionEnvironment' ;
15
15
import warningWithoutStack from 'shared/warningWithoutStack' ;
16
+ import { shorthandProperties } from '../shared/CSSProperty' ;
16
17
17
18
import * as DOMPropertyOperations from './DOMPropertyOperations' ;
18
19
import * as ReactDOMInput from './ReactDOMInput' ;
@@ -709,6 +710,26 @@ export function diffProperties(
709
710
if ( ! styleUpdates ) {
710
711
styleUpdates = { } ;
711
712
}
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
+ }
712
733
styleUpdates [ styleName ] = nextProp [ styleName ] ;
713
734
}
714
735
}
Original file line number Diff line number Diff line change @@ -55,6 +55,18 @@ export const isUnitlessNumber = {
55
55
strokeWidth : true ,
56
56
} ;
57
57
58
+ export const shorthandProperties = {
59
+ background : [
60
+ 'backgroundAttachment' ,
61
+ 'backgroundClip' ,
62
+ 'backgroundColor' ,
63
+ 'backgroundImage' ,
64
+ 'backgroundOrigin' ,
65
+ 'backgroundPosition' ,
66
+ 'backgroundRepeat' ,
67
+ 'backgroundSize' ,
68
+ ] ,
69
+ } ;
58
70
/**
59
71
* @param {string } prefix vendor-specific prefix, eg: Webkit
60
72
* @param {string } key style name, eg: transitionDuration
You can’t perform that action at this time.
0 commit comments