MCPcopy Create free account
hub / github.com/angular/angular / calcPathForNode

Function calcPathForNode

packages/core/src/hydration/node_lookup_utils.ts:357–432  ·  view source on GitHub ↗
(
  tNode: TNode,
  lView: LView,
  excludedParentNodes: Set<number> | null,
)

Source from the content-addressed store, hash-verified

355 * instructions needs to be generated for a TNode.
356 */
357export function calcPathForNode(
358 tNode: TNode,
359 lView: LView,
360 excludedParentNodes: Set<number> | null,
361): string {
362 let parentTNode = tNode.parent;
363 let parentIndex: number | string;
364 let parentRNode: RNode;
365 let referenceNodeName: string;
366
367 // Skip over all parent nodes that are disconnected from the DOM, such nodes
368 // can not be used as anchors.
369 //
370 // This might happen in certain content projection-based use-cases, where
371 // a content of an element is projected and used, when a parent element
372 // itself remains detached from DOM. In this scenario we try to find a parent
373 // element that is attached to DOM and can act as an anchor instead.
374 //
375 // It can also happen that the parent node should be excluded, for example,
376 // because it belongs to an i18n block, which requires paths which aren't
377 // relative to other views in an i18n block.
378 while (
379 parentTNode !== null &&
380 (isDisconnectedNode(parentTNode, lView) || excludedParentNodes?.has(parentTNode.index))
381 ) {
382 parentTNode = parentTNode.parent;
383 }
384
385 if (parentTNode === null || !(parentTNode.type & TNodeType.AnyRNode)) {
386 // If there is no parent TNode or a parent TNode does not represent an RNode
387 // (i.e. not a DOM node), use component host element as a reference node.
388 parentIndex = referenceNodeName = REFERENCE_NODE_HOST;
389 parentRNode = lView[DECLARATION_COMPONENT_VIEW][HOST]!;
390 } else {
391 // Use parent TNode as a reference node.
392 parentIndex = parentTNode.index;
393 parentRNode = unwrapRNode(lView[parentIndex]);
394 referenceNodeName = renderStringify(parentIndex - HEADER_OFFSET);
395 }
396 let rNode = unwrapRNode(lView[tNode.index]);
397 if (tNode.type & (TNodeType.AnyContainer | TNodeType.Icu)) {
398 // For <ng-container> nodes, instead of serializing a reference
399 // to the anchor comment node, serialize a location of the first
400 // DOM element. Paired with the container size (serialized as a part
401 // of `ngh.containers`), it should give enough information for runtime
402 // to hydrate nodes in this container.
403 const firstRNode = getFirstNativeNode(lView, tNode);
404
405 // If container is not empty, use a reference to the first element,
406 // otherwise, rNode would point to an anchor comment node.
407 if (firstRNode) {
408 rNode = firstRNode;
409 }
410 }
411 let path: string | null = calcPathBetween(parentRNode as Node, rNode as Node, referenceNodeName);
412 if (path === null && parentRNode !== rNode) {
413 // Searching for a path between elements within a host node failed.
414 // Trying to find a path to an element starting from the `document.body` instead.

Callers 1

appendSerializedNodePathFunction · 0.90

Calls 7

unwrapRNodeFunction · 0.90
renderStringifyFunction · 0.90
getFirstNativeNodeFunction · 0.90
nodeNotFoundErrorFunction · 0.90
calcPathBetweenFunction · 0.85
isDisconnectedNodeFunction · 0.70
hasMethod · 0.65

Tested by

no test coverage detected