66 INSERT_VNODE ,
77 MATCHED ,
88 UNDEFINED ,
9- NULL
9+ NULL ,
10+ HAS_MOVE_BEFORE_SUPPORT
1011} from '../constants' ;
1112import { isArray } from '../util' ;
1213import { getDomSibling } from '../component' ;
@@ -126,8 +127,9 @@ export function diffChildren(
126127 }
127128
128129 let shouldPlace = childVNode . _flags & INSERT_VNODE ;
130+ const isMounting = oldVNode == NULL || oldVNode . _original == NULL ;
129131 if ( shouldPlace || oldVNode . _children === childVNode . _children ) {
130- oldDom = insert ( childVNode , oldDom , parentDom , shouldPlace ) ;
132+ oldDom = insert ( childVNode , oldDom , parentDom , shouldPlace , isMounting ) ;
131133
132134 // When a matched VNode is physically moved via INSERT_VNODE, its old
133135 // _dom pointer becomes a stale positional reference. Clear it so that
@@ -344,9 +346,10 @@ function constructNewChildrenArray(
344346 * @param {PreactElement } oldDom
345347 * @param {PreactElement } parentDom
346348 * @param {number } shouldPlace
349+ * @param {boolean } isMounting
347350 * @returns {PreactElement }
348351 */
349- function insert ( parentVNode , oldDom , parentDom , shouldPlace ) {
352+ function insert ( parentVNode , oldDom , parentDom , shouldPlace , isMounting ) {
350353 // Note: VNodes in nested suspended trees may be missing _children.
351354 if ( typeof parentVNode . type == 'function' ) {
352355 let children = parentVNode . _children ;
@@ -357,7 +360,7 @@ function insert(parentVNode, oldDom, parentDom, shouldPlace) {
357360 // children's _parent pointer to point to the newVNode (parentVNode
358361 // here).
359362 children [ i ] . _parent = parentVNode ;
360- oldDom = insert ( children [ i ] , oldDom , parentDom , shouldPlace ) ;
363+ oldDom = insert ( children [ i ] , oldDom , parentDom , shouldPlace , false ) ;
361364 }
362365 }
363366
@@ -367,7 +370,13 @@ function insert(parentVNode, oldDom, parentDom, shouldPlace) {
367370 if ( oldDom && parentVNode . type && ! oldDom . parentNode ) {
368371 oldDom = getDomSibling ( parentVNode ) ;
369372 }
370- parentDom . insertBefore ( parentVNode . _dom , oldDom || NULL ) ;
373+
374+ if ( HAS_MOVE_BEFORE_SUPPORT && ! isMounting ) {
375+ // @ts -expect-error This isn't added to TypeScript lib.d.ts yet
376+ parentDom . moveBefore ( parentVNode . _dom , oldDom ) ;
377+ } else {
378+ parentDom . insertBefore ( parentVNode . _dom , oldDom || NULL ) ;
379+ }
371380 }
372381 oldDom = parentVNode . _dom ;
373382 }
0 commit comments