-
Notifications
You must be signed in to change notification settings - Fork 45.6k
/
ReactDOMUnknownPropertyHook.js
165 lines (144 loc) · 4.57 KB
/
ReactDOMUnknownPropertyHook.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Copyright 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule ReactDOMUnknownPropertyHook
*/
'use strict';
var DOMProperty = require('DOMProperty');
var EventPluginRegistry = require('EventPluginRegistry');
if (__DEV__) {
var warning = require('fbjs/lib/warning');
var {
ReactComponentTreeHook,
ReactDebugCurrentFrame,
} = require('ReactGlobalSharedState');
var {getStackAddendumByID} = ReactComponentTreeHook;
}
function getStackAddendum(debugID) {
if (debugID != null) {
// This can only happen on Stack
return getStackAddendumByID(debugID);
} else {
// This can only happen on Fiber / Server
var stack = ReactDebugCurrentFrame.getStackAddendum();
return stack != null ? stack : '';
}
}
if (__DEV__) {
var warnedProperties = {};
var EVENT_NAME_REGEX = /^on[A-Z]/;
var ARIA_NAME_REGEX = /^aria-/i;
var validateProperty = function(tagName, name, value, debugID) {
if (warnedProperties.hasOwnProperty(name) && warnedProperties[name]) {
return true;
}
warnedProperties[name] = true;
if (EventPluginRegistry.registrationNameModules.hasOwnProperty(name)) {
return true;
}
if (
EventPluginRegistry.plugins.length === 0 &&
EVENT_NAME_REGEX.test(name)
) {
// If no event plugins have been injected, we might be in a server environment.
// Don't check events in this case.
return true;
}
var lowerCasedName = name.toLowerCase();
var registrationName = EventPluginRegistry.possibleRegistrationNames.hasOwnProperty(
lowerCasedName,
)
? EventPluginRegistry.possibleRegistrationNames[lowerCasedName]
: null;
if (registrationName != null) {
warning(
false,
'Unknown event handler property `%s`. Did you mean `%s`?%s',
name,
registrationName,
getStackAddendum(debugID),
);
return true;
}
if (DOMProperty.isReservedProp(name)) {
return true;
}
// Let the ARIA attribute hook validate ARIA attributes
if (ARIA_NAME_REGEX.test(name)) {
return true;
}
// Known attributes should match the casing specified in the property config.
if (DOMProperty.getPossibleStandardName.hasOwnProperty(lowerCasedName)) {
var standardName = DOMProperty.getPossibleStandardName[lowerCasedName];
if (standardName !== name) {
warning(
false,
'Invalid DOM property `%s`. Did you mean `%s`?%s',
name,
standardName,
getStackAddendum(debugID),
);
}
return true;
}
return DOMProperty.shouldSetAttribute(name, value);
};
}
var warnUnknownProperties = function(type, props, debugID) {
var unknownProps = [];
for (var key in props) {
var isValid = validateProperty(type, key, props[key], debugID);
if (!isValid) {
unknownProps.push(key);
}
}
var unknownPropString = unknownProps.map(prop => '`' + prop + '`').join(', ');
if (unknownProps.length === 1) {
warning(
false,
'Invalid prop %s on <%s> tag. Either remove this prop from the element, ' +
'or pass a string, number, or boolean value to keep it in the DOM. ' +
'For details, see https://fb.me/react-unknown-prop%s',
unknownPropString,
type,
getStackAddendum(debugID),
);
} else if (unknownProps.length > 1) {
warning(
false,
'Invalid props %s on <%s> tag. Either remove these props from the element, ' +
'or pass a string, number, or boolean value to keep them in the DOM. ' +
'For details, see https://fb.me/react-unknown-prop%s',
unknownPropString,
type,
getStackAddendum(debugID),
);
}
};
function validateProperties(type, props, debugID /* Stack only */) {
if (type.indexOf('-') >= 0 || props.is) {
return;
}
warnUnknownProperties(type, props, debugID);
}
var ReactDOMUnknownPropertyHook = {
// Fiber
validateProperties,
// Stack
onBeforeMountComponent(debugID, element) {
if (__DEV__ && element != null && typeof element.type === 'string') {
validateProperties(element.type, element.props, debugID);
}
},
onBeforeUpdateComponent(debugID, element) {
if (__DEV__ && element != null && typeof element.type === 'string') {
validateProperties(element.type, element.props, debugID);
}
},
};
module.exports = ReactDOMUnknownPropertyHook;