1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | var DOMLazyTree = require('./DOMLazyTree');
|
12 | var Danger = require('./Danger');
|
13 | var ReactDOMComponentTree = require('./ReactDOMComponentTree');
|
14 | var ReactInstrumentation = require('./ReactInstrumentation');
|
15 |
|
16 | var createMicrosoftUnsafeLocalFunction = require('./createMicrosoftUnsafeLocalFunction');
|
17 | var setInnerHTML = require('./setInnerHTML');
|
18 | var setTextContent = require('./setTextContent');
|
19 |
|
20 | function getNodeAfter(parentNode, node) {
|
21 |
|
22 |
|
23 | if (Array.isArray(node)) {
|
24 | node = node[1];
|
25 | }
|
26 | return node ? node.nextSibling : parentNode.firstChild;
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | var insertChildAt = createMicrosoftUnsafeLocalFunction(function (parentNode, childNode, referenceNode) {
|
38 |
|
39 |
|
40 |
|
41 | parentNode.insertBefore(childNode, referenceNode);
|
42 | });
|
43 |
|
44 | function insertLazyTreeChildAt(parentNode, childTree, referenceNode) {
|
45 | DOMLazyTree.insertTreeBefore(parentNode, childTree, referenceNode);
|
46 | }
|
47 |
|
48 | function moveChild(parentNode, childNode, referenceNode) {
|
49 | if (Array.isArray(childNode)) {
|
50 | moveDelimitedText(parentNode, childNode[0], childNode[1], referenceNode);
|
51 | } else {
|
52 | insertChildAt(parentNode, childNode, referenceNode);
|
53 | }
|
54 | }
|
55 |
|
56 | function removeChild(parentNode, childNode) {
|
57 | if (Array.isArray(childNode)) {
|
58 | var closingComment = childNode[1];
|
59 | childNode = childNode[0];
|
60 | removeDelimitedText(parentNode, childNode, closingComment);
|
61 | parentNode.removeChild(closingComment);
|
62 | }
|
63 | parentNode.removeChild(childNode);
|
64 | }
|
65 |
|
66 | function moveDelimitedText(parentNode, openingComment, closingComment, referenceNode) {
|
67 | var node = openingComment;
|
68 | while (true) {
|
69 | var nextNode = node.nextSibling;
|
70 | insertChildAt(parentNode, node, referenceNode);
|
71 | if (node === closingComment) {
|
72 | break;
|
73 | }
|
74 | node = nextNode;
|
75 | }
|
76 | }
|
77 |
|
78 | function removeDelimitedText(parentNode, startNode, closingComment) {
|
79 | while (true) {
|
80 | var node = startNode.nextSibling;
|
81 | if (node === closingComment) {
|
82 |
|
83 | break;
|
84 | } else {
|
85 | parentNode.removeChild(node);
|
86 | }
|
87 | }
|
88 | }
|
89 |
|
90 | function replaceDelimitedText(openingComment, closingComment, stringText) {
|
91 | var parentNode = openingComment.parentNode;
|
92 | var nodeAfterComment = openingComment.nextSibling;
|
93 | if (nodeAfterComment === closingComment) {
|
94 |
|
95 |
|
96 | if (stringText) {
|
97 | insertChildAt(parentNode, document.createTextNode(stringText), nodeAfterComment);
|
98 | }
|
99 | } else {
|
100 | if (stringText) {
|
101 |
|
102 |
|
103 | setTextContent(nodeAfterComment, stringText);
|
104 | removeDelimitedText(parentNode, nodeAfterComment, closingComment);
|
105 | } else {
|
106 | removeDelimitedText(parentNode, openingComment, closingComment);
|
107 | }
|
108 | }
|
109 |
|
110 | if (process.env.NODE_ENV !== 'production') {
|
111 | ReactInstrumentation.debugTool.onHostOperation({
|
112 | instanceID: ReactDOMComponentTree.getInstanceFromNode(openingComment)._debugID,
|
113 | type: 'replace text',
|
114 | payload: stringText
|
115 | });
|
116 | }
|
117 | }
|
118 |
|
119 | var dangerouslyReplaceNodeWithMarkup = Danger.dangerouslyReplaceNodeWithMarkup;
|
120 | if (process.env.NODE_ENV !== 'production') {
|
121 | dangerouslyReplaceNodeWithMarkup = function (oldChild, markup, prevInstance) {
|
122 | Danger.dangerouslyReplaceNodeWithMarkup(oldChild, markup);
|
123 | if (prevInstance._debugID !== 0) {
|
124 | ReactInstrumentation.debugTool.onHostOperation({
|
125 | instanceID: prevInstance._debugID,
|
126 | type: 'replace with',
|
127 | payload: markup.toString()
|
128 | });
|
129 | } else {
|
130 | var nextInstance = ReactDOMComponentTree.getInstanceFromNode(markup.node);
|
131 | if (nextInstance._debugID !== 0) {
|
132 | ReactInstrumentation.debugTool.onHostOperation({
|
133 | instanceID: nextInstance._debugID,
|
134 | type: 'mount',
|
135 | payload: markup.toString()
|
136 | });
|
137 | }
|
138 | }
|
139 | };
|
140 | }
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | var DOMChildrenOperations = {
|
146 | dangerouslyReplaceNodeWithMarkup: dangerouslyReplaceNodeWithMarkup,
|
147 |
|
148 | replaceDelimitedText: replaceDelimitedText,
|
149 |
|
150 | |
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | processUpdates: function (parentNode, updates) {
|
158 | if (process.env.NODE_ENV !== 'production') {
|
159 | var parentNodeDebugID = ReactDOMComponentTree.getInstanceFromNode(parentNode)._debugID;
|
160 | }
|
161 |
|
162 | for (var k = 0; k < updates.length; k++) {
|
163 | var update = updates[k];
|
164 | switch (update.type) {
|
165 | case 'INSERT_MARKUP':
|
166 | insertLazyTreeChildAt(parentNode, update.content, getNodeAfter(parentNode, update.afterNode));
|
167 | if (process.env.NODE_ENV !== 'production') {
|
168 | ReactInstrumentation.debugTool.onHostOperation({
|
169 | instanceID: parentNodeDebugID,
|
170 | type: 'insert child',
|
171 | payload: {
|
172 | toIndex: update.toIndex,
|
173 | content: update.content.toString()
|
174 | }
|
175 | });
|
176 | }
|
177 | break;
|
178 | case 'MOVE_EXISTING':
|
179 | moveChild(parentNode, update.fromNode, getNodeAfter(parentNode, update.afterNode));
|
180 | if (process.env.NODE_ENV !== 'production') {
|
181 | ReactInstrumentation.debugTool.onHostOperation({
|
182 | instanceID: parentNodeDebugID,
|
183 | type: 'move child',
|
184 | payload: { fromIndex: update.fromIndex, toIndex: update.toIndex }
|
185 | });
|
186 | }
|
187 | break;
|
188 | case 'SET_MARKUP':
|
189 | setInnerHTML(parentNode, update.content);
|
190 | if (process.env.NODE_ENV !== 'production') {
|
191 | ReactInstrumentation.debugTool.onHostOperation({
|
192 | instanceID: parentNodeDebugID,
|
193 | type: 'replace children',
|
194 | payload: update.content.toString()
|
195 | });
|
196 | }
|
197 | break;
|
198 | case 'TEXT_CONTENT':
|
199 | setTextContent(parentNode, update.content);
|
200 | if (process.env.NODE_ENV !== 'production') {
|
201 | ReactInstrumentation.debugTool.onHostOperation({
|
202 | instanceID: parentNodeDebugID,
|
203 | type: 'replace text',
|
204 | payload: update.content.toString()
|
205 | });
|
206 | }
|
207 | break;
|
208 | case 'REMOVE_NODE':
|
209 | removeChild(parentNode, update.fromNode);
|
210 | if (process.env.NODE_ENV !== 'production') {
|
211 | ReactInstrumentation.debugTool.onHostOperation({
|
212 | instanceID: parentNodeDebugID,
|
213 | type: 'remove child',
|
214 | payload: { fromIndex: update.fromIndex }
|
215 | });
|
216 | }
|
217 | break;
|
218 | }
|
219 | }
|
220 | }
|
221 | };
|
222 |
|
223 | module.exports = DOMChildrenOperations; |
\ | No newline at end of file |