@@ -18,7 +18,10 @@ var ReactPerf = require('ReactPerf');
18
18
19
19
var setInnerHTML = require ( 'setInnerHTML' ) ;
20
20
var setTextContent = require ( 'setTextContent' ) ;
21
- var invariant = require ( 'invariant' ) ;
21
+
22
+ function getNodeAfter ( parentNode , node ) {
23
+ return node ? node . nextSibling : parentNode . firstChild ;
24
+ }
22
25
23
26
/**
24
27
* Inserts `childNode` as a child of `parentNode` at the `index`.
@@ -28,27 +31,14 @@ var invariant = require('invariant');
28
31
* @param {number } index Index at which to insert the child.
29
32
* @internal
30
33
*/
31
- function insertChildAt ( parentNode , childNode , index ) {
32
- // We can rely exclusively on `insertBefore(node, null)` instead of also using
34
+ function insertChildAt ( parentNode , childNode , referenceNode ) {
35
+ // We rely exclusively on `insertBefore(node, null)` instead of also using
33
36
// `appendChild(node)`. (Using `undefined` is not allowed by all browsers so
34
37
// we are careful to use `null`.)
35
-
36
- // In Safari, .childNodes[index] can return a DOM node with id={index} so we
37
- // use .item() instead which is immune to this bug. (See #3560.) In contrast
38
- // to the spec, IE8 throws an error if index is larger than the list size.
39
- var referenceNode =
40
- index < parentNode . childNodes . length ?
41
- parentNode . childNodes . item ( index ) : null ;
42
-
43
38
parentNode . insertBefore ( childNode , referenceNode ) ;
44
39
}
45
40
46
- function insertLazyTreeChildAt ( parentNode , childTree , index ) {
47
- // See above.
48
- var referenceNode =
49
- index < parentNode . childNodes . length ?
50
- parentNode . childNodes . item ( index ) : null ;
51
-
41
+ function insertLazyTreeChildAt ( parentNode , childTree , referenceNode ) {
52
42
DOMLazyTree . insertTreeBefore ( parentNode , childTree , referenceNode ) ;
53
43
}
54
44
@@ -69,81 +59,21 @@ var DOMChildrenOperations = {
69
59
* @internal
70
60
*/
71
61
processUpdates : function ( parentNode , updates ) {
72
- var update ;
73
- // Mapping from parent IDs to initial child orderings.
74
- var initialChildren = null ;
75
- // List of children that will be moved or removed.
76
- var updatedChildren = null ;
77
-
78
- var markupList = null ;
79
-
80
- for ( var i = 0 ; i < updates . length ; i ++ ) {
81
- update = updates [ i ] ;
82
- if ( update . type === ReactMultiChildUpdateTypes . MOVE_EXISTING ||
83
- update . type === ReactMultiChildUpdateTypes . REMOVE_NODE ) {
84
- var updatedIndex = update . fromIndex ;
85
- var updatedChild = parentNode . childNodes [ updatedIndex ] ;
86
-
87
- invariant (
88
- updatedChild ,
89
- 'processUpdates(): Unable to find child %s of element %s. This ' +
90
- 'probably means the DOM was unexpectedly mutated (e.g., by the ' +
91
- 'browser), usually due to forgetting a <tbody> when using tables, ' +
92
- 'nesting tags like <form>, <p>, or <a>, or using non-SVG elements ' +
93
- 'in an <svg> parent.' ,
94
- updatedIndex ,
95
- parentNode ,
96
- ) ;
97
-
98
- initialChildren = initialChildren || { } ;
99
- initialChildren [ updatedIndex ] = updatedChild ;
100
-
101
- updatedChildren = updatedChildren || [ ] ;
102
- updatedChildren . push ( updatedChild ) ;
103
- } else if ( update . type === ReactMultiChildUpdateTypes . INSERT_MARKUP ) {
104
- // Replace each HTML string with an index into the markup list
105
- if ( typeof update . content === 'string' ) {
106
- markupList = markupList || [ ] ;
107
- update . content = markupList . push ( update . markup ) ;
108
- }
109
- }
110
- }
111
-
112
- var renderedMarkup ;
113
- if ( markupList ) {
114
- renderedMarkup = Danger . dangerouslyRenderMarkup ( markupList ) ;
115
- }
116
-
117
- // Remove updated children first so that `toIndex` is consistent.
118
- if ( updatedChildren ) {
119
- for ( var j = 0 ; j < updatedChildren . length ; j ++ ) {
120
- parentNode . removeChild ( updatedChildren [ j ] ) ;
121
- }
122
- }
123
-
124
62
for ( var k = 0 ; k < updates . length ; k ++ ) {
125
- update = updates [ k ] ;
63
+ var update = updates [ k ] ;
126
64
switch ( update . type ) {
127
65
case ReactMultiChildUpdateTypes . INSERT_MARKUP :
128
- if ( renderedMarkup ) {
129
- insertChildAt (
130
- parentNode ,
131
- renderedMarkup [ update . content ] ,
132
- update . toIndex
133
- ) ;
134
- } else {
135
- insertLazyTreeChildAt (
136
- parentNode ,
137
- update . content ,
138
- update . toIndex
139
- ) ;
140
- }
66
+ insertLazyTreeChildAt (
67
+ parentNode ,
68
+ update . content ,
69
+ getNodeAfter ( parentNode , update . afterNode )
70
+ ) ;
141
71
break ;
142
72
case ReactMultiChildUpdateTypes . MOVE_EXISTING :
143
73
insertChildAt (
144
74
parentNode ,
145
- initialChildren [ update . fromIndex ] ,
146
- update . toIndex
75
+ update . fromNode ,
76
+ getNodeAfter ( parentNode , update . afterNode )
147
77
) ;
148
78
break ;
149
79
case ReactMultiChildUpdateTypes . SET_MARKUP :
@@ -159,7 +89,7 @@ var DOMChildrenOperations = {
159
89
) ;
160
90
break ;
161
91
case ReactMultiChildUpdateTypes . REMOVE_NODE :
162
- // Already removed by the for-loop above.
92
+ parentNode . removeChild ( update . fromNode ) ;
163
93
break ;
164
94
}
165
95
}
0 commit comments