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

Function navigateToNode

packages/core/src/hydration/node_lookup_utils.ts:225–269  ·  view source on GitHub ↗

* Helper function that navigates from a starting point node (the `from` node) * using provided set of navigation instructions (within `path` argument).

(from: Node, instructions: (number | NodeNavigationStep)[])

Source from the content-addressed store, hash-verified

223 * using provided set of navigation instructions (within `path` argument).
224 */
225function navigateToNode(from: Node, instructions: (number | NodeNavigationStep)[]): RNode {
226 let node = from;
227 for (let i = 0; i < instructions.length; i += 2) {
228 const step = instructions[i];
229 const repeat = instructions[i + 1] as number;
230 for (let r = 0; r < repeat; r++) {
231 if (ngDevMode && !node) {
232 throw nodeNotFoundAtPathError(from, stringifyNavigationInstructions(instructions));
233 }
234 // `nodeNotFoundAtPathError` above would normally catch a missing node too, but it's
235 // dev-mode only. Guard against it here so production throws a coded RuntimeError instead
236 // of a raw TypeError when dereferencing `node` below.
237 if (!node) {
238 throw new RuntimeError(
239 RuntimeErrorCode.HYDRATION_MISSING_NODE_ON_PATH,
240 ngDevMode &&
241 'During hydration Angular was unable to locate a node using a recorded navigation path. This usually means the client-rendered DOM no longer matches the server-rendered HTML.',
242 );
243 }
244 switch (step) {
245 case NODE_NAVIGATION_STEP_FIRST_CHILD:
246 node = getFirstChild(node)!;
247 break;
248 case NODE_NAVIGATION_STEP_NEXT_SIBLING:
249 node = getNextSibling(node)!;
250 break;
251 }
252 }
253 }
254 if (ngDevMode && !node) {
255 throw nodeNotFoundAtPathError(from, stringifyNavigationInstructions(instructions));
256 }
257 // Same as above: `node` can legitimately end up null here (the path's last step ran off
258 // the end of the real DOM) without the loop ever dereferencing it further, so the raw
259 // TypeError guard above never gets a chance to fire for this case. Catch it here instead,
260 // since this function's return type promises a non-null `RNode`.
261 if (!node) {
262 throw new RuntimeError(
263 RuntimeErrorCode.HYDRATION_MISSING_NODE_ON_PATH,
264 ngDevMode &&
265 'During hydration Angular was unable to locate a node using a recorded navigation path. This usually means the client-rendered DOM no longer matches the server-rendered HTML.',
266 );
267 }
268 return node as RNode;
269}
270
271/**
272 * Locates an RNode given a set of navigation instructions (which also contains

Callers 1

locateRNodeByPathFunction · 0.85

Calls 4

nodeNotFoundAtPathErrorFunction · 0.90
getFirstChildFunction · 0.90
getNextSiblingFunction · 0.90

Tested by

no test coverage detected