@@ -155,39 +155,32 @@ public function process($content, array $options = [])
155155 }
156156 $ insertHTMLSources = [];
157157 $ list = []; // Save the elements into an array because removeChild() messes up the NodeList
158- foreach ($ componentElements as $ componentElement ) {
159- $ isInOtherComponentTag = false ;
158+ foreach ($ componentElements as $ index => $ componentElement ) {
160159 $ parentNode = $ componentElement ->parentNode ;
160+ $ list [$ index ] = [$ componentElement , $ parentNode , []]; // The last one will contain the parents tag names
161161 while ($ parentNode !== null && isset ($ parentNode ->tagName )) {
162- if (array_search ($ parentNode ->tagName , $ tagNames ) !== false ) {
163- $ isInOtherComponentTag = true ;
162+ $ tagName = $ parentNode ->tagName ;
163+ $ list [$ index ][2 ][] = $ tagName ;
164+ if ($ tagName === 'head ' || $ tagName === 'body ' ) {
164165 break ;
165166 }
166167 $ parentNode = $ parentNode ->parentNode ;
167168 }
168- if (!$ isInOtherComponentTag ) {
169- $ list [] = $ componentElement ;
170- }
171169 }
172- foreach ($ list as $ i => $ componentElement ) {
170+ foreach ($ list as $ index => $ componentData ) {
171+ if (!empty (array_intersect ($ componentData [2 ], $ tagNames ))) {
172+ continue ;
173+ }
174+ $ componentElement = $ componentData [0 ];
173175 $ component = $ this ->makeComponent ($ componentElement ->getAttributes (), $ componentElement ->innerHTML , $ componentElement ->tagName );
174176 $ componentResultHTML = $ getComponentResultHTML ($ component );
175- $ isInBodyTag = false ;
176- $ parentNode = $ componentElement ->parentNode ;
177- while ($ parentNode !== null && isset ($ parentNode ->tagName )) {
178- if ($ parentNode ->tagName === 'body ' ) {
179- $ isInBodyTag = true ;
180- break ;
181- }
182- $ parentNode = $ parentNode ->parentNode ;
183- }
184- if ($ isInBodyTag ) {
185- $ insertTargetName = 'html-server-components-compiler-insert-target- ' . $ i ;
186- $ componentElement ->parentNode ->insertBefore ($ domDocument ->createInsertTarget ($ insertTargetName ), $ componentElement );
187- $ componentElement ->parentNode ->removeChild ($ componentElement ); // must be before insertHTML because a duplicate elements IDs can occur.
177+ if (array_search ('body ' , $ componentData [2 ]) !== false ) {
178+ $ insertTargetName = 'html-server-components-compiler-insert-target- ' . $ index ;
179+ $ componentData [1 ]->insertBefore ($ domDocument ->createInsertTarget ($ insertTargetName ), $ componentElement );
180+ $ componentData [1 ]->removeChild ($ componentElement ); // must be before insertHTML because a duplicate elements IDs can occur.
188181 $ insertHTMLSources [] = ['source ' => $ componentResultHTML , 'target ' => $ insertTargetName ];
189182 } else {
190- $ componentElement -> parentNode ->removeChild ($ componentElement );
183+ $ componentData [ 1 ] ->removeChild ($ componentElement );
191184 $ insertHTMLSources [] = ['source ' => $ componentResultHTML ];
192185 }
193186 }
0 commit comments