Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81159 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule DOMProperty
10
* @typechecks static-only
11
*/
12
13
/*jslint bitwise: true */
14
15
"use strict";
16
17
var invariant = require('invariant');
18
19
function checkMask(value, bitmask) {
20
return (value & bitmask) === bitmask;
21
}
22
23
var DOMPropertyInjection = {
24
/**
25
* Mapping from normalized, camelcased property names to a configuration that
26
* specifies how the associated DOM property should be accessed or rendered.
27
*/
28
MUST_USE_ATTRIBUTE: 0x1,
29
MUST_USE_PROPERTY: 0x2,
30
HAS_SIDE_EFFECTS: 0x4,
31
HAS_BOOLEAN_VALUE: 0x8,
32
HAS_NUMERIC_VALUE: 0x10,
33
HAS_POSITIVE_NUMERIC_VALUE: 0x20 | 0x10,
34
HAS_OVERLOADED_BOOLEAN_VALUE: 0x40,
35
36
/**
37
* Inject some specialized knowledge about the DOM. This takes a config object
38
* with the following properties:
39
*
40
* isCustomAttribute: function that given an attribute name will return true
41
* if it can be inserted into the DOM verbatim. Useful for data-* or aria-*
42
* attributes where it's impossible to enumerate all of the possible
43
* attribute names,
44
*
45
* Properties: object mapping DOM property name to one of the
46
* DOMPropertyInjection constants or null. If your attribute isn't in here,
47
* it won't get written to the DOM.
48
*
49
* DOMAttributeNames: object mapping React attribute name to the DOM
50
* attribute name. Attribute names not specified use the **lowercase**
51
* normalized name.
52
*
53
* DOMPropertyNames: similar to DOMAttributeNames but for DOM properties.
54
* Property names not specified use the normalized name.
55
*
56
* DOMMutationMethods: Properties that require special mutation methods. If
57
* `value` is undefined, the mutation method should unset the property.
58
*
59
* @param {object} domPropertyConfig the config as described above.
60
*/
61
injectDOMPropertyConfig: function(domPropertyConfig) {
62
var Properties = domPropertyConfig.Properties || {};
63
var DOMAttributeNames = domPropertyConfig.DOMAttributeNames || {};
64
var DOMPropertyNames = domPropertyConfig.DOMPropertyNames || {};
65
var DOMMutationMethods = domPropertyConfig.DOMMutationMethods || {};
66
67
if (domPropertyConfig.isCustomAttribute) {
68
DOMProperty._isCustomAttributeFunctions.push(
69
domPropertyConfig.isCustomAttribute
70
);
71
}
72
73
for (var propName in Properties) {
74
invariant(
75
!DOMProperty.isStandardName.hasOwnProperty(propName),
76
'injectDOMPropertyConfig(...): You\'re trying to inject DOM property ' +
77
'\'%s\' which has already been injected. You may be accidentally ' +
78
'injecting the same DOM property config twice, or you may be ' +
79
'injecting two configs that have conflicting property names.',
80
propName
81
);
82
83
DOMProperty.isStandardName[propName] = true;
84
85
var lowerCased = propName.toLowerCase();
86
DOMProperty.getPossibleStandardName[lowerCased] = propName;
87
88
if (DOMAttributeNames.hasOwnProperty(propName)) {
89
var attributeName = DOMAttributeNames[propName];
90
DOMProperty.getPossibleStandardName[attributeName] = propName;
91
DOMProperty.getAttributeName[propName] = attributeName;
92
} else {
93
DOMProperty.getAttributeName[propName] = lowerCased;
94
}
95
96
DOMProperty.getPropertyName[propName] =
97
DOMPropertyNames.hasOwnProperty(propName) ?
98
DOMPropertyNames[propName] :
99
propName;
100
101
if (DOMMutationMethods.hasOwnProperty(propName)) {
102
DOMProperty.getMutationMethod[propName] = DOMMutationMethods[propName];
103
} else {
104
DOMProperty.getMutationMethod[propName] = null;
105
}
106
107
var propConfig = Properties[propName];
108
DOMProperty.mustUseAttribute[propName] =
109
checkMask(propConfig, DOMPropertyInjection.MUST_USE_ATTRIBUTE);
110
DOMProperty.mustUseProperty[propName] =
111
checkMask(propConfig, DOMPropertyInjection.MUST_USE_PROPERTY);
112
DOMProperty.hasSideEffects[propName] =
113
checkMask(propConfig, DOMPropertyInjection.HAS_SIDE_EFFECTS);
114
DOMProperty.hasBooleanValue[propName] =
115
checkMask(propConfig, DOMPropertyInjection.HAS_BOOLEAN_VALUE);
116
DOMProperty.hasNumericValue[propName] =
117
checkMask(propConfig, DOMPropertyInjection.HAS_NUMERIC_VALUE);
118
DOMProperty.hasPositiveNumericValue[propName] =
119
checkMask(propConfig, DOMPropertyInjection.HAS_POSITIVE_NUMERIC_VALUE);
120
DOMProperty.hasOverloadedBooleanValue[propName] =
121
checkMask(propConfig, DOMPropertyInjection.HAS_OVERLOADED_BOOLEAN_VALUE);
122
123
invariant(
124
!DOMProperty.mustUseAttribute[propName] ||
125
!DOMProperty.mustUseProperty[propName],
126
'DOMProperty: Cannot require using both attribute and property: %s',
127
propName
128
);
129
invariant(
130
DOMProperty.mustUseProperty[propName] ||
131
!DOMProperty.hasSideEffects[propName],
132
'DOMProperty: Properties that have side effects must use property: %s',
133
propName
134
);
135
invariant(
136
!!DOMProperty.hasBooleanValue[propName] +
137
!!DOMProperty.hasNumericValue[propName] +
138
!!DOMProperty.hasOverloadedBooleanValue[propName] <= 1,
139
'DOMProperty: Value can be one of boolean, overloaded boolean, or ' +
140
'numeric value, but not a combination: %s',
141
propName
142
);
143
}
144
}
145
};
146
var defaultValueCache = {};
147
148
/**
149
* DOMProperty exports lookup objects that can be used like functions:
150
*
151
* > DOMProperty.isValid['id']
152
* true
153
* > DOMProperty.isValid['foobar']
154
* undefined
155
*
156
* Although this may be confusing, it performs better in general.
157
*
158
* @see http://jsperf.com/key-exists
159
* @see http://jsperf.com/key-missing
160
*/
161
var DOMProperty = {
162
163
ID_ATTRIBUTE_NAME: 'data-reactid',
164
165
/**
166
* Checks whether a property name is a standard property.
167
* @type {Object}
168
*/
169
isStandardName: {},
170
171
/**
172
* Mapping from lowercase property names to the properly cased version, used
173
* to warn in the case of missing properties.
174
* @type {Object}
175
*/
176
getPossibleStandardName: {},
177
178
/**
179
* Mapping from normalized names to attribute names that differ. Attribute
180
* names are used when rendering markup or with `*Attribute()`.
181
* @type {Object}
182
*/
183
getAttributeName: {},
184
185
/**
186
* Mapping from normalized names to properties on DOM node instances.
187
* (This includes properties that mutate due to external factors.)
188
* @type {Object}
189
*/
190
getPropertyName: {},
191
192
/**
193
* Mapping from normalized names to mutation methods. This will only exist if
194
* mutation cannot be set simply by the property or `setAttribute()`.
195
* @type {Object}
196
*/
197
getMutationMethod: {},
198
199
/**
200
* Whether the property must be accessed and mutated as an object property.
201
* @type {Object}
202
*/
203
mustUseAttribute: {},
204
205
/**
206
* Whether the property must be accessed and mutated using `*Attribute()`.
207
* (This includes anything that fails `<propName> in <element>`.)
208
* @type {Object}
209
*/
210
mustUseProperty: {},
211
212
/**
213
* Whether or not setting a value causes side effects such as triggering
214
* resources to be loaded or text selection changes. We must ensure that
215
* the value is only set if it has changed.
216
* @type {Object}
217
*/
218
hasSideEffects: {},
219
220
/**
221
* Whether the property should be removed when set to a falsey value.
222
* @type {Object}
223
*/
224
hasBooleanValue: {},
225
226
/**
227
* Whether the property must be numeric or parse as a
228
* numeric and should be removed when set to a falsey value.
229
* @type {Object}
230
*/
231
hasNumericValue: {},
232
233
/**
234
* Whether the property must be positive numeric or parse as a positive
235
* numeric and should be removed when set to a falsey value.
236
* @type {Object}
237
*/
238
hasPositiveNumericValue: {},
239
240
/**
241
* Whether the property can be used as a flag as well as with a value. Removed
242
* when strictly equal to false; present without a value when strictly equal
243
* to true; present with a value otherwise.
244
* @type {Object}
245
*/
246
hasOverloadedBooleanValue: {},
247
248
/**
249
* All of the isCustomAttribute() functions that have been injected.
250
*/
251
_isCustomAttributeFunctions: [],
252
253
/**
254
* Checks whether a property name is a custom attribute.
255
* @method
256
*/
257
isCustomAttribute: function(attributeName) {
258
for (var i = 0; i < DOMProperty._isCustomAttributeFunctions.length; i++) {
259
var isCustomAttributeFn = DOMProperty._isCustomAttributeFunctions[i];
260
if (isCustomAttributeFn(attributeName)) {
261
return true;
262
}
263
}
264
return false;
265
},
266
267
/**
268
* Returns the default property value for a DOM property (i.e., not an
269
* attribute). Most default values are '' or false, but not all. Worse yet,
270
* some (in particular, `type`) vary depending on the type of element.
271
*
272
* TODO: Is it better to grab all the possible properties when creating an
273
* element to avoid having to create the same element twice?
274
*/
275
getDefaultValueForProperty: function(nodeName, prop) {
276
var nodeDefaults = defaultValueCache[nodeName];
277
var testElement;
278
if (!nodeDefaults) {
279
defaultValueCache[nodeName] = nodeDefaults = {};
280
}
281
if (!(prop in nodeDefaults)) {
282
testElement = document.createElement(nodeName);
283
nodeDefaults[prop] = testElement[prop];
284
}
285
return nodeDefaults[prop];
286
},
287
288
injection: DOMPropertyInjection
289
};
290
291
module.exports = DOMProperty;
292
293