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

Function processTextNodeBeforeSerialization

packages/core/src/hydration/utils.ts:539–577  ·  view source on GitHub ↗
(context: HydrationContext, node: RNode)

Source from the content-addressed store, hash-verified

537 * serialization.
538 */
539export function processTextNodeBeforeSerialization(context: HydrationContext, node: RNode) {
540 // Handle cases where text nodes can be lost after DOM serialization:
541 // 1. When there is an *empty text node* in DOM: in this case, this
542 // node would not make it into the serialized string and as a result,
543 // this node wouldn't be created in a browser. This would result in
544 // a mismatch during the hydration, where the runtime logic would expect
545 // a text node to be present in live DOM, but no text node would exist.
546 // Example: `<span>{{ name }}</span>` when the `name` is an empty string.
547 // This would result in `<span></span>` string after serialization and
548 // in a browser only the `span` element would be created. To resolve that,
549 // an extra comment node is appended in place of an empty text node and
550 // that special comment node is replaced with an empty text node *before*
551 // hydration.
552 // 2. When there are 2 consecutive text nodes present in the DOM.
553 // Example: `<div>Hello <ng-container *ngIf="true">world</ng-container></div>`.
554 // In this scenario, the live DOM would look like this:
555 // <div>#text('Hello ') #text('world') #comment('container')</div>
556 // Serialized string would look like this: `<div>Hello world<!--container--></div>`.
557 // The live DOM in a browser after that would be:
558 // <div>#text('Hello world') #comment('container')</div>
559 // Notice how 2 text nodes are now "merged" into one. This would cause hydration
560 // logic to fail, since it'd expect 2 text nodes being present, not one.
561 // To fix this, we insert a special comment node in between those text nodes, so
562 // serialized representation is: `<div>Hello <!--ngtns-->world<!--container--></div>`.
563 // This forces browser to create 2 text nodes separated by a comment node.
564 // Before running a hydration process, this special comment node is removed, so the
565 // live DOM has exactly the same state as it was before serialization.
566
567 // Collect this node as required special annotation only when its
568 // contents is empty. Otherwise, such text node would be present on
569 // the client after server-side rendering and no special handling needed.
570 const el = node as HTMLElement;
571 const corruptedTextNodes = context.corruptedTextNodes;
572 if (el.textContent === '') {
573 corruptedTextNodes.set(el, TextNodeMarker.EmptyNode);
574 } else if (el.nextSibling?.nodeType === Node.TEXT_NODE) {
575 corruptedTextNodes.set(el, TextNodeMarker.Separator);
576 }
577}
578
579export function convertHydrateTriggersToJsAction(
580 triggers: Map<DeferBlockTrigger, HydrateTriggerDetails | null> | null,

Callers 2

serializeI18nNodeFunction · 0.90
serializeLViewFunction · 0.90

Calls 1

setMethod · 0.65

Tested by

no test coverage detected