@@ -155,39 +155,32 @@ public function process($content, array $options = [])
155
155
}
156
156
$ insertHTMLSources = [];
157
157
$ 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 ) {
160
159
$ parentNode = $ componentElement ->parentNode ;
160
+ $ list [$ index ] = [$ componentElement , $ parentNode , []]; // The last one will contain the parents tag names
161
161
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 ' ) {
164
165
break ;
165
166
}
166
167
$ parentNode = $ parentNode ->parentNode ;
167
168
}
168
- if (!$ isInOtherComponentTag ) {
169
- $ list [] = $ componentElement ;
170
- }
171
169
}
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 ];
173
175
$ component = $ this ->makeComponent ($ componentElement ->getAttributes (), $ componentElement ->innerHTML , $ componentElement ->tagName );
174
176
$ 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.
188
181
$ insertHTMLSources [] = ['source ' => $ componentResultHTML , 'target ' => $ insertTargetName ];
189
182
} else {
190
- $ componentElement -> parentNode ->removeChild ($ componentElement );
183
+ $ componentData [ 1 ] ->removeChild ($ componentElement );
191
184
$ insertHTMLSources [] = ['source ' => $ componentResultHTML ];
192
185
}
193
186
}
0 commit comments