mirror of
https://github.com/angular/angular.git
synced 2026-11-23 23:17:50 -05:00
This commit adds serialization and hydration logic for content projection. While hydration for regular elements relies on their location in the TNode tree, the content projection may move elements around, so in order to hydrate them correcty, the runtime needs some extra information. This commit adds a serialization logic that adds element locations (instructions on how to navigate to a particular element from another known location of other element) into the hydration state for the following cases: - when a TNode is a first element in projection segment (other nodes are linked from that node) - when a TNode's next sibling is different before and after projection (we serialize extra info about the template-based sibling) - when a TNode's previous sibling was a content projection (i.e. `<ng-content>` slot), because we can not rely on the previous element in this case (projection happens at a later point) PR Close #49454
47 lines
1.6 KiB
TypeScript
47 lines
1.6 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright Google LLC All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
* found in the LICENSE file at https://angular.io/license
|
|
*/
|
|
|
|
import {compressNodeLocation, decompressNodeLocation} from '../../src/hydration/compression';
|
|
import {NodeNavigationStep, REFERENCE_NODE_BODY, REFERENCE_NODE_HOST} from '../../src/hydration/interfaces';
|
|
|
|
describe('compression of node location', () => {
|
|
it('should handle basic cases', () => {
|
|
const fc = NodeNavigationStep.FirstChild;
|
|
const ns = NodeNavigationStep.NextSibling;
|
|
const cases = [
|
|
[[REFERENCE_NODE_HOST, fc, 1], 'hf'],
|
|
[[REFERENCE_NODE_BODY, fc, 1], 'bf'],
|
|
[[0, fc, 1], '0f'],
|
|
[[15, fc, 1], '15f'],
|
|
[[15, fc, 4], '15f4'],
|
|
[[5, fc, 4, ns, 1, fc, 1], '5f4nf'],
|
|
[[7, ns, 4, fc, 1, ns, 1], '7n4fn'],
|
|
];
|
|
cases.forEach((_case) => {
|
|
const [origSteps, path] = _case as [(string | number)[], string];
|
|
const refNode = origSteps.shift()!;
|
|
|
|
// Transform the short version of instructions (e.g. [fc, 4, ns, 2])
|
|
// to a long version (e.g. [fc, fc, fc, fc, ns, ns]).
|
|
const steps = [];
|
|
let i = 0;
|
|
while (i < origSteps.length) {
|
|
const step = origSteps[i++];
|
|
const repeat = origSteps[i++] as number;
|
|
for (let r = 0; r < repeat; r++) {
|
|
steps.push(step);
|
|
}
|
|
}
|
|
|
|
// Check that one type can be converted to another and vice versa.
|
|
expect(compressNodeLocation(String(refNode), steps as NodeNavigationStep[])).toEqual(path);
|
|
expect(decompressNodeLocation(path)).toEqual([refNode, ...origSteps]);
|
|
});
|
|
});
|
|
});
|