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

100% Statements 47/47
94.73% Branches 18/19
100% Functions 1/1
100% Lines 47/47

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 35 36 37 38 39 40 41 42 43 44 45 46 47 48532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 904x 897x 897x 897x 897x 897x 897x 897x 897x 897x 897x 904x 155221x 48883x 43x 43x 44759x 42x 42x 42x 42x 42x 42x 42x 36557x 21x 21x 21x 42x 42x 155221x 155221x 155221x 904x  
/**
 * @file slotProps.js
 * @description Rewrites `:prop` bindings on `<slot>` tags into `data-props-*` attributes.
 */
import { scanTagEnd } from '../htmlTree.js';
 
/**
 * Processes slot elements in the template, converting dynamic props starting
 * with `:` to `data-props-` attributes.
 * @param {string} template - The template string.
 * @returns {string} The processed template.
 */
export function processSlotProps(template) {
  if (!template) return template;
 
  // The tag is bounded by the shared lexer rather than by `/<slot\b([^>]*?)>/`,
  // which ends a tag at the first `>` wherever it appears. Two shapes failed
  // silently: `<slot name="a > b" :item="row">`, and -- the one that matters --
  // `<slot :label="a > b ? x : y">`, a conditional passed to a scoped slot.
  // In both, the `:prop` was never rewritten to `data-props-*`, so the slot
  // received nothing and the raw directive was left in the markup.
  const out = [];
  let cursor = 0;
 
  for (let i = 0; i < template.length; i++) {
    if (template[i] !== '<') continue;
    if (!/^<slot\b/i.test(template.slice(i, i + 6))) continue;
 
    const end = scanTagEnd(template, i);
    if (end === -1) continue;
 
    const attrsStr = template.slice(i + '<slot'.length, end);
    const replacedAttrs = attrsStr.replace(
      /\s+:([\w\d.-]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g,
      (m, name, dquote, squote) => ` data-props-${name}="${dquote !== undefined ? dquote : squote}"`,
    );
 
    if (replacedAttrs !== attrsStr) {
      out.push(template.slice(cursor, i), `<slot${replacedAttrs}>`);
      cursor = end + 1;
    }
    i = end;
  }
 
  out.push(template.slice(cursor));
  return out.join('');
}