All files / client/src/hydration vnode.js

92.54% Statements 149/161
92.25% Branches 143/155
100% Functions 29/29
92.02% Lines 127/138

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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389                            11x                         26818x                       5x 5x 5x     5x 5x               5x       11x                   11382x     11382x 11394x                           10360x 10360x   10360x 2x 2x     10360x 2x   10358x 15x   10343x       15471x 6240x   9231x 25x 25x   9206x 6161x 3080x   6126x 1x 1x   1x   6125x 1x 1x     6124x 2x 2x   2x   6122x 4x       6118x 6118x 6128x                     22774x 16x 16x             11x     11x           11x     6146x       8x   4x                 8x 14x 8x                       63x 14x   49x 13x   41x 27x                                       6185x   6185x   6185x 12288x 4139x   4132x 12288x 14x     12288x 13x   4132x       18x     4132x 8x 8x 4124x 10x 4114x 4097x       6185x                                       9336x 25x       9311x 9311x 4x   9307x 9307x       9307x 9307x 6179x   9307x     9307x 9307x 12333x 12333x 9x   12324x       12324x                   3105x   3105x 4091x 2049x 2049x                                       3209x 3209x   3209x 3206x 3206x 3206x     3x 3x 1x           3x     3x 3x 1x 1x   3x                       4159x 4126x 3147x 2056x 2056x    
/**
 * Virtual node helpers shared by hydration, mismatch detection and patching.
 *
 * A component's children are not a list of DOM nodes: they may contain null,
 * booleans, nested arrays, function components and adjacent strings that the
 * server merges into one text node. Everything that walks the virtual tree
 * next to the DOM must first reduce children to what the server actually
 * emitted, or indexes drift — a `null` before a button binds that button's
 * handler to the next element.
 *
 * @module @coherent.js/client/hydration/vnode
 */
 
/** Elements the server renders without content or a closing tag. */
export const VOID_ELEMENTS = new Set([
  'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input',
  'link', 'meta', 'param', 'source', 'track', 'wbr',
]);
 
/**
 * Content marked with core's dangerouslySetInnerContent(), emitted verbatim.
 * @param {*} value
 * @returns {boolean}
 */
export function isTrustedContent(value) {
  // Same brand core's dangerouslySetInnerContent() sets: plain objects with
  // __trusted/__html keys (e.g. from JSON) are not trusted.
  return Boolean(value) &&
    typeof value === 'object' &&
    value[Symbol.for('coherent.js.trustedContent')] === true &&
    typeof value.__html === 'string';
}
 
/**
 * Call a function component the way core's renderer does: with no arguments,
 * following returned functions.
 * @returns {{ ok: boolean, value?: * }}
 */
function callFunctionComponent(fn) {
  let result = fn;
  for (let guard = 0; typeof result === 'function'; guard++) {
    Iif (guard > 100) {
      return { ok: false };
    }
    try {
      result = result();
    } catch {
      // On the server a throwing component either failed the render or was
      // replaced through render()'s onError; neither is reproducible here,
      // so treat it as an opaque region.
      return { ok: false };
    }
  }
  return { ok: true, value: result };
}
 
/** What core's isCoherentObject (core/object-utils.js) accepts as a tag name. */
const TAG_NAME = /^[a-zA-Z][a-zA-Z0-9-]*$/;
 
/**
 * Whether a value is an element virtual node (`{ tagName: props }`), as core
 * decides it: a non-empty object whose keys are all tag names. Core renders
 * an object with any other key (`{ my_tag: ... }`) as nothing.
 * @param {*} vNode
 * @returns {boolean}
 */
export function isElementVNode(vNode) {
  Iif (!vNode || typeof vNode !== 'object' || Array.isArray(vNode) || isTrustedContent(vNode)) {
    return false;
  }
  const keys = Object.keys(vNode);
  return keys.length > 0 && keys.every((key) => TAG_NAME.test(key));
}
 
/**
 * Split an element virtual node into its tag name and props, normalising the
 * shorthand forms core accepts (`{ span: 'text' }`, `{ br: null }`, function
 * content).
 *
 * Only the first key is read: an object with several keys is several sibling
 * elements, which getRenderedChildren() lists one by one.
 * @param {Object} vNode - Element virtual node
 * @returns {{ tagName: string, props: Object }}
 */
export function readElement(vNode) {
  const tagName = Object.keys(vNode)[0];
  let content = vNode[tagName];
 
  if (typeof content === 'function') {
    const called = callFunctionComponent(content);
    content = called.ok ? called.value : null;
  }
 
  if (content === null || content === undefined) {
    return { tagName, props: {} };
  }
  if (typeof content !== 'object') {
    return { tagName, props: { text: content } };
  }
  return { tagName, props: content };
}
 
