forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReactDOMUnknownPropertyHook.js
256 lines (228 loc) · 7.23 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule ReactDOMUnknownPropertyHook
*/
'use strict';
var DOMProperty = require('DOMProperty');
var EventPluginRegistry = require('EventPluginRegistry');
var isCustomComponent = require('isCustomComponent');
if (__DEV__) {
var warning = require('fbjs/lib/warning');
var {ReactDebugCurrentFrame} = require('ReactGlobalSharedState');
}
function getStackAddendum() {
var stack = ReactDebugCurrentFrame.getStackAddendum();
return stack != null ? stack : '';
}
if (__DEV__) {
var warnedProperties = {};
var hasOwnProperty = Object.prototype.hasOwnProperty;
var EVENT_NAME_REGEX = /^on[A-Z]/;
var rARIA = new RegExp('^(aria)-[' + DOMProperty.ATTRIBUTE_NAME_CHAR + ']*$');
var rARIACamel = new RegExp(
'^(aria)[A-Z][' + DOMProperty.ATTRIBUTE_NAME_CHAR + ']*$',
);
var possibleStandardNames = require('possibleStandardNames');
var validateProperty = function(tagName, name, value) {
if (hasOwnProperty.call(warnedProperties, name) && warnedProperties[name]) {
return 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,
'Invalid event handler property `%s`. Did you mean `%s`?%s',
name,
registrationName,
getStackAddendum(),
);
warnedProperties[name] = true;
return true;
}
if (lowerCasedName.indexOf('on') === 0 && lowerCasedName !== 'on') {
warning(
false,
'Unknown event handler property `%s`. It will be ignored.%s',
name,
getStackAddendum(),
);
warnedProperties[name] = true;
return true;
}
// Let the ARIA attribute hook validate ARIA attributes
if (rARIA.test(name) || rARIACamel.test(name)) {
return true;
}
if (lowerCasedName === 'onfocusin' || lowerCasedName === 'onfocusout') {
warning(
false,
'React uses onFocus and onBlur instead of onFocusIn and onFocusOut. ' +
'All React events are normalized to bubble, so onFocusIn and onFocusOut ' +
'are not needed/supported by React.',
);
warnedProperties[name] = true;
return true;
}
if (lowerCasedName === 'innerhtml') {
warning(
false,
'Directly setting property `innerHTML` is not permitted. ' +
'For more information, lookup documentation on `dangerouslySetInnerHTML`.',
);
warnedProperties[name] = true;
return true;
}
if (lowerCasedName === 'aria') {
warning(
false,
'The `aria` attribute is reserved for future use in React. ' +
'Pass individual `aria-` attributes instead.',
);
warnedProperties[name] = true;
return true;
}
if (
lowerCasedName === 'is' &&
value !== null &&
value !== undefined &&
typeof value !== 'string'
) {
warning(
false,
'Received a `%s` for string attribute `is`. If this is expected, cast ' +
'the value to a string.%s',
typeof value,
getStackAddendum(),
);
warnedProperties[name] = true;
return true;
}
if (typeof value === 'number' && isNaN(value)) {
warning(
false,
'Received NaN for numeric attribute `%s`. If this is expected, cast ' +
'the value to a string.%s',
name,
getStackAddendum(),
);
warnedProperties[name] = true;
return true;
}
const isReserved = DOMProperty.isReservedProp(name);
// Known attributes should match the casing specified in the property config.
if (possibleStandardNames.hasOwnProperty(lowerCasedName)) {
var standardName = possibleStandardNames[lowerCasedName];
if (standardName !== name) {
warning(
false,
'Invalid DOM property `%s`. Did you mean `%s`?%s',
name,
standardName,
getStackAddendum(),
);
warnedProperties[name] = true;
return true;
}
} else if (!isReserved && name !== lowerCasedName) {
// Unknown attributes should have lowercase casing since that's how they
// will be cased anyway with server rendering.
warning(
false,
'React does not recognize the `%s` prop on a DOM element. If you ' +
'intentionally want it to appear in the DOM as a custom ' +
'attribute, spell it as lowercase `%s` instead. ' +
'If you accidentally passed it from a parent component, remove ' +
'it from the DOM element.%s',
name,
lowerCasedName,
getStackAddendum(),
);
warnedProperties[name] = true;
return true;
}
if (typeof value === 'boolean') {
warning(
DOMProperty.shouldAttributeAcceptBooleanValue(name),
'Received `%s` for non-boolean attribute `%s`. If this is expected, cast ' +
'the value to a string.%s',
value,
name,
getStackAddendum(),
);
warnedProperties[name] = true;
return true;
}
// Now that we've validated casing, do not validate
// data types for reserved props
if (isReserved) {
return true;
}
// Warn when a known attribute is a bad type
if (!DOMProperty.shouldSetAttribute(name, value)) {
warnedProperties[name] = true;
return false;
}
return true;
};
}
var warnUnknownProperties = function(type, props) {
var unknownProps = [];
for (var key in props) {
var isValid = validateProperty(type, key, props[key]);
if (!isValid) {
unknownProps.push(key);
}
}
var unknownPropString = unknownProps.map(prop => '`' + prop + '`').join(', ');
if (unknownProps.length === 1) {
warning(
false,
'Invalid value for prop %s on <%s> tag. Either remove it from the element, ' +
'or pass a string or number value to keep it in the DOM. ' +
'For details, see https://fb.me/react-attribute-behavior%s',
unknownPropString,
type,
getStackAddendum(),
);
} else if (unknownProps.length > 1) {
warning(
false,
'Invalid values for props %s on <%s> tag. Either remove them from the element, ' +
'or pass a string or number value to keep them in the DOM. ' +
'For details, see https://fb.me/react-attribute-behavior%s',
unknownPropString,
type,
getStackAddendum(),
);
}
};
function validateProperties(type, props) {
if (isCustomComponent(type, props)) {
return;
}
warnUnknownProperties(type, props);
}
var ReactDOMUnknownPropertyHook = {
validateProperties,
};
module.exports = ReactDOMUnknownPropertyHook;