Skip to content

Commit 0aeda52

Browse files
committed
Validate styles during reconciliation
Previously, Fiber validated styles at the same time setting properties. However, since properties get updated during the commit phase, validation in updates did not have access to the current owner. We have moved validation to prepareUpdate() since it has all the necessary information. Now Fiber has access to the owner when validating styles. We can add more validations to the same place later.
1 parent 8ce84e9 commit 0aeda52

4 files changed

Lines changed: 48 additions & 6 deletions

File tree

scripts/fiber/tests-passing-except-dev.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
src/addons/transitions/__tests__/ReactTransitionGroup-test.js
22
* should warn for duplicated keys with component stack info
33

4-
src/renderers/dom/shared/__tests__/CSSPropertyOperations-test.js
5-
* should warn when updating hyphenated style names
6-
74
src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js
85
* should warn for unknown prop
96
* should group multiple unknown prop warnings together

scripts/fiber/tests-passing.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ src/renderers/dom/shared/__tests__/CSSPropertyOperations-test.js
553553
* should set style attribute when styles exist
554554
* should not set style attribute when no styles exist
555555
* should warn when using hyphenated style names
556+
* should warn when updating hyphenated style names
556557
* warns when miscapitalizing vendored style names
557558
* should warn about style having a trailing semicolon
558559
* should warn about style containing a NaN value

src/renderers/dom/fiber/ReactDOMFiber.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var {
3535
createElement,
3636
getChildNamespace,
3737
setInitialProperties,
38+
validateProperties,
3839
updateProperties,
3940
} = ReactDOMFiberComponent;
4041
var { precacheFiberNode } = ReactDOMComponentTree;
@@ -101,6 +102,9 @@ var DOMRenderer = ReactFiberReconciler({
101102
props : Props,
102103
rootContainerInstance : Container,
103104
) : void {
105+
if (__DEV__) {
106+
validateProperties(domElement, type, null, props);
107+
}
104108
setInitialProperties(domElement, type, props, rootContainerInstance);
105109
},
106110

@@ -110,6 +114,9 @@ var DOMRenderer = ReactFiberReconciler({
110114
oldProps : Props,
111115
newProps : Props
112116
) : boolean {
117+
if (__DEV__) {
118+
validateProperties(domElement, type, oldProps, newProps);
119+
}
113120
return true;
114121
},
115122

src/renderers/dom/fiber/ReactDOMFiberComponent.js

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,16 +424,38 @@ function updateDOMProperties(
424424
}
425425
}
426426
if (styleUpdates) {
427-
if (__DEV__) {
428-
CSSPropertyOperations.warnValidStyles(styleUpdates);
429-
}
430427
CSSPropertyOperations.setValueForStyles(
431428
domElement,
432429
styleUpdates,
433430
);
434431
}
435432
}
436433

434+
function validateStyles(lastStyle : Object | null, nextStyle : Object) {
435+
if (__DEV__) {
436+
let styleUpdates = null;
437+
// Find all styles that changed.
438+
if (lastStyle != null) {
439+
for (let styleName in nextStyle) {
440+
if (
441+
nextStyle.hasOwnProperty(styleName) &&
442+
lastStyle[styleName] !== nextStyle[styleName]
443+
) {
444+
styleUpdates = styleUpdates || {};
445+
styleUpdates[styleName] = nextStyle[styleUpdates];
446+
}
447+
}
448+
if (styleUpdates == null) {
449+
// Nothing changed.
450+
return;
451+
}
452+
} else {
453+
styleUpdates = nextStyle;
454+
}
455+
CSSPropertyOperations.warnValidStyles(styleUpdates);
456+
}
457+
}
458+
437459
// Assumes there is no parent namespace.
438460
function getIntrinsicNamespace(type : string) : string | null {
439461
switch (type) {
@@ -694,6 +716,21 @@ var ReactDOMFiberComponent = {
694716
}
695717
},
696718

719+
validateProperties(
720+
domElement : Element,
721+
tag : string,
722+
lastProps : Object | null,
723+
nextProps : Object,
724+
) : void {
725+
if (__DEV__) {
726+
const nextStyle = nextProps.style;
727+
if (nextStyle != null) {
728+
const lastStyle = lastProps == null ? null : lastProps.style;
729+
validateStyles(lastStyle, nextStyle);
730+
}
731+
}
732+
},
733+
697734
restoreControlledState(domElement : Element, tag : string, props : Object) : void {
698735
switch (tag) {
699736
case 'input':

0 commit comments

Comments
 (0)