function flatten(node, out) {
  if (node === null || node === undefined || typeof node === 'boolean') {
    return;
  }
  if (typeof node === 'string' || typeof node === 'number') {
    out.push({ type: 'text', text: String(node) });
    return;
  }
  if (Array.isArray(node)) {
    for (const child of node) flatten(child, out);
    return;
  }
  if (typeof node === 'function') {
    const called = callFunctionComponent(node);
    if (called.ok) flatten(called.value, out);
    else Eout.push({ type: 'opaque' });
    return;
  }
  if (isTrustedContent(node)) {
    out.push({ type: 'opaque', html: node.__html });
    return;
  }
  // A lazy() value: core renders what evaluate() returns.
  if (node.__isLazy === true && typeof node.evaluate === 'function') {
    const called = callFunctionComponent(() => node.evaluate());
    if (called.ok) flatten(called.value, out);
    else Eout.push({ type: 'opaque' });
    return;
  }
  if (!isElementVNode(node)) {
    return;
  }
  // Every key is an element: `{ span: ..., button: ... }` renders a span and
  // then a button, as core's renderer does.
  const tagNames = Object.keys(node);
  for (const tagName of tagNames) {
    out.push({ type: 'element', vNode: tagNames.length === 1 ? node : { [tagName]: node[tagName] } });
  }
}
 
/**
 * Evaluate an attribute value the way core's formatAttributes does: functions
 * are called with no arguments (a throwing one yields '').
 * @param {*} value
 * @returns {*}
 */
export function resolveAttributeValue(value) {
  if (typeof value !== 'function') return value;
  try {
    return value();
  } catch {
    return '';
  }
}
 
/** Props that are content or identity, never attributes. */
const NON_ATTRIBUTE_PROPS = new Set(['children', 'text', 'html', 'key']);
 
/** Prop names that differ from their attribute name. */
const ATTRIBUTE_NAMES = { className: 'class', htmlFor: 'for' };
 
/**
 * Enumerated attributes whose `false` is a value that must be written out,
 * as in core's ENUMERATED_BOOLEAN_ATTRIBUTES (core/html-utils.js).
 */
const ENUMERATED_BOOLEAN_ATTRIBUTES = new Set(['spellcheck', 'draggable', 'contenteditable']);
 
function isEventProp(name, value) {
  return name.startsWith('on') && typeof value === 'function';
}
 
function toKebabCase(property) {
  return property.startsWith('--')
    ? property
    : property.replace(/[A-Z]/g, (m) => `-${m.toLowerCase()}`);
}
 
/**
 * Serialise a style object like core's formatAttributes does.
 * @param {Object} style
 * @returns {string}
 */
function styleToCss(style) {
  return Object.entries(style)
    .filter(([, value]) => value !== null && value !== undefined && value !== false)
    .map(([property, value]) => `${toKebabCase(property)}: ${value}`)
    .join('; ');
}
 
/**
 * Normalise a class value like core's normalizeClassValue
 * (core/html-utils.js): strings as-is, arrays flattened with falsy entries
 * dropped, objects as the keys whose values are truthy (clsx-style).
 * @param {*} value
 * @returns {string}
 */
function normalizeClassValue(value) {
  if (Array.isArray(value)) {
    return value.map(normalizeClassValue).filter(Boolean).join(' ');
  }
  if (value && typeof value === 'object') {
    return Object.keys(value).filter((name) => value[name]).join(' ');
  }
  if (value === null || value === undefined || value === false) return '';
  return String(value);
}
 
/**
 * The attributes a props object renders, as name → string ('' for a bare
 * attribute), in the order core's formatAttributes writes them. Omitted
 * names are absent.
 *
 * Functions are called, except `on*` handlers, which are not attributes;
 * `className`/`class` arrays and objects are joined like clsx, and the two
 * props together make one class attribute; style objects become
 * `prop: value` declarations; `true` is a bare attribute and `false`, `null`
 * and `undefined` omit it, except on `aria-*` and enumerated attributes
 * (`spellcheck`, `draggable`, `contenteditable`), where booleans are the
 * values "true" and "false".
 *
 * @param {Object} props
 * @returns {Map<string, string>}
 */
export function renderedAttributes(props) {
  const attributes = new Map();
  // Both given: one class attribute, in `class`'s place, `className` last
  const mergeClass = props.class !== undefined && props.className !== undefined;
 
  for (const [name, raw] of Object.entries(props)) {
    if (NON_ATTRIBUTE_PROPS.has(name) || isEventProp(name, raw)) continue;
    if (mergeClass && name === 'className') continue;
 
    const attrName = ATTRIBUTE_NAMES[name] ?? name;
    let value = mergeClass && name === 'class'
      ? [raw, props.className].map((v) => normalizeClassValue(resolveAttributeValue(v))).filter(Boolean).join(' ')
      : resolveAttributeValue(raw);
 
    if (attrName === 'class' && value !== null && typeof value === 'object') {
      value = normalizeClassValue(value);
    }
    if (
      typeof value === 'boolean' &&
      (attrName.startsWith('aria-') || ENUMERATED_BOOLEAN_ATTRIBUTES.has(attrName.toLowerCase()))
    ) {
      value = String(value);
    }
 
    if (attrName === 'style' && value && typeof value === 'object') {
      const css = styleToCss(value);
      if (css) attributes.set('style', css);
    } else if (value === true) {
      attributes.set(attrName, '');
    } else if (value !== false && value !== null && value !== undefined) {
      attributes.set(attrName, String(value));
    }
  }
 
  return attributes;
}
 
