Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80522 views
1
/**
2
* Copyright 2013-2015, 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 DOMPropertyOperations
10
* @typechecks static-only
11
*/
12
13
'use strict';
14
15
var DOMProperty = require("./DOMProperty");
16
17
var quoteAttributeValueForBrowser = require("./quoteAttributeValueForBrowser");
18
var warning = require("./warning");
19
20
function shouldIgnoreValue(name, value) {
21
return value == null ||
22
(DOMProperty.hasBooleanValue[name] && !value) ||
23
(DOMProperty.hasNumericValue[name] && isNaN(value)) ||
24
(DOMProperty.hasPositiveNumericValue[name] && (value < 1)) ||
25
(DOMProperty.hasOverloadedBooleanValue[name] && value === false);
26
}
27
28
if ("production" !== process.env.NODE_ENV) {
29
var reactProps = {
30
children: true,
31
dangerouslySetInnerHTML: true,
32
key: true,
33
ref: true
34
};
35
var warnedProperties = {};
36
37
var warnUnknownProperty = function(name) {
38
if (reactProps.hasOwnProperty(name) && reactProps[name] ||
39
warnedProperties.hasOwnProperty(name) && warnedProperties[name]) {
40
return;
41
}
42
43
warnedProperties[name] = true;
44
var lowerCasedName = name.toLowerCase();
45
46
// data-* attributes should be lowercase; suggest the lowercase version
47
var standardName = (
48
DOMProperty.isCustomAttribute(lowerCasedName) ?
49
lowerCasedName :
50
DOMProperty.getPossibleStandardName.hasOwnProperty(lowerCasedName) ?
51
DOMProperty.getPossibleStandardName[lowerCasedName] :
52
null
53
);
54
55
// For now, only warn when we have a suggested correction. This prevents
56
// logging too much when using transferPropsTo.
57
("production" !== process.env.NODE_ENV ? warning(
58
standardName == null,
59
'Unknown DOM property %s. Did you mean %s?',
60
name,
61
standardName
62
) : null);
63
64
};
65
}
66
67
/**
68
* Operations for dealing with DOM properties.
69
*/
70
var DOMPropertyOperations = {
71
72
/**
73
* Creates markup for the ID property.
74
*
75
* @param {string} id Unescaped ID.
76
* @return {string} Markup string.
77
*/
78
createMarkupForID: function(id) {
79
return DOMProperty.ID_ATTRIBUTE_NAME + '=' +
80
quoteAttributeValueForBrowser(id);
81
},
82
83
/**
84
* Creates markup for a property.
85
*
86
* @param {string} name
87
* @param {*} value
88
* @return {?string} Markup string, or null if the property was invalid.
89
*/
90
createMarkupForProperty: function(name, value) {
91
if (DOMProperty.isStandardName.hasOwnProperty(name) &&
92
DOMProperty.isStandardName[name]) {
93
if (shouldIgnoreValue(name, value)) {
94
return '';
95
}
96
var attributeName = DOMProperty.getAttributeName[name];
97
if (DOMProperty.hasBooleanValue[name] ||
98
(DOMProperty.hasOverloadedBooleanValue[name] && value === true)) {
99
return attributeName;
100
}
101
return attributeName + '=' + quoteAttributeValueForBrowser(value);
102
} else if (DOMProperty.isCustomAttribute(name)) {
103
if (value == null) {
104
return '';
105
}
106
return name + '=' + quoteAttributeValueForBrowser(value);
107
} else if ("production" !== process.env.NODE_ENV) {
108
warnUnknownProperty(name);
109
}
110
return null;
111
},
112
113
/**
114
* Sets the value for a property on a node.
115
*
116
* @param {DOMElement} node
117
* @param {string} name
118
* @param {*} value
119
*/
120
setValueForProperty: function(node, name, value) {
121
if (DOMProperty.isStandardName.hasOwnProperty(name) &&
122
DOMProperty.isStandardName[name]) {
123
var mutationMethod = DOMProperty.getMutationMethod[name];
124
if (mutationMethod) {
125
mutationMethod(node, value);
126
} else if (shouldIgnoreValue(name, value)) {
127
this.deleteValueForProperty(node, name);
128
} else if (DOMProperty.mustUseAttribute[name]) {
129
// `setAttribute` with objects becomes only `[object]` in IE8/9,
130
// ('' + value) makes it output the correct toString()-value.
131
node.setAttribute(DOMProperty.getAttributeName[name], '' + value);
132
} else {
133
var propName = DOMProperty.getPropertyName[name];
134
// Must explicitly cast values for HAS_SIDE_EFFECTS-properties to the
135
// property type before comparing; only `value` does and is string.
136
if (!DOMProperty.hasSideEffects[name] ||
137
('' + node[propName]) !== ('' + value)) {
138
// Contrary to `setAttribute`, object properties are properly
139
// `toString`ed by IE8/9.
140
node[propName] = value;
141
}
142
}
143
} else if (DOMProperty.isCustomAttribute(name)) {
144
if (value == null) {
145
node.removeAttribute(name);
146
} else {
147
node.setAttribute(name, '' + value);
148
}
149
} else if ("production" !== process.env.NODE_ENV) {
150
warnUnknownProperty(name);
151
}
152
},
153
154
/**
155
* Deletes the value for a property on a node.
156
*
157
* @param {DOMElement} node
158
* @param {string} name
159
*/
160
deleteValueForProperty: function(node, name) {
161
if (DOMProperty.isStandardName.hasOwnProperty(name) &&
162
DOMProperty.isStandardName[name]) {
163
var mutationMethod = DOMProperty.getMutationMethod[name];
164
if (mutationMethod) {
165
mutationMethod(node, undefined);
166
} else if (DOMProperty.mustUseAttribute[name]) {
167
node.removeAttribute(DOMProperty.getAttributeName[name]);
168
} else {
169
var propName = DOMProperty.getPropertyName[name];
170
var defaultValue = DOMProperty.getDefaultValueForProperty(
171
node.nodeName,
172
propName
173
);
174
if (!DOMProperty.hasSideEffects[name] ||
175
('' + node[propName]) !== defaultValue) {
176
node[propName] = defaultValue;
177
}
178
}
179
} else if (DOMProperty.isCustomAttribute(name)) {
180
node.removeAttribute(name);
181
} else if ("production" !== process.env.NODE_ENV) {
182
warnUnknownProperty(name);
183
}
184
}
185
186
};
187
188
module.exports = DOMPropertyOperations;
189
190