|
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import {AST, BindingPipe, BindingType, BoundTarget, Call, createCssSelectorFromNode, CssSelector, DYNAMIC_TYPE, ImplicitReceiver, ParsedEventType, ParseSourceSpan, PropertyRead, PropertyWrite, R3Identifiers, SafeCall, SafePropertyRead, SchemaMetadata, SelectorMatcher, ThisReceiver, TmplAstBoundAttribute, TmplAstBoundEvent, TmplAstBoundText, TmplAstDeferredBlock, TmplAstDeferredBlockTriggers, TmplAstElement, TmplAstForLoopBlock, TmplAstHoverDeferredTrigger, TmplAstIcu, TmplAstIfBlock, TmplAstIfBlockBranch, TmplAstInteractionDeferredTrigger, TmplAstNode, TmplAstReference, TmplAstSwitchBlock, TmplAstSwitchBlockCase, TmplAstTemplate, TmplAstText, TmplAstTextAttribute, TmplAstVariable, TmplAstViewportDeferredTrigger, TransplantedType} from '@angular/compiler'; |
| 9 | +import {AST, BindingPipe, BindingType, BoundTarget, Call, createCssSelectorFromNode, CssSelector, DYNAMIC_TYPE, ImplicitReceiver, ParsedEventType, ParseSourceSpan, PropertyRead, PropertyWrite, R3Identifiers, SafeCall, SafePropertyRead, SchemaMetadata, SelectorMatcher, ThisReceiver, TmplAstBoundAttribute, TmplAstBoundEvent, TmplAstBoundText, TmplAstDeferredBlock, TmplAstDeferredBlockTriggers, TmplAstElement, TmplAstForLoopBlock, TmplAstForLoopBlockEmpty, TmplAstHoverDeferredTrigger, TmplAstIcu, TmplAstIfBlock, TmplAstIfBlockBranch, TmplAstInteractionDeferredTrigger, TmplAstNode, TmplAstReference, TmplAstSwitchBlock, TmplAstSwitchBlockCase, TmplAstTemplate, TmplAstText, TmplAstTextAttribute, TmplAstVariable, TmplAstViewportDeferredTrigger, TransplantedType} from '@angular/compiler'; |
10 | 10 | import ts from 'typescript'; |
11 | 11 |
|
12 | 12 | import {Reference} from '../../imports'; |
@@ -992,14 +992,19 @@ class TcbControlFlowContentProjectionOp extends TcbOp { |
992 | 992 | } |
993 | 993 |
|
994 | 994 | private findPotentialControlFlowNodes() { |
995 | | - const result: Array<TmplAstIfBlockBranch|TmplAstForLoopBlock> = []; |
| 995 | + const result: Array<TmplAstIfBlockBranch|TmplAstForLoopBlock|TmplAstForLoopBlockEmpty> = []; |
996 | 996 |
|
997 | 997 | for (const child of this.element.children) { |
998 | 998 | let eligibleNode: TmplAstForLoopBlock|TmplAstIfBlockBranch|null = null; |
999 | 999 |
|
1000 | 1000 | // Only `@for` blocks and the first branch of `@if` blocks participate in content projection. |
1001 | 1001 | if (child instanceof TmplAstForLoopBlock) { |
1002 | | - eligibleNode = child; |
| 1002 | + if (this.shouldCheck(child)) { |
| 1003 | + result.push(child); |
| 1004 | + } |
| 1005 | + if (child.empty !== null && this.shouldCheck(child.empty)) { |
| 1006 | + result.push(child.empty); |
| 1007 | + } |
1003 | 1008 | } else if (child instanceof TmplAstIfBlock) { |
1004 | 1009 | eligibleNode = child.branches[0]; // @if blocks are guaranteed to have at least one branch. |
1005 | 1010 | } |
@@ -1033,6 +1038,32 @@ class TcbControlFlowContentProjectionOp extends TcbOp { |
1033 | 1038 |
|
1034 | 1039 | return result; |
1035 | 1040 | } |
| 1041 | + |
| 1042 | + private shouldCheck(node: TmplAstNode&{children: TmplAstNode[]}): boolean { |
| 1043 | + // Skip nodes with less than two children since it's impossible |
| 1044 | + // for them to run into the issue that we're checking for. |
| 1045 | + if (node.children.length < 2) { |
| 1046 | + return false; |
| 1047 | + } |
| 1048 | + |
| 1049 | + // Count the number of root nodes while skipping empty text where relevant. |
| 1050 | + const rootNodeCount = node.children.reduce((count, node) => { |
| 1051 | + // Normally `preserveWhitspaces` would have been accounted for during parsing, however |
| 1052 | + // in `ngtsc/annotations/component/src/resources.ts#parseExtractedTemplate` we enable |
| 1053 | + // `preserveWhitespaces` to preserve the accuracy of source maps diagnostics. This means |
| 1054 | + // that we have to account for it here since the presence of text nodes affects the |
| 1055 | + // content projection behavior. |
| 1056 | + if (!(node instanceof TmplAstText) || this.tcb.hostPreserveWhitespaces || |
| 1057 | + node.value.trim().length > 0) { |
| 1058 | + count++; |
| 1059 | + } |
| 1060 | + |
| 1061 | + return count; |
| 1062 | + }, 0); |
| 1063 | + |
| 1064 | + // Content projection can only be affected if there is more than one root node. |
| 1065 | + return rootNodeCount > 1; |
| 1066 | + } |
1036 | 1067 | } |
1037 | 1068 |
|
1038 | 1069 | /** |
|
0 commit comments