All files / lib/compiler/parser/directives suspense.js

100% Statements 34/34
100% Branches 9/9
100% Functions 1/1
100% Lines 34/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 911x 911x 924x 924x 13x 13x 13x 13x 13x 13x 13x 13x 36x 12x 12x 12x 13x 13x 13x 13x 911x 911x  
/**
 * @file suspense.js
 * @description Desugars `<@suspense>` (with an optional `<@fallback>`) into DOM markers.
 */
import { escapeTemplateMarkers } from '../../../core/utils/templateUtils.js';
 
/**
 * Processes <@suspense> tags, converting them to DOM markers.
 * @param {string} template - The template string.
 * @returns {string} The processed template.
 */
export function processSuspense(template) {
  let currentTemplate = template;
  while (true) {
    const match = currentTemplate.match(/<@suspense>([\s\S]*?)<\/ ?@suspense>/i);
    if (!match) break;
 
    const fullMatch = match[0];
    const inner = match[1];
 
    // Extract <@fallback>
    let fallbackContent = '';
    let suspenseContent = inner;
    const fallbackMatch = inner.match(/<@fallback>([\s\S]*?)<\/ ?@fallback>/i);
    if (fallbackMatch) {
      fallbackContent = escapeTemplateMarkers(fallbackMatch[1]); // Escape fallback template logic
      suspenseContent = inner.replace(fallbackMatch[0], '');
    }
 
    const replacement = `<div data-ax-suspense="true"><template data-ax-fallback>${fallbackContent}</template>${suspenseContent}</div>`;
    currentTemplate = currentTemplate.replace(fullMatch, replacement);
  }
  return currentTemplate;
}