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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 891x 891x 891x 891x 891x 1207x 1207x 1207x 891x 891x 316x 316x 316x 316x 316x 316x 316x 316x 316x 316x 1207x 3025x 3025x 924x 110x 110x 3025x 110x 2101x 316x 316x 279x 279x 316x 316x 316x 2709x 2709x 316x 883x 316x 316x 316x 1207x 279x 279x 316x 316x 316x 316x 316x 316x 316x 316x 316x 870x 110x 4x 2x 2x 2x 2x 110x 2x 105x 104x 104x 101x 104x 3x 104x 1x 101x 100x 100x 100x 8x 8x 8x 100x 22x 22x 22x 1x 1x 92x 70x 7x 68x 63x 63x 63x 63x 63x 61x 63x 29x 37x 34x 34x 63x 70x 100x 100x 110x 316x 879x 29x 28x 28x 29x 316x 879x 29x 29x 316x 1207x 1207x 1207x 1207x 1207x 1207x 279x 279x 883x 37x 37x 37x 37x 37x 37x 37x 4907x 4907x 4907x 4907x 37x 37x 37x 37x 37x 37x 4907x 4870x 4870x 4870x 4907x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 1207x 891x 891x | /**
* @file componentTags.js
* @description Rewrites PascalCase component tags into `data-avenx-comp` host elements.
*/
import { parseAttributes } from '../htmlTree.js';
import { processSlotProps } from './slotProps.js';
import { escapeScopedSlots } from './scopedSlots.js';
/**
* Processes component tags recursively to handle transclusion slots.
* Maps `<CompName ...>...</CompName>` to `<div data-avenx-comp="CompName">...</div>`.
*
* Slot props and scoped slots are rewritten first, on every level of the
* recursion: `:prop` on a `<slot>` must already be `data-props-*`, and a scoped
* slot's interpolations must already be escaped, before the component tag that
* contains them is turned into markup the parent renders.
* @param {string} template - The template string.
* @returns {string} The processed template.
*/
export function processComponentTags(template) {
let currentTemplate = template;
currentTemplate = processSlotProps(currentTemplate);
currentTemplate = escapeScopedSlots(currentTemplate);
while (true) {
// Find the first occurrence of < followed by an uppercase letter
const match = currentTemplate.match(/<([A-Z][a-zA-Z0-9]*)\b/);
if (!match) {
break;
}
const compName = match[1];
const startIndex = match.index;
// Find the end of this opening/self-closing tag
let i = startIndex + 1 + compName.length;
let inQuote = null;
let isSelfClosing = false;
let tagEndIndex = -1;
while (i < currentTemplate.length) {
const char = currentTemplate[i];
if (inQuote) {
if (char === inQuote) {
inQuote = null;
}
} else if (char === '"' || char === "'") {
inQuote = char;
} else if (char === '>') {
const trimmedBefore = currentTemplate.substring(startIndex + 1 + compName.length, i).trim();
if (trimmedBefore.endsWith('/')) {
isSelfClosing = true;
}
tagEndIndex = i + 1;
break;
}
i++;
}
if (tagEndIndex === -1) {
break; // Malformed tag, stop parsing to prevent infinite loops
}
// Extract the attributes string
let attrsStr = currentTemplate.substring(startIndex + 1 + compName.length, tagEndIndex - 1).trim();
if (isSelfClosing && attrsStr.endsWith('/')) {
attrsStr = attrsStr.slice(0, -1).trim();
}
let isExpr = '';
const isDynamic = compName === 'Component';
// Parse attributes
const props = [];
const others = [];
let userStyle = null;
const attrs = parseAttributes(attrsStr);
for (const [attrName, attrVal] of Object.entries(attrs)) {
if (isDynamic && (attrName === 'is' || attrName === ':is')) {
if (attrVal.startsWith('{{') && attrVal.endsWith('}}')) {
isExpr = attrVal.slice(2, -2).trim();
} else {
isExpr = attrVal.trim();
}
} else if (attrName.startsWith('@')) {
others.push(`${attrName}="${attrVal.replace(/"/g, '"')}"`);
} else if (
attrName === 'class' ||
attrName === 'id' ||
(attrName.startsWith('data-') && !attrName.startsWith('data-props-'))
) {
others.push(`${attrName}="${attrVal.replace(/"/g, '"')}"`);
} else if (attrName === 'style') {
userStyle = attrVal;
} else {
let propName = attrName;
let propExpr;
if (attrName.startsWith(':')) {
propName = attrName.slice(1);
propExpr = attrVal.trim();
if (propExpr.startsWith('{{') && propExpr.endsWith('}}')) {
propExpr = propExpr.slice(2, -2).trim();
}
} else if (attrName.startsWith('data-props-')) {
propName = attrName.slice('data-props-'.length);
propExpr = attrVal.trim();
if (propExpr.startsWith('{{') && propExpr.endsWith('}}')) {
propExpr = propExpr.slice(2, -2).trim();
}
} else {
if (attrVal.startsWith('{{') && attrVal.endsWith('}}')) {
propExpr = attrVal.slice(2, -2).trim();
} else {
const trimmed = attrVal.trim();
if (
trimmed === 'true' ||
trimmed === 'false' ||
trimmed === 'null' ||
(trimmed !== '' && !isNaN(trimmed))
) {
propExpr = trimmed;
} else {
propExpr = `'${trimmed.replace(/'/g, "\\'")}'`;
}
}
}
props.push(`data-props-${propName}="${propExpr}"`);
}
}
if (compName === 'VirtualList') {
if (userStyle === null) {
userStyle = 'height: 100%;';
} else if (!/\bheight\s*:/i.test(userStyle)) {
userStyle = userStyle.trim().endsWith(';') ? `${userStyle} height: 100%;` : `${userStyle}; height: 100%;`;
}
}
if (userStyle !== null) {
others.push(`style="${userStyle.replace(/"/g, '"')}"`);
}
const propsAttr = props.length > 0 ? ` ${props.join(' ')}` : '';
const othersAttr = others.length > 0 ? ` ${others.join(' ')}` : '';
const replacementTag = isDynamic ? `data-avenx-comp-dynamic="${isExpr}"` : `data-avenx-comp="${compName}"`;
if (isSelfClosing) {
const replacement = `<div ${replacementTag}${propsAttr}${othersAttr}></div>`;
currentTemplate = currentTemplate.substring(0, startIndex) + replacement + currentTemplate.substring(tagEndIndex);
} else {
// Find matching closing tag </CompName>
let searchIndex = tagEndIndex;
let depth = 1;
let closingTagIndex = -1;
let closingTagLength = 0;
while (searchIndex < currentTemplate.length) {
const nextOpen = currentTemplate.substring(searchIndex).match(new RegExp(`^<${compName}\\b`));
const nextClose = currentTemplate.substring(searchIndex).match(new RegExp(`^</\\s*${compName}\\s*>`));
if (nextClose) {
depth--;
if (depth === 0) {
closingTagIndex = searchIndex;
closingTagLength = nextClose[0].length;
break;
}
searchIndex += nextClose[0].length;
} else if (nextOpen) {
// Scan to end of this open tag to see if it is self-closing
let tempIdx = searchIndex + nextOpen[0].length;
let tempInQuote = null;
let tempIsSelfClosing = false;
while (tempIdx < currentTemplate.length) {
const tc = currentTemplate[tempIdx];
if (tempInQuote) {
if (tc === tempInQuote) tempInQuote = null;
} else if (tc === '"' || tc === "'") {
tempInQuote = tc;
} else if (tc === '>') {
const trimmedBefore = currentTemplate.substring(searchIndex + nextOpen[0].length, tempIdx).trim();
if (trimmedBefore.endsWith('/')) {
tempIsSelfClosing = true;
}
tempIdx++;
break;
}
tempIdx++;
}
if (!tempIsSelfClosing) {
depth++;
}
searchIndex = tempIdx;
} else {
searchIndex++;
}
}
if (closingTagIndex === -1) {
// No matching closing tag, treat as self-closing
const replacement = `<div ${replacementTag}${propsAttr}${othersAttr}></div>`;
currentTemplate =
currentTemplate.substring(0, startIndex) + replacement + currentTemplate.substring(tagEndIndex);
} else {
const innerContent = currentTemplate.substring(tagEndIndex, closingTagIndex);
// Recursively process tags inside innerContent
const processedInner = processComponentTags(innerContent);
const replacement = `<div ${replacementTag}${propsAttr}${othersAttr}>${processedInner}</div>`;
currentTemplate =
currentTemplate.substring(0, startIndex) +
replacement +
currentTemplate.substring(closingTagIndex + closingTagLength);
}
}
}
return currentTemplate;
}
|