@@ -269,8 +269,10 @@ export function processChildrenAsStatement(
269269 return createBlockStatement ( childContext . body )
270270}
271271
272- const isStaticElement = ( c : TemplateChildNode ) : boolean =>
273- c . type === NodeTypes . ELEMENT && c . tagType !== ElementTypes . COMPONENT
272+ const isStaticChildNode = ( c : TemplateChildNode ) : boolean =>
273+ ( c . type === NodeTypes . ELEMENT && c . tagType !== ElementTypes . COMPONENT ) ||
274+ c . type === NodeTypes . TEXT ||
275+ c . type === NodeTypes . COMMENT
274276
275277/**
276278 * Check if a node should be processed as dynamic.
@@ -289,19 +291,21 @@ function shouldProcessAsDynamic(
289291 node : TemplateChildNode ,
290292) : boolean {
291293 // 1. Must be a dynamic node type
292- if ( isStaticElement ( node ) ) return false
294+ if ( isStaticChildNode ( node ) ) return false
293295 // 2. Must be inside a parent element
294296 if ( ! parent . tag ) return false
295297
296- const children = parent . children
298+ const children = parent . children . filter (
299+ child => ! ( child . type === NodeTypes . TEXT && ! child . content . trim ( ) ) ,
300+ )
297301 const len = children . length
298302 const index = children . indexOf ( node )
299303
300304 // 3. Check for a static previous sibling
301305 let hasStaticPreviousSibling = false
302306 if ( index > 0 ) {
303307 for ( let i = index - 1 ; i >= 0 ; i -- ) {
304- if ( isStaticElement ( children [ i ] ) ) {
308+ if ( isStaticChildNode ( children [ i ] ) ) {
305309 hasStaticPreviousSibling = true
306310 break
307311 }
@@ -313,27 +317,47 @@ function shouldProcessAsDynamic(
313317 let hasStaticNextSibling = false
314318 if ( index > - 1 && index < len - 1 ) {
315319 for ( let i = index + 1 ; i < len ; i ++ ) {
316- if ( isStaticElement ( children [ i ] ) ) {
320+ if ( isStaticChildNode ( children [ i ] ) ) {
317321 hasStaticNextSibling = true
318322 break
319323 }
320324 }
321325 }
322326 if ( ! hasStaticNextSibling ) return false
323327
324- // 5. Check for a consecutive dynamic sibling (immediately before or after)
325- let hasConsecutiveDynamicNodes = false
326- if ( index > 0 && ! isStaticElement ( children [ index - 1 ] ) ) {
327- hasConsecutiveDynamicNodes = true
328+ // 5. Calculate the number and location of continuous dynamic nodes
329+ let dynamicNodeCount = 1 // The current node is counted as one
330+ let prevDynamicCount = 0
331+ let nextDynamicCount = 0
332+
333+ // Count consecutive dynamic nodes forward
334+ for ( let i = index - 1 ; i >= 0 ; i -- ) {
335+ if ( ! isStaticChildNode ( children [ i ] ) ) {
336+ prevDynamicCount ++
337+ } else {
338+ break
339+ }
340+ }
341+
342+ // Count consecutive dynamic nodes backwards
343+ for ( let i = index + 1 ; i < len ; i ++ ) {
344+ if ( ! isStaticChildNode ( children [ i ] ) ) {
345+ nextDynamicCount ++
346+ } else {
347+ break
348+ }
349+ }
350+
351+ dynamicNodeCount = 1 + prevDynamicCount + nextDynamicCount
352+
353+ // For two consecutive dynamic nodes, mark both as dynamic
354+ if ( dynamicNodeCount === 2 ) {
355+ return prevDynamicCount > 0 || nextDynamicCount > 0
328356 }
329- if (
330- ! hasConsecutiveDynamicNodes &&
331- index < len - 1 &&
332- ! isStaticElement ( children [ index + 1 ] )
333- ) {
334- hasConsecutiveDynamicNodes = true
357+ // For three or more dynamic nodes, only mark the intermediate nodes as dynamic
358+ else if ( dynamicNodeCount >= 3 ) {
359+ return prevDynamicCount > 0 && nextDynamicCount > 0
335360 }
336361
337- // Only process as dynamic if all conditions are met
338- return hasConsecutiveDynamicNodes
362+ return false
339363}
0 commit comments