/**
 * The children an element's server output contains, in order: element nodes,
 * text nodes (adjacent strings merged, whitespace-only dropped) and opaque
 * regions (raw HTML or components whose output cannot be reproduced).
 *
 * Null, undefined and booleans render nothing (as does `text: null`); nested
 * arrays are flattened; an object with several tag keys is one element per
 * key, in key order; `text` precedes `children`; an `html` prop replaces both.
 * Each element entry's `vNode` has a single key.
 *
 * @param {string} tagName
 * @param {Object} props
 * @returns {Array<{type: 'element', vNode: Object}|{type: 'text', text: string}|{type: 'opaque', html?: string}>}
 *   an opaque region carries its `html` when it is raw HTML rather than a
 *   component this module cannot run
 */
export function getRenderedChildren(tagName, props) {
  if (!props || VOID_ELEMENTS.has(String(tagName).toLowerCase())) {
    return [];
  }
  // Null (or a function returning it) means "no raw HTML" / "no text" to
  // core, not the string "null"
  const html = resolveAttributeValue(props.html);
  if (html !== undefined && html !== null) {
    return [{ type: 'opaque', html: isTrustedContent(html) ? html.__html : String(html) }];
  }
  const text = resolveAttributeValue(props.text);
  Iif (isTrustedContent(text)) {
    return [{ type: 'opaque', html: text.__html }];
  }
 
  const raw = [];
  if (text !== undefined && text !== null) {
    raw.push({ type: 'text', text: String(text) });
  }
  flatten(props.children, raw);
 
  // Merge adjacent text the way the HTML parser does.
  const merged = [];
  for (const item of raw) {
    const last = merged[merged.length - 1];
    if (item.type === 'text' && last?.type === 'text') {
      last.text += item.text;
    } else {
      merged.push(item.type === 'text' ? { ...item } : item);
    }
  }
 
  return merged.filter((item) => item.type !== 'text' || item.text.trim() !== '');
}
 
/**
 * DOM children that correspond to rendered children: elements and text nodes
 * that are not whitespace-only.
 * @param {Node} element
 * @returns {Node[]}
 */
export function getSignificantDOMChildren(element) {
  Iif (!element || !element.childNodes) return [];
 
  return Array.from(element.childNodes).filter((node) => {
    if (node.nodeType === 1) return true;
    Eif (node.nodeType === 3) {
      return typeof node.textContent === 'string' && node.textContent.trim().length > 0;
    }
    return false;
  });
}
 
/**
 * Pair virtual children with DOM children.
 *
 * Without opaque regions the lists pair by position. Around opaque regions
 * only the children before the first one (from the start) and after the last
 * one (from the end) can be paired.
 *
 * @template V, D
 * @param {Array<V & {type: string}>} vList
 * @param {D[]} dList
 * @returns {{ pairs: Array<[V, D, number]>, exact: boolean }} `exact` is false
 *   when opaque regions made the lengths incomparable
 */
export function alignChildren(vList, dList) {
  const firstOpaque = vList.findIndex((item) => item.type === 'opaque');
  const pairs = [];
 
  if (firstOpaque === -1) {
    const length = Math.min(vList.length, dList.length);
    for (let i = 0; i < length; i++) pairs.push([vList[i], dList[i], i]);
    return { pairs, exact: true };
  }
 
  let lastOpaque = firstOpaque;
  for (let i = vList.length - 1; i > firstOpaque; i--) {
    Iif (vList[i].type === 'opaque') {
      lastOpaque = i;
      break;
    }
  }
 
  for (let i = 0; i < firstOpaque && i < dList.length; i++) {
    pairs.push([vList[i], dList[i], i]);
  }
  const tail = vList.length - 1 - lastOpaque;
  for (let k = 1; k <= tail && dList.length - k >= firstOpaque; k++) {
    const vIndex = vList.length - k;
    pairs.push([vList[vIndex], dList[dList.length - k], vIndex]);
  }
  return { pairs, exact: false };
}
 
/**
 * Pair an element's virtual element children with its DOM element children,
 * ignoring text entirely — what handler binding needs.
 * @param {string} tagName
 * @param {Object} props
 * @param {Element} domElement
 * @returns {Array<[Object, Element]>} [child vNode, DOM element] pairs
 */
export function pairElementChildren(tagName, props, domElement) {
  const vElements = getRenderedChildren(tagName, props).filter((item) => item.type !== 'text');
  const dElements = Array.from(domElement?.childNodes ?? []).filter((node) => node.nodeType === 1);
  return alignChildren(vElements, dElements).pairs
    .filter(([item]) => item.type === 'element')
    .map(([item, node]) => [item.vNode, node]);
}