@@ -592,6 +592,9 @@ var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof win
592592 let insertionPoint = oldParent . firstChild ;
593593 let newChild ;
594594
595+ newParent . children ;
596+ oldParent . children ;
597+
595598 // run through all the new content
596599 while ( nextNewChild ) {
597600
@@ -1205,68 +1208,82 @@ function executeMorphdom(rootFromElement, rootToElement, modifiedFieldElements,
12051208 childComponents . forEach ( ( childComponent ) => {
12061209 childComponentMap . set ( childComponent . element , childComponent ) ;
12071210 } ) ;
1208- Idiomorph . morph ( rootFromElement , rootToElement , {
1209- beforeNodeMorphed : ( fromEl , toEl ) => {
1210- if ( ! ( fromEl instanceof Element ) || ! ( toEl instanceof Element ) ) {
1211- return true ;
1212- }
1213- if ( fromEl === rootFromElement ) {
1214- return true ;
1215- }
1216- if ( fromEl . hasAttribute ( 'data-live-id' ) ) {
1217- if ( fromEl . getAttribute ( 'data-live-id' ) !== toEl . getAttribute ( 'data-live-id' ) ) {
1218- for ( const child of fromEl . children ) {
1219- child . setAttribute ( 'parent-live-id-changed' , '' ) ;
1211+ Idiomorph . morph ( rootFromElement , rootToElement , { callbacks : {
1212+ beforeNodeMorphed : ( fromEl , toEl ) => {
1213+ if ( ! ( fromEl instanceof Element ) || ! ( toEl instanceof Element ) ) {
1214+ return true ;
1215+ }
1216+ if ( fromEl === rootFromElement ) {
1217+ return true ;
1218+ }
1219+ let idChanged = false ;
1220+ if ( fromEl . hasAttribute ( 'data-live-id' ) ) {
1221+ if ( fromEl . getAttribute ( 'data-live-id' ) !== toEl . getAttribute ( 'data-live-id' ) ) {
1222+ for ( const child of fromEl . children ) {
1223+ child . setAttribute ( 'parent-live-id-changed' , '' ) ;
1224+ }
1225+ idChanged = true ;
12201226 }
12211227 }
1222- }
1223- if ( fromEl instanceof HTMLElement && toEl instanceof HTMLElement ) {
1224- if ( typeof fromEl . __x !== 'undefined' ) {
1225- if ( ! window . Alpine ) {
1226- throw new Error ( 'Unable to access Alpine.js though the global window.Alpine variable. Please make sure Alpine.js is loaded before Symfony UX LiveComponent.' ) ;
1228+ if ( fromEl instanceof HTMLElement && toEl instanceof HTMLElement ) {
1229+ if ( typeof fromEl . __x !== 'undefined' ) {
1230+ if ( ! window . Alpine ) {
1231+ throw new Error ( 'Unable to access Alpine.js though the global window.Alpine variable. Please make sure Alpine.js is loaded before Symfony UX LiveComponent.' ) ;
1232+ }
1233+ if ( typeof window . Alpine . morph !== 'function' ) {
1234+ throw new Error ( 'Unable to access Alpine.js morph function. Please make sure the Alpine.js Morph plugin is installed and loaded, see https://alpinejs.dev/plugins/morph for more information.' ) ;
1235+ }
1236+ window . Alpine . morph ( fromEl . __x , toEl ) ;
12271237 }
1228- if ( typeof window . Alpine . morph !== 'function' ) {
1229- throw new Error ( 'Unable to access Alpine.js morph function. Please make sure the Alpine.js Morph plugin is installed and loaded, see https://alpinejs.dev/plugins/morph for more information.' ) ;
1238+ if ( childComponentMap . has ( fromEl ) ) {
1239+ const childComponent = childComponentMap . get ( fromEl ) ;
1240+ return ! childComponent . updateFromNewElementFromParentRender ( toEl ) && idChanged ;
1241+ }
1242+ if ( externalMutationTracker . wasElementAdded ( fromEl ) ) {
1243+ fromEl . insertAdjacentElement ( 'afterend' , toEl ) ;
1244+ return false ;
1245+ }
1246+ if ( modifiedFieldElements . includes ( fromEl ) ) {
1247+ setValueOnElement ( toEl , getElementValue ( fromEl ) ) ;
1248+ }
1249+ const elementChanges = externalMutationTracker . getChangedElement ( fromEl ) ;
1250+ if ( elementChanges ) {
1251+ elementChanges . applyToElement ( toEl ) ;
1252+ }
1253+ if ( fromEl . nodeName . toUpperCase ( ) !== 'OPTION' && fromEl . isEqualNode ( toEl ) ) {
1254+ const normalizedFromEl = cloneHTMLElement ( fromEl ) ;
1255+ normalizeAttributesForComparison ( normalizedFromEl ) ;
1256+ const normalizedToEl = cloneHTMLElement ( toEl ) ;
1257+ normalizeAttributesForComparison ( normalizedToEl ) ;
1258+ if ( normalizedFromEl . isEqualNode ( normalizedToEl ) ) {
1259+ return false ;
1260+ }
12301261 }
1231- window . Alpine . morph ( fromEl . __x , toEl ) ;
1232- }
1233- if ( childComponentMap . has ( fromEl ) ) {
1234- const childComponent = childComponentMap . get ( fromEl ) ;
1235- childComponent . updateFromNewElementFromParentRender ( toEl ) ;
1236- return false ;
12371262 }
1238- if ( modifiedFieldElements . includes ( fromEl ) ) {
1239- setValueOnElement ( toEl , getElementValue ( fromEl ) ) ;
1263+ if ( fromEl . hasAttribute ( 'parent-live-id-changed' ) ) {
1264+ fromEl . removeAttribute ( 'parent-live-id-changed' ) ;
1265+ return true ;
12401266 }
1241- const elementChanges = externalMutationTracker . getChangedElement ( fromEl ) ;
1242- if ( elementChanges ) {
1243- elementChanges . applyToElement ( toEl ) ;
1267+ return ! fromEl . hasAttribute ( 'data-live-ignore' ) ;
1268+ } ,
1269+ beforeNodeRemoved ( node ) {
1270+ if ( ! ( node instanceof HTMLElement ) ) {
1271+ return true ;
12441272 }
1245- if ( fromEl . nodeName . toUpperCase ( ) !== 'OPTION' && fromEl . isEqualNode ( toEl ) ) {
1246- const normalizedFromEl = cloneHTMLElement ( fromEl ) ;
1247- normalizeAttributesForComparison ( normalizedFromEl ) ;
1248- const normalizedToEl = cloneHTMLElement ( toEl ) ;
1249- normalizeAttributesForComparison ( normalizedToEl ) ;
1250- if ( normalizedFromEl . isEqualNode ( normalizedToEl ) ) {
1251- return false ;
1252- }
1273+ if ( externalMutationTracker . wasElementAdded ( node ) ) {
1274+ return false ;
12531275 }
1254- }
1255- if ( fromEl . hasAttribute ( 'parent-live-id-changed' ) ) {
1256- fromEl . removeAttribute ( 'parent-live-id-changed' ) ;
1257- return true ;
1258- }
1259- return ! fromEl . hasAttribute ( 'data-live-ignore' ) ;
1260- } ,
1261- beforeNodeRemoved ( node ) {
1262- if ( ! ( node instanceof HTMLElement ) ) {
1263- return true ;
1264- }
1265- if ( externalMutationTracker . wasElementAdded ( node ) ) {
1266- return false ;
1267- }
1268- return ! node . hasAttribute ( 'data-live-ignore' ) ;
1269- } ,
1276+ return ! node . hasAttribute ( 'data-live-ignore' ) ;
1277+ } ,
1278+ } } ) ;
1279+ childComponentMap . forEach ( ( childComponent , element ) => {
1280+ var _a ;
1281+ const childComponentInResult = findChildComponent ( ( _a = childComponent . id ) !== null && _a !== void 0 ? _a : '' , rootFromElement ) ;
1282+ if ( null === childComponentInResult || element === childComponentInResult ) {
1283+ return ;
1284+ }
1285+ childComponentInResult === null || childComponentInResult === void 0 ? void 0 : childComponentInResult . replaceWith ( element ) ;
1286+ childComponent . updateFromNewElementFromParentRender ( childComponentInResult ) ;
12701287 } ) ;
12711288}
12721289
0 commit comments