Skip to content

refactor(compiler): add g3-only checks for ng-content and ng-template bindings - #71887

Open
JeanMeche wants to merge 1 commit into
angular:mainfrom
JeanMeche:compiler-virtual-element-bindings
Open

JeanMeche wants to merge 1 commit into
angular:mainfrom
JeanMeche:compiler-virtual-element-bindings

Conversation

@JeanMeche

@JeanMeche JeanMeche commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

This PR extends compiler and template type-checking validation for virtual elements to <ng-content> and <ng-template>.

Because disallowing unsupported bindings and attributes on <ng-content> and <ng-template> is a breaking change for existing templates, this PR guards the new checks behind g3-only / 3p-only markers so they take effect internally first, leaving external (3P) behavior unchanged until the v23 breaking-change window opens (tracked in #72191):

  • Template transformation (r3_template_transform.ts):
    • Guarded with g3-only / 3p-only markers and TODO(v23):
      • <ng-template> does not support attribute bindings ([attr.foo]).
      • <ng-content> does not support property bindings ([id]), event bindings ((click)), or template references (#ref), and uses parsed attributes rather than raw element.attrs on t.Content.
  • Type Check Block (TcbContentAttributesOp in ngtsc and ngp):
    • Guarded with g3-only markers and TODO(v23) in Scope (recordContentNodesInProjectionSlots and queueing TcbContentAttributesOp).
    • When enabled, <ng-content> enforces an allowlist for static attributes (select case-insensitive, ngProjectAs, ngSkipHydration, ngPreserveWhitespaces) and allows static attributes that select a specific content projection slot of a parent component when re-projecting.
    • If selection or selector is used on <ng-content> (and does not match a parent projection slot), a helpful suggestion is provided: 'selection'/'selector' is not supported on ng-content. Did you mean 'select'?.
  • Tests:
    • Adds TODO(v23) skipped (xit) tests in r3_template_transform_spec.ts and diagnostics_spec.ts to be re-enabled when the breaking change lands in 3P.
    • Fixes invalid attributes found in existing tests and examples where slot="foo", selector="...", or class="..." were mistakenly used on <ng-content>.
    • Keeps existing 3P compliance tests (ng_template_bindings and ng-content structural directive goldens) unchanged until v23.

@angular-robot angular-robot Bot added the area: compiler Issues related to `ngc`, Angular's template compiler label Nov 8, 2026
@ngbot ngbot Bot added this to the Backlog milestone Nov 8, 2026
@JeanMeche
JeanMeche force-pushed the compiler-virtual-element-bindings branch from b7a8b82 to f3dfcfe Compare November 8, 2026 20:11
@JeanMeche
JeanMeche force-pushed the compiler-virtual-element-bindings branch from f3dfcfe to e99d42b Compare November 9, 2026 14:16
@JeanMeche JeanMeche changed the title refactor(compiler): disallow unsupported bindings on ng-content and ng-template refactor(compiler): disallow unsupported bindings and attributes on ng-content and ng-template Nov 9, 2026
@JeanMeche
JeanMeche force-pushed the compiler-virtual-element-bindings branch 6 times, most recently from fdcecd6 to af14208 Compare November 10, 2026 15:12
JeanMeche

This comment was marked as outdated.

@JeanMeche
JeanMeche force-pushed the compiler-virtual-element-bindings branch 2 times, most recently from dbf45c7 to 363c2ef Compare November 10, 2026 17:35

@JeanMeche JeanMeche left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement! Splitting the validation between r3_template_transform.ts (for syntactic errors that don't depend on component metadata: [attr.foo] on <ng-template>, property/event/reference bindings and selection/selector typos on <ng-content>) and the TCB (TcbContentAttributesOp for static attributes on <ng-content> that don't match a parent component's content projection slot) fits cleanly into the existing architecture and works across both ngtsc and ngp.

Extracting validateNgContentElement and validateNoAttributeBindings also keeps visitElement much easier to read.

I left a few comments on edge cases around multi-class attributes / comma-separated slot selectors, namespaced attributes, legacy .d.ts metadata, and reporting multiple invalid bindings.

@JeanMeche
JeanMeche force-pushed the compiler-virtual-element-bindings branch 13 times, most recently from f6014b5 to 41017f3 Compare November 12, 2026 09:23
@JeanMeche
JeanMeche force-pushed the compiler-virtual-element-bindings branch from 41017f3 to e952401 Compare November 24, 2026 17:54
@JeanMeche JeanMeche changed the title refactor(compiler): disallow unsupported bindings and attributes on ng-content and ng-template refactor(compiler): add g3-only checks for ng-content and ng-template bindings Nov 24, 2026
@JeanMeche JeanMeche mentioned this pull request Nov 24, 2026
30 tasks
@JeanMeche
JeanMeche force-pushed the compiler-virtual-element-bindings branch from e952401 to c7d9237 Compare November 24, 2026 22:16
@JeanMeche
JeanMeche marked this pull request as ready for review November 24, 2026 22:16
@JeanMeche
JeanMeche requested a review from mattrbeck November 24, 2026 22:16
@JeanMeche

Copy link
Copy Markdown
Member Author

… bindings

Virtual elements such as `<ng-content>` and `<ng-template>` do not support
certain bindings and attributes:
- `<ng-template>` does not support attribute bindings (`[attr.foo]`).
- `<ng-content>` does not support property bindings (`[id]`), event bindings
  (`(click)`), or template references (`#ref`).
- `<ng-content>` only supports an allowlist of static attributes (`select`,
  `ngProjectAs`, `ngSkipHydration`, `ngPreserveWhitespaces`), as well as static
  attributes that select a content projection slot of a parent component when
  re-projecting `<ng-content>`. If `selection` or `selector` is encountered on
  `<ng-content>`, a helpful "Did you mean 'select'?" suggestion is provided.

Enforcing these checks is a breaking change for existing templates. To roll
this out safely in two steps, this change guards the new checks behind
`g3-only` / `3p-only` markers so they take effect internally first, leaving
external (3P) behavior unchanged until the v23 breaking-change window opens:

- Template transformation (`r3_template_transform.ts`): guard `<ng-content>`
  binding/reference validation, passing parsed `attributes` to `t.Content`,
  and `<ng-template>` attribute binding validation with `g3-only`/`3p-only`
  markers and `TODO(v23)` comments.
- Type Check Block (`scope.ts`, `content_projection.ts`, `oob.ts`): add
  `TcbContentAttributesOp` and `getContentAttributesInProjectionSlots` across
  both `ngtsc` and `ngp`, and guard queueing the op and recording parent
  projection slots in `Scope` behind `g3-only` markers with `TODO(v23)`.
- Tests: add `TODO(v23)` skipped (`xit`) tests in
  `r3_template_transform_spec.ts` and `diagnostics_spec.ts` to re-enable when
  the breaking change lands in 3P, and fix invalid `<ng-content>` attributes
  in existing tests and examples.
@JeanMeche
JeanMeche force-pushed the compiler-virtual-element-bindings branch from c7d9237 to 5173d2e Compare December 1, 2026 23:50
@JeanMeche
JeanMeche requested a review from atscott December 2, 2026 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: compiler Issues related to `ngc`, Angular's template compiler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant