Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80517 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 ReactDOMIDOperations
10
* @typechecks static-only
11
*/
12
13
/*jslint evil: true */
14
15
'use strict';
16
17
var CSSPropertyOperations = require("./CSSPropertyOperations");
18
var DOMChildrenOperations = require("./DOMChildrenOperations");
19
var DOMPropertyOperations = require("./DOMPropertyOperations");
20
var ReactMount = require("./ReactMount");
21
var ReactPerf = require("./ReactPerf");
22
23
var invariant = require("./invariant");
24
var setInnerHTML = require("./setInnerHTML");
25
26
/**
27
* Errors for properties that should not be updated with `updatePropertyById()`.
28
*
29
* @type {object}
30
* @private
31
*/
32
var INVALID_PROPERTY_ERRORS = {
33
dangerouslySetInnerHTML:
34
'`dangerouslySetInnerHTML` must be set using `updateInnerHTMLByID()`.',
35
style: '`style` must be set using `updateStylesByID()`.'
36
};
37
38
/**
39
* Operations used to process updates to DOM nodes. This is made injectable via
40
* `ReactDOMComponent.BackendIDOperations`.
41
*/
42
var ReactDOMIDOperations = {
43
44
/**
45
* Updates a DOM node with new property values. This should only be used to
46
* update DOM properties in `DOMProperty`.
47
*
48
* @param {string} id ID of the node to update.
49
* @param {string} name A valid property name, see `DOMProperty`.
50
* @param {*} value New value of the property.
51
* @internal
52
*/
53
updatePropertyByID: function(id, name, value) {
54
var node = ReactMount.getNode(id);
55
("production" !== process.env.NODE_ENV ? invariant(
56
!INVALID_PROPERTY_ERRORS.hasOwnProperty(name),
57
'updatePropertyByID(...): %s',
58
INVALID_PROPERTY_ERRORS[name]
59
) : invariant(!INVALID_PROPERTY_ERRORS.hasOwnProperty(name)));
60
61
// If we're updating to null or undefined, we should remove the property
62
// from the DOM node instead of inadvertantly setting to a string. This
63
// brings us in line with the same behavior we have on initial render.
64
if (value != null) {
65
DOMPropertyOperations.setValueForProperty(node, name, value);
66
} else {
67
DOMPropertyOperations.deleteValueForProperty(node, name);
68
}
69
},
70
71
/**
72
* Updates a DOM node to remove a property. This should only be used to remove
73
* DOM properties in `DOMProperty`.
74
*
75
* @param {string} id ID of the node to update.
76
* @param {string} name A property name to remove, see `DOMProperty`.
77
* @internal
78
*/
79
deletePropertyByID: function(id, name, value) {
80
var node = ReactMount.getNode(id);
81
("production" !== process.env.NODE_ENV ? invariant(
82
!INVALID_PROPERTY_ERRORS.hasOwnProperty(name),
83
'updatePropertyByID(...): %s',
84
INVALID_PROPERTY_ERRORS[name]
85
) : invariant(!INVALID_PROPERTY_ERRORS.hasOwnProperty(name)));
86
DOMPropertyOperations.deleteValueForProperty(node, name, value);
87
},
88
89
/**
90
* Updates a DOM node with new style values. If a value is specified as '',
91
* the corresponding style property will be unset.
92
*
93
* @param {string} id ID of the node to update.
94
* @param {object} styles Mapping from styles to values.
95
* @internal
96
*/
97
updateStylesByID: function(id, styles) {
98
var node = ReactMount.getNode(id);
99
CSSPropertyOperations.setValueForStyles(node, styles);
100
},
101
102
/**
103
* Updates a DOM node's innerHTML.
104
*
105
* @param {string} id ID of the node to update.
106
* @param {string} html An HTML string.
107
* @internal
108
*/
109
updateInnerHTMLByID: function(id, html) {
110
var node = ReactMount.getNode(id);
111
setInnerHTML(node, html);
112
},
113
114
/**
115
* Updates a DOM node's text content set by `props.content`.
116
*
117
* @param {string} id ID of the node to update.
118
* @param {string} content Text content.
119
* @internal
120
*/
121
updateTextContentByID: function(id, content) {
122
var node = ReactMount.getNode(id);
123
DOMChildrenOperations.updateTextContent(node, content);
124
},
125
126
/**
127
* Replaces a DOM node that exists in the document with markup.
128
*
129
* @param {string} id ID of child to be replaced.
130
* @param {string} markup Dangerous markup to inject in place of child.
131
* @internal
132
* @see {Danger.dangerouslyReplaceNodeWithMarkup}
133
*/
134
dangerouslyReplaceNodeWithMarkupByID: function(id, markup) {
135
var node = ReactMount.getNode(id);
136
DOMChildrenOperations.dangerouslyReplaceNodeWithMarkup(node, markup);
137
},
138
139
/**
140
* Updates a component's children by processing a series of updates.
141
*
142
* @param {array<object>} updates List of update configurations.
143
* @param {array<string>} markup List of markup strings.
144
* @internal
145
*/
146
dangerouslyProcessChildrenUpdates: function(updates, markup) {
147
for (var i = 0; i < updates.length; i++) {
148
updates[i].parentNode = ReactMount.getNode(updates[i].parentID);
149
}
150
DOMChildrenOperations.processUpdates(updates, markup);
151
}
152
};
153
154
ReactPerf.measureMethods(ReactDOMIDOperations, 'ReactDOMIDOperations', {
155
updatePropertyByID: 'updatePropertyByID',
156
deletePropertyByID: 'deletePropertyByID',
157
updateStylesByID: 'updateStylesByID',
158
updateInnerHTMLByID: 'updateInnerHTMLByID',
159
updateTextContentByID: 'updateTextContentByID',
160
dangerouslyReplaceNodeWithMarkupByID: 'dangerouslyReplaceNodeWithMarkupByID',
161
dangerouslyProcessChildrenUpdates: 'dangerouslyProcessChildrenUpdates'
162
});
163
164
module.exports = ReactDOMIDOperations;
165
166