| 365 | }, |
| 366 | |
| 367 | Fragment(node, context) { |
| 368 | /** @type {AST.SvelteNode[][]} */ |
| 369 | const items = []; |
| 370 | |
| 371 | /** @type {AST.SvelteNode[]} */ |
| 372 | let sequence = []; |
| 373 | |
| 374 | const flush = () => { |
| 375 | items.push(sequence); |
| 376 | sequence = []; |
| 377 | }; |
| 378 | |
| 379 | for (let i = 0; i < node.nodes.length; i += 1) { |
| 380 | let child_node = node.nodes[i]; |
| 381 | |
| 382 | const prev = node.nodes[i - 1]; |
| 383 | const next = node.nodes[i + 1]; |
| 384 | |
| 385 | if (child_node.type === 'Text') { |
| 386 | child_node = { ...child_node }; // always clone, so we can safely mutate |
| 387 | |
| 388 | child_node.data = child_node.data.replace(/[^\S]+/g, ' '); |
| 389 | |
| 390 | // trim fragment |
| 391 | if (i === 0) { |
| 392 | child_node.data = child_node.data.trimStart(); |
| 393 | } |
| 394 | |
| 395 | if (i === node.nodes.length - 1) { |
| 396 | child_node.data = child_node.data.trimEnd(); |
| 397 | } |
| 398 | |
| 399 | if (child_node.data === '') { |
| 400 | continue; |
| 401 | } |
| 402 | |
| 403 | if (child_node.data.startsWith(' ') && prev && prev.type !== 'ExpressionTag') { |
| 404 | flush(); |
| 405 | child_node.data = child_node.data.trimStart(); |
| 406 | } |
| 407 | |
| 408 | if (child_node.data !== '') { |
| 409 | sequence.push({ ...child_node, data: child_node.data }); |
| 410 | |
| 411 | if (child_node.data.endsWith(' ') && next && next.type !== 'ExpressionTag') { |
| 412 | flush(); |
| 413 | child_node.data = child_node.data.trimStart(); |
| 414 | } |
| 415 | } |
| 416 | } else { |
| 417 | const is_block_element = |
| 418 | child_node.type === 'RegularElement' || |
| 419 | child_node.type === 'Component' || |
| 420 | child_node.type === 'SvelteBody' || |
| 421 | child_node.type === 'SvelteHead' || |
| 422 | child_node.type === 'SvelteFragment' || |
| 423 | child_node.type === 'SvelteBoundary' || |
| 424 | child_node.type === 'SvelteDocument' || |