Skip to content

Commit 33d0102

Browse files
crisbetodylhunn
authored andcommitted
fix(compiler): allow comments between connected blocks (#55966)
Fixes that the logic which looks for connected blocks didn't allow for comments between them. Fixes #55954. PR Close #55966
1 parent 5052d4c commit 33d0102

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

packages/compiler/src/render3/r3_template_transform.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,11 @@ class HtmlAstToIvyAst implements html.Visitor {
478478
for (let i = primaryBlockIndex + 1; i < siblings.length; i++) {
479479
const node = siblings[i];
480480

481+
// Skip over comments.
482+
if (node instanceof html.Comment) {
483+
continue;
484+
}
485+
481486
// Ignore empty text nodes between blocks.
482487
if (node instanceof html.Text && node.value.trim().length === 0) {
483488
// Add the text node to the processed nodes since we don't want

packages/compiler/test/render3/r3_template_transform_spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,25 @@ describe('R3 template transform', () => {
918918
]);
919919
});
920920

921+
it('should parse a deferred block with comments between the connected blocks', () => {
922+
expectFromHtml(
923+
'@defer {<calendar-cmp [date]="current"/>}' +
924+
'<!-- Show this while loading --> @loading {Loading...}' +
925+
'<!-- Show this on the server --> @placeholder {Placeholder content!}' +
926+
'<!-- Show this on error --> @error {Loading failed :(}',
927+
).toEqual([
928+
['DeferredBlock'],
929+
['Element', 'calendar-cmp'],
930+
['BoundAttribute', 0, 'date', 'current'],
931+
['DeferredBlockPlaceholder'],
932+
['Text', 'Placeholder content!'],
933+
['DeferredBlockLoading'],
934+
['Text', 'Loading...'],
935+
['DeferredBlockError'],
936+
['Text', 'Loading failed :('],
937+
]);
938+
});
939+
921940
it(
922941
'should parse a deferred block with connected blocks that have an arbitrary ' +
923942
'amount of whitespace between them when preserveWhitespaces is enabled',
@@ -2070,6 +2089,31 @@ describe('R3 template transform', () => {
20702089
]);
20712090
});
20722091

2092+
it('should parse an if block containing comments between the branches', () => {
2093+
expectFromHtml(`
2094+
@if (cond.expr; as foo) {
2095+
Main case was true!
2096+
}
2097+
<!-- Extra case -->
2098+
@else if (other.expr) {
2099+
Extra case was true!
2100+
}
2101+
<!-- False case -->
2102+
@else {
2103+
False case!
2104+
}
2105+
`).toEqual([
2106+
['IfBlock'],
2107+
['IfBlockBranch', 'cond.expr'],
2108+
['Variable', 'foo', 'foo'],
2109+
['Text', ' Main case was true! '],
2110+
['IfBlockBranch', 'other.expr'],
2111+
['Text', ' Extra case was true! '],
2112+
['IfBlockBranch', null],
2113+
['Text', ' False case! '],
2114+
]);
2115+
});
2116+
20732117
describe('validations', () => {
20742118
it('should report an if block without a condition', () => {
20752119
expect(() =>

0 commit comments

Comments
 (0)