Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81155 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 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
* `ReactComponent.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: ReactPerf.measure(
54
'ReactDOMIDOperations',
55
'updatePropertyByID',
56
function(id, name, value) {
57
var node = ReactMount.getNode(id);
58
invariant(
59
!INVALID_PROPERTY_ERRORS.hasOwnProperty(name),
60
'updatePropertyByID(...): %s',
61
INVALID_PROPERTY_ERRORS[name]
62
);
63
64
// If we're updating to null or undefined, we should remove the property
65
// from the DOM node instead of inadvertantly setting to a string. This
66
// brings us in line with the same behavior we have on initial render.
67
if (value != null) {
68
DOMPropertyOperations.setValueForProperty(node, name, value);
69
} else {
70
DOMPropertyOperations.deleteValueForProperty(node, name);
71
}
72
}
73
),
74
75
/**
76
* Updates a DOM node to remove a property. This should only be used to remove
77
* DOM properties in `DOMProperty`.
78
*
79
* @param {string} id ID of the node to update.
80
* @param {string} name A property name to remove, see `DOMProperty`.
81
* @internal
82
*/
83
deletePropertyByID: ReactPerf.measure(
84
'ReactDOMIDOperations',
85
'deletePropertyByID',
86
function(id, name, value) {
87
var node = ReactMount.getNode(id);
88
invariant(
89
!INVALID_PROPERTY_ERRORS.hasOwnProperty(name),
90
'updatePropertyByID(...): %s',
91
INVALID_PROPERTY_ERRORS[name]
92
);
93
DOMPropertyOperations.deleteValueForProperty(node, name, value);
94
}
95
),
96
97
/**
98
* Updates a DOM node with new style values. If a value is specified as '',
99
* the corresponding style property will be unset.
100
*
101
* @param {string} id ID of the node to update.
102
* @param {object} styles Mapping from styles to values.
103
* @internal
104
*/
105
updateStylesByID: ReactPerf.measure(
106
'ReactDOMIDOperations',
107
'updateStylesByID',
108
function(id, styles) {
109
var node = ReactMount.getNode(id);
110
CSSPropertyOperations.setValueForStyles(node, styles);
111
}
112
),
113
114
/**
115
* Updates a DOM node's innerHTML.
116
*
117
* @param {string} id ID of the node to update.
118
* @param {string} html An HTML string.
119
* @internal
120
*/
121
updateInnerHTMLByID: ReactPerf.measure(
122
'ReactDOMIDOperations',
123
'updateInnerHTMLByID',
124
function(id, html) {
125
var node = ReactMount.getNode(id);
126
setInnerHTML(node, html);
127
}
128
),
129
130
/**
131
* Updates a DOM node's text content set by `props.content`.
132
*
133
* @param {string} id ID of the node to update.
134
* @param {string} content Text content.
135
* @internal
136
*/
137
updateTextContentByID: ReactPerf.measure(
138
'ReactDOMIDOperations',
139
'updateTextContentByID',
140
function(id, content) {
141
var node = ReactMount.getNode(id);
142
DOMChildrenOperations.updateTextContent(node, content);
143
}
144
),
145
146
/**
147
* Replaces a DOM node that exists in the document with markup.
148
*
149
* @param {string} id ID of child to be replaced.
150
* @param {string} markup Dangerous markup to inject in place of child.
151
* @internal
152
* @see {Danger.dangerouslyReplaceNodeWithMarkup}
153
*/
154
dangerouslyReplaceNodeWithMarkupByID: ReactPerf.measure(
155
'ReactDOMIDOperations',
156
'dangerouslyReplaceNodeWithMarkupByID',
157
function(id, markup) {
158
var node = ReactMount.getNode(id);
159
DOMChildrenOperations.dangerouslyReplaceNodeWithMarkup(node, markup);
160
}
161
),
162
163
/**
164
* Updates a component's children by processing a series of updates.
165
*
166
* @param {array<object>} updates List of update configurations.
167
* @param {array<string>} markup List of markup strings.
168
* @internal
169
*/
170
dangerouslyProcessChildrenUpdates: ReactPerf.measure(
171
'ReactDOMIDOperations',
172
'dangerouslyProcessChildrenUpdates',
173
function(updates, markup) {
174
for (var i = 0; i < updates.length; i++) {
175
updates[i].parentNode = ReactMount.getNode(updates[i].parentID);
176
}
177
DOMChildrenOperations.processUpdates(updates, markup);
178
}
179
)
180
};
181
182
module.exports = ReactDOMIDOperations;
183
184