All files / lib/core/renderer renderTemplate.js

92.09% Statements 303/329
87.05% Branches 74/85
100% Functions 6/6
92.09% Lines 303/329

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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 778x 778x 778x 24x 774x 754x   754x     754x 754x 754x 754x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 362x 362x 362x 362x 362x 362x 362x 20698x 20698x 20698x 20698x 20698x 20698x 20637x 20632x 20632x 20632x 20632x 20632x 20637x 20637x 20637x 20637x 20637x 20637x 20637x 20637x 20637x 20698x 20698x 20689x 20689x 20689x 20689x 20689x 20698x 20698x 20698x 362x 362x 362x 362x 362x 1x 1x 362x 362x 362x 362x 362x 362x 362x 362x 362x 21627x 21626x 21627x 21627x 21627x 21627x 20697x 20697x 20697x 21626x 21626x 21626x 21627x 65000x 65000x 43299x 64991x 21701x 21701x 21701x 21701x 21701x 21701x 21701x 15x 15x 21701x 15x 21696x 21664x 21664x 21701x 7x 1x 1x 7x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 21701x 65000x 21619x 21619x 21627x 362x 362x 362x 362x 362x 362x 362x 362x 362x 21629x 21612x 21612x 17x 21629x 21629x 21629x 21629x 21629x 21629x 21629x 21629x 21629x 21629x 21629x 21629x 21629x 21629x 21629x 207x 43x 207x 21x 21x 21x 21x 135x 17x 17x 17x 21x 21x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 21x     21x 21x 21x 4x 4x 17x 17x 17x 17x 17x 17x 17x 17x 18x 18x 18x 18x 18x 18x 18x 18x                   18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 7x 7x 7x 7x 11x 11x 18x 11x 11x 11x                   11x 11x 18x 11x 11x   11x 11x 11x 11x 18x     18x 11x 11x 11x 17x 17x 17x 17x 17x 11x 11x 17x 17x 21x 21629x 362x  
import { AvenxErrorCodes, formatMessage } from '../runtime/AvenxError.js';
import { HtmlEscaper, SafeHtml } from '../security/escapeHtml.js';
import { logger } from '../runtime/AvenxLogger.js';
import { LruCache } from '../utils/LruCache.js';
import { createInterpolationRegex } from '../utils/templateUtils.js';
import { findTagEnd } from '../utils/markupLexer.js';
import { isEventHandlerAttribute } from '../security/eventAttributes.js';
import { formatRenderDiagnostic } from '../diagnostics/renderDiagnostics.js';
 
const templateEscaper = new HtmlEscaper();
const DEFAULT_TEMPLATE_CACHE_CAPACITY = 500;
 
/**
 * Segment structure for parsed template AST:
 * @typedef {object} TemplateSegment
 * @property {boolean} isExpression - True if this segment is an interpolation expression.
 * @property {string} [value] - Static text content when isExpression is false.
 * @property {string} [expression] - Expression source code when isExpression is true.
 * @property {boolean} [isRaw] - True if raw interpolation {{{ ... }}} was used.
 */
 
/**
 * Handles the rendering of HTML templates by resolving interpolation expressions.
 * Uses an LruCache to cache parsed template AST segments with bounded capacity.
 */
export class TemplateRenderer {
  /**
   * Constructs the TemplateRenderer with a configurable LRU cache capacity.
   * @param {number|object} [capacityOrConfig] - Maximum LRU cache capacity or configuration object.
   */
  constructor(capacityOrConfig = DEFAULT_TEMPLATE_CACHE_CAPACITY) {
    let capacity = DEFAULT_TEMPLATE_CACHE_CAPACITY;
    let logContext = null;
    if (typeof capacityOrConfig === 'number' && capacityOrConfig > 0) {
      capacity = capacityOrConfig;
    } else if (capacityOrConfig && typeof capacityOrConfig === 'object') {
      if (typeof capacityOrConfig.templateCacheCapacity === 'number' && capacityOrConfig.templateCacheCapacity > 0) {
        capacity = capacityOrConfig.templateCacheCapacity;
      } else if (typeof capacityOrConfig.capacity === 'number' && capacityOrConfig.capacity > 0) {
        capacity = capacityOrConfig.capacity;
      }
      if (capacityOrConfig.logContext) {
        logContext = capacityOrConfig.logContext;
      }
    }
 
    /**
     * @type {object}
     */
    this.options = typeof capacityOrConfig === 'object' ? capacityOrConfig : {};
 
    /**
     * @type {object|null}
     */
    this.logContext = logContext;
 
    /**
     * Maximum capacity of the LRU cache.
     * @type {number}
     */
    this.capacity = capacity;
 
    /**
     * LRU Cache storing parsed template AST segments.
     * @type {LruCache}
     */
    this.cache = new LruCache(capacity);
  }
 
  /**
   * Parses a raw HTML template string into tokenized AST segments.
   * @param {string} template - The HTML template string.
   * @returns {TemplateSegment[]} Array of template segments.
   */
  parseTemplate(template) {
    const segments = [];
    const regex = createInterpolationRegex();
    let lastIndex = 0;
    let match;
 
    while ((match = regex.exec(template)) !== null) {
      if (match.index > lastIndex) {
        segments.push({
          isExpression: false,
          value: template.substring(lastIndex, match.index),
        });
      }
      const isRaw = match[1] !== undefined;
      const expression = isRaw ? match[1] : match[2];
      segments.push({
        isExpression: true,
        expression,
        isRaw,
      });
      lastIndex = regex.lastIndex;
    }
 
    if (lastIndex < template.length) {
      segments.push({
        isExpression: false,
        value: template.substring(lastIndex),
      });
    }
 
    return segments;
  }
 
  /**
   * Clears the template LRU cache.
   */
  clearCache() {
    this.cache.clear();
  }
 
  /**
   * Renders the template by replacing {{ expression }} and {{{ expression }}} with evaluated values.
   * @param {string} template - The HTML template string.
   * @param {function(string): any} resolveExpression - Function to evaluate expressions.
   * @param {object|null} [logContext] - Optional logging/diagnostic context.
   * @returns {string} The rendered HTML string.
   */
  render(template, resolveExpression, logContext = null) {
    if (!template) return '';
 
    const context = logContext || this.logContext || this.options?.logContext || {};
 
    let segments = this.cache.get(template);
    if (!segments) {
      segments = this.parseTemplate(template);
      this.cache.set(template, segments);
    }
 
    const occurrences = Object.create(null);
    let result = '';
    for (let i = 0; i < segments.length; i++) {
      const seg = segments[i];
      if (!seg.isExpression) {
        result += seg.value;
      } else {
        const expression = seg.expression;
        const isRaw = seg.isRaw;
        const trimmed = typeof expression === 'string' ? expression.trim() : String(expression || '');
        const occ = (occurrences[trimmed] = (occurrences[trimmed] || 0) + 1) - 1;
        try {
          const value = resolveExpression(expression);
          if (value == null) {
            continue;
          }
          if (isRaw || value instanceof SafeHtml) {
            result += String(value);
          } else {
            result += templateEscaper.escape(value);
          }
        } catch (error) {
          if (error instanceof Promise) {
            throw error; // Suspense: bubble up the promise without logging
          }
          if (error && error.code === AvenxErrorCodes.STATE_MUTATION_IN_UPDATE) {
            throw error;
          }
          const formatted = formatRenderDiagnostic(
            AvenxErrorCodes.TEMPLATE_RENDER_ERROR,
            expression,
            error,
            context,
            { occurrenceIndex: occ, type: 'interpolation' },
          );
          logger.warn(formatted, context);
          throw error;
        }
      }
    }
 
    return this.resolveDynamicAttributes(result, resolveExpression, context);
  }
 
  /**
   * Resolves dynamic attribute name binding syntax (:[attrName]="attrValue") in HTML string.
   * @param {string} html - The HTML string.
   * @param {function(string): any} resolveExpression - Function to evaluate expressions.
   * @param {object|null} [logContext] - Optional logging/diagnostic context.
   * @returns {string} The HTML string with dynamic attributes resolved.
   */
  resolveDynamicAttributes(html, resolveExpression, logContext = null) {
    if (!html || typeof html !== 'string' || !html.includes(':[')) {
      return html;
    }
 
    const context = logContext || this.logContext || this.options?.logContext || {};
 
    // Tags are located with the shared lexer's scanner rather than with
    // `/<([a-zA-Z0-9@/!-][^>]*?)>/g`, which ends a tag at the first `>` wherever
    // it appears. On `<div title="a > b" :[name]="val">` that regex matched only
    // `<div title="a >`, so the dynamic attribute fell outside the tag, was
    // never resolved, and `:[name]="val"` was left on the element as literal
    // markup -- silently, with the intended attribute simply absent.
    //
    // This runs after interpolation, so the `>` need not even be in the
    // template: any rendered value containing one, in any attribute before the
    // dynamic one, was enough.
    const rewritten = [];
    let cursor = 0;
    for (let i = 0; i < html.length; i++) {
      if (html[i] !== '<') continue;
      const end = findTagEnd(html, i);
      if (end === -1) continue;
 
      const fullTag = html.slice(i, end + 1);
      const tagInner = html.slice(i + 1, end);
      const replaced = rewriteTag(fullTag, tagInner);
      if (replaced !== fullTag) {
        rewritten.push(html.slice(cursor, i), replaced);
        cursor = end + 1;
      }
      i = end;
    }
    rewritten.push(html.slice(cursor));
    return rewritten.join('');
 
    /**
     * Resolves the dynamic attributes of one tag.
     * @param {string} fullTag - The tag as written, including its angle brackets.
     * @param {string} tagInner - The tag's contents, without the angle brackets.
     * @returns {string} The rewritten tag, or `fullTag` when nothing changed.
     */
    function rewriteTag(fullTag, tagInner) {
      if (tagInner.startsWith('/') || tagInner.startsWith('!') || tagInner.startsWith('?')) {
        return fullTag;
      }
 
      const dynAttrRegex = /:\[(.*?)\]\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/g;
      if (!dynAttrRegex.test(tagInner)) {
        return fullTag;
      }
      dynAttrRegex.lastIndex = 0;
 
      const activeDynAttrs = [];
      const parts = [];
      let lastIdx = 0;
      let match;
 
      while ((match = dynAttrRegex.exec(tagInner)) !== null) {
        parts.push(tagInner.substring(lastIdx, match.index));
        const nameExpr = match[1];
        const valExpr = match[2] !== undefined ? match[2] : (match[3] !== undefined ? match[3] : match[4]);
 
        let resolvedName = null;
        try {
          resolvedName = resolveExpression(nameExpr);
        } catch (error) {
          const formatted = formatRenderDiagnostic(
            AvenxErrorCodes.TEMPLATE_RENDER_ERROR,
            nameExpr,
            error,
            context,
            { type: 'attr_name' },
          );
          logger.warn(formatted, context);
        }
 
        if (resolvedName != null && String(resolvedName).trim() !== '') {
          const attrName = String(resolvedName).trim();
 
          // A dynamic name that resolves to an on* handler would write an
          // inline handler built from a value, which is the one on* path the
          // build cannot see: a literal `onclick="{{ x }}"` is refused as
          // AVX_C28, but `:[name]` is only known at run time.
          //
          // The compiled renderer and the DOM patcher have both refused this
          // for a while. This renderer did not -- and it is the one that
          // actually runs here, because a dynamic attribute name is itself a
          // reason the template cannot be compiled (AVX_W47). The name was
          // written straight into the markup, so by the time the patcher saw
          // the element the handler was already a parsed inline handler rather
          // than a dynamic attribute it would police.
          //
          // Measured in a browser: `:[handlerName]="handlerBody"` with
          // handlerName = 'onclick' produced onclick="window.__xss=1" and ran
          // it on the next click. A strict CSP stopped the execution but not
          // the attribute; without one, nothing did.
          if (isEventHandlerAttribute(attrName)) {
            logger.warn(formatMessage(AvenxErrorCodes.SECURITY_BLOCKED_EVENT_ATTRIBUTE, attrName));
            lastIdx = dynAttrRegex.lastIndex;
            continue;
          }
 
          let resolvedVal = null;
          if (valExpr !== undefined && valExpr !== null) {
            try {
              resolvedVal = resolveExpression(valExpr);
            } catch (error) {
              const formatted = formatRenderDiagnostic(
                AvenxErrorCodes.TEMPLATE_RENDER_ERROR,
                valExpr,
                error,
                context,
                { type: 'attr_value' },
              );
              logger.warn(formatted, context);
            }
          }
 
          if (resolvedVal !== false && resolvedVal != null) {
            activeDynAttrs.push(attrName);
            if (resolvedVal === true) {
              parts.push(`${attrName}="true"`);
            } else {
              const escapedVal = templateEscaper.escape(String(resolvedVal));
              parts.push(`${attrName}="${escapedVal}"`);
            }
          } else {
            activeDynAttrs.push(attrName);
          }
        }
 
        lastIdx = dynAttrRegex.lastIndex;
      }
 
      parts.push(tagInner.substring(lastIdx));
      let newTagInner = parts.join('');
 
      if (activeDynAttrs.length > 0) {
        newTagInner += ` data-ax-dyn-attrs="${activeDynAttrs.join(',')}"`;
      }
 
      return `<${newTagInner}>`;
    }
  }
}