Skip to content

fix(compiler): ignore host-context when invoked without parentheses#68664

Open
mattrbeck wants to merge 1 commit into
angular:mainfrom
mattrbeck:refactor/enforce-host-context-parentheses
Open

fix(compiler): ignore host-context when invoked without parentheses#68664
mattrbeck wants to merge 1 commit into
angular:mainfrom
mattrbeck:refactor/enforce-host-context-parentheses

Conversation

@mattrbeck
Copy link
Copy Markdown
Member

Modify _colonHostContextRe and _hostContextPattern to strictly process :host-context only when parentheses are present. When invoked without parentheses (which is invalid), the selector is ignored and preserved in the source text. Also remove the legacy test case from host_and_host_context_spec.ts.

Support for this behavior was introduced in 2021 in #40494 with the following comment:

It is not clear what the behavior should be for a :host-context with no selectors.
This test is checking that the result is backward compatible with previous behavior.
Arguably it should actually be an error that should be reported.

I believe giving this invalid selector special meaning was a mistake, and that Angular should not diverge from the spec in this particular case. Any code this impacts can be trivially migrated to use :host to produce the same result.

@angular-robot angular-robot Bot added the area: compiler Issues related to `ngc`, Angular's template compiler label May 11, 2026
@ngbot ngbot Bot added this to the Backlog milestone May 11, 2026
@mattrbeck mattrbeck force-pushed the refactor/enforce-host-context-parentheses branch from 8b53c6e to 7b288ee Compare May 11, 2026 02:10
@mattrbeck mattrbeck changed the title refactor(compiler): ignore host-context when invoked without parentheses fix(compiler): ignore host-context when invoked without parentheses May 11, 2026
@mattrbeck mattrbeck force-pushed the refactor/enforce-host-context-parentheses branch 2 times, most recently from da0f5bf to 1ea9942 Compare May 11, 2026 18:16
…text

Modify `_colonHostContextRe` and `_hostContextPattern` to strictly process `:host-context` only when parentheses containing at least one non-whitespace argument character are present. Update `_colonHostRe` to explicitly NOT match `:host` when followed by a hyphen. When invoked without parentheses or with empty parentheses, the selector is completely ignored and treated as a standard CSS pseudo-class in the source text. Also update the legacy test case from `host_and_host_context_spec.ts`.
@mattrbeck mattrbeck force-pushed the refactor/enforce-host-context-parentheses branch from 1ea9942 to a84ae23 Compare May 11, 2026 21:09
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.

2 participants