Skip to content

Commit e55d644

Browse files
committed
fixup! feat(compiler-cli): add ignoredBinding check
Match an <ng-content> in a component's content against the component's specific projection slots, as <ng-container> already is, instead of keeping all of its static attributes. A slot sees only the attributes and classes of a projected <ng-content>, because it has no tag name at runtime. An attribute that no specific slot selects lands the content in the default slot, so it has no effect and is reported. Components with unknown slots still keep every attribute.
1 parent 0747cac commit e55d644

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

‎packages/compiler-cli/test/ngtsc/ignored_bindings_spec.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,10 +476,15 @@ runInEachFileSystem(() => {
476476
[NG8031, NG8031],
477477
],
478478
[
479-
'should not report static attributes on ng-content that is projected again',
480-
`<proj><ng-content title="t" /></proj>`,
479+
'should not report a static class on ng-content that a projection slot selects',
480+
`<proj><ng-content class="card-body" /></proj>`,
481481
[],
482482
],
483+
[
484+
'should report a static attribute on ng-content that no projection slot selects',
485+
`<proj><ng-content title="t" /></proj>`,
486+
[NG8031],
487+
],
483488
[
484489
'should report static attributes on ng-content projected into a catch-all slot',
485490
`<catch-all><ng-content title="t" /></catch-all>`,

‎packages/compiler/src/typecheck/ops/ignored_bindings.ts‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {NO_ERRORS_SCHEMA} from '../../core';
1010
import {CssSelector} from '../../directive_matching';
1111
import {BindingType, ParsedEventType} from '../../expression_parser/ast';
1212
import {PRESERVE_WS_ATTR_NAME} from '../../ml_parser/html_whitespaces';
13-
import {isNgContainer, isNgTemplate} from '../../ml_parser/tags';
13+
import {isNgContainer, isNgTemplate, splitNsName} from '../../ml_parser/tags';
1414
import {
1515
BoundAttribute,
1616
BoundEvent,
@@ -270,21 +270,41 @@ export function getNodesInProjectionSlots(
270270
candidate.children.length === 1
271271
? candidate.children[0]
272272
: candidate;
273-
if (target instanceof Content || ngContentSelectors === null) {
274-
// An `<ng-content>` can be projected again, and a component whose slots are unknown may
275-
// select any of its content; keep their attributes conservatively.
273+
if (ngContentSelectors === null) {
274+
// A component whose slots are unknown may select any of its content.
276275
result.add(target);
277276
} else if (
278-
(target instanceof Element || target instanceof Template) &&
277+
(target instanceof Element || target instanceof Template || target instanceof Content) &&
279278
!target.attributes.some((attr) => attr.name === NG_PROJECT_AS) &&
280-
matcher.match(createCssSelectorFromNode(target), null)
279+
matcher.match(createProjectionSelector(target), null)
281280
) {
282281
result.add(target);
283282
}
284283
}
285284
return result;
286285
}
287286

287+
/**
288+
* Creates the selector that content projection matches `node` against. An `<ng-content>` that is
289+
* projected again has no tag name at runtime, so only its attributes and classes can select it.
290+
*/
291+
function createProjectionSelector(node: Element | Template | Content): CssSelector {
292+
if (!(node instanceof Content)) {
293+
return createCssSelectorFromNode(node);
294+
}
295+
const selector = new CssSelector();
296+
for (const attr of node.attributes) {
297+
const name = splitNsName(attr.name)[1];
298+
selector.addAttribute(name, attr.value);
299+
if (name.toLowerCase() === 'class') {
300+
for (const className of attr.value.trim().split(/\s+/)) {
301+
selector.addClassName(className);
302+
}
303+
}
304+
}
305+
return selector;
306+
}
307+
288308
/** Collects the nodes at the root of a component's content, descending only into blocks. */
289309
class ProjectionCandidateCollector extends RecursiveVisitor {
290310
readonly candidates: Node[] = [];

0 commit comments

Comments
 (0)