refactor(core): move SharedStylesHost interface into @angular/core with a dedicated InjectionToken

This allows code in `@angular/core` to inject and use `SharedStylesHost`, even though the implementation is defined in `@angular/platform-browser`.
This commit is contained in:
Doug Parker 2026-01-28 10:49:30 -08:00 committed by Kirill Cherkashin
parent 1415d86980
commit df1f5d4394
13 changed files with 67 additions and 13 deletions

View file

@ -61,6 +61,10 @@ export {
DeferBlockState as ɵDeferBlockState,
} from './defer/interfaces';
export {getDocument as ɵgetDocument} from './render3/interfaces/document';
export {
SHARED_STYLES_HOST as ɵSHARED_STYLES_HOST,
SharedStylesHost as ɵSharedStylesHost,
} from './render3/interfaces/shared_styles_host';
export {
convertToBitFlags as ɵconvertToBitFlags,
setCurrentInjector as ɵsetCurrentInjector,

View file

@ -0,0 +1,47 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {InjectionToken} from '../../di/injection_token';
/** Token used to retrieve the `SharedStylesHost`. */
export const SHARED_STYLES_HOST = new InjectionToken<SharedStylesHost>(
typeof ngDevMode !== 'undefined' && ngDevMode ? 'SHARED_STYLES_HOST' : '',
);
/** Manages stylesheets for components in the application. */
export interface SharedStylesHost {
/**
* Adds embedded styles to the DOM via HTML `style` elements.
* @param styles An array of style content strings.
* @param urls An array of URLs to be added as link tags.
*/
addStyles(styles: string[], urls?: string[]): void;
/**
* Removes embedded styles from the DOM that were added as HTML `style` elements.
* @param styles An array of style content strings.
* @param urls An array of URLs to be removed as link tags.
*/
removeStyles(styles: string[], urls?: string[]): void;
/**
* Adds a host node to contain styles added to the DOM and adds all existing style usage to
* the newly added host node.
*
* @param hostNode The node to contain styles added to the DOM.
*/
addHost(hostNode: Node): void;
/**
* Removes a host node from the set of style hosts and removes all existing style usage from
* the removed host node.
*
* @param hostNode The node to remove from the set of style hosts.
*/
removeHost(hostNode: Node): void;
}

View file

@ -226,6 +226,7 @@
"SELF_TOKEN",
"SELF_TOKEN_REGEX",
"SHARED_ANIMATION_PROVIDERS",
"SHARED_STYLES_HOST",
"SIGNAL",
"SIMPLE_CHANGES_STORE",
"STABILITY_WARNING_THRESHOLD",

View file

@ -173,6 +173,7 @@
"RuntimeError",
"SCHEDULE_IN_ROOT_ZONE",
"SCHEDULE_IN_ROOT_ZONE_DEFAULT",
"SHARED_STYLES_HOST",
"SIGNAL",
"SIGNAL_NODE",
"SIMPLE_CHANGES_STORE",

View file

@ -216,6 +216,7 @@
"RuntimeError",
"SCHEDULE_IN_ROOT_ZONE",
"SCHEDULE_IN_ROOT_ZONE_DEFAULT",
"SHARED_STYLES_HOST",
"SIGNAL",
"SIMPLE_CHANGES_STORE",
"SSR_BLOCK_STATE",

View file

@ -238,6 +238,7 @@
"RuntimeError",
"SCHEDULE_IN_ROOT_ZONE",
"SCHEDULE_IN_ROOT_ZONE_DEFAULT",
"SHARED_STYLES_HOST",
"SIGNAL",
"SIGNAL_NODE",
"SIMPLE_CHANGES_STORE",

View file

@ -230,6 +230,7 @@
"RuntimeError",
"SCHEDULE_IN_ROOT_ZONE",
"SCHEDULE_IN_ROOT_ZONE_DEFAULT",
"SHARED_STYLES_HOST",
"SIGNAL",
"SIGNAL_NODE",
"SIMPLE_CHANGES_STORE",

View file

@ -255,6 +255,7 @@
"RuntimeError",
"SCHEDULE_IN_ROOT_ZONE",
"SCHEDULE_IN_ROOT_ZONE_DEFAULT",
"SHARED_STYLES_HOST",
"SIGNAL",
"SIMPLE_CHANGES_STORE",
"SKIP_HYDRATION_ATTR_NAME",

View file

@ -276,6 +276,7 @@
"SCHEDULE_IN_ROOT_ZONE",
"SCHEDULE_IN_ROOT_ZONE_DEFAULT",
"SEGMENT_RE",
"SHARED_STYLES_HOST",
"SIGNAL",
"SIGNAL_NODE",
"SIMPLE_CHANGES_STORE",

View file

@ -165,6 +165,7 @@
"RuntimeError",
"SCHEDULE_IN_ROOT_ZONE",
"SCHEDULE_IN_ROOT_ZONE_DEFAULT",
"SHARED_STYLES_HOST",
"SIGNAL",
"SIMPLE_CHANGES_STORE",
"STABILITY_WARNING_THRESHOLD",

View file

@ -37,6 +37,7 @@ import {
ɵTESTABILITY_GETTER as TESTABILITY_GETTER,
inject,
ɵresolveComponentResources as resolveComponentResources,
ɵSHARED_STYLES_HOST as SHARED_STYLES_HOST,
} from '@angular/core';
import {BrowserDomAdapter} from './browser/browser_adapter';
@ -266,7 +267,9 @@ const BROWSER_MODULE_PROVIDERS: Provider[] = [
},
{provide: EVENT_MANAGER_PLUGINS, useClass: KeyEventsPlugin, multi: true},
DomRendererFactory2,
SharedStylesHost,
{provide: SHARED_STYLES_HOST, useClass: SharedStylesHost},
// Only remains for backwards compatibility, should be removed once g3 no longer needs it.
{provide: SharedStylesHost, useExisting: SHARED_STYLES_HOST},
EventManager,
{provide: RendererFactory2, useExisting: DomRendererFactory2},
{provide: XhrFactory, useClass: BrowserXhr},

View file

@ -26,6 +26,7 @@ import {
ɵTracingSnapshot as TracingSnapshot,
Optional,
ɵallLeavingAnimations as allLeavingAnimations,
ɵSHARED_STYLES_HOST as SHARED_STYLES_HOST,
} from '@angular/core';
import {RuntimeErrorCode} from '../errors';
@ -137,7 +138,7 @@ export class DomRendererFactory2 implements RendererFactory2, OnDestroy {
constructor(
private readonly eventManager: EventManager,
private readonly sharedStylesHost: SharedStylesHost,
@Inject(SHARED_STYLES_HOST) private readonly sharedStylesHost: SharedStylesHost,
@Inject(APP_ID) private readonly appId: string,
@Inject(REMOVE_STYLES_ON_COMPONENT_DESTROY) private removeStylesOnCompDestroy: boolean,
@Inject(DOCUMENT) private readonly doc: Document,

View file

@ -15,6 +15,7 @@ import {
OnDestroy,
Optional,
PLATFORM_ID,
ɵSharedStylesHost,
} from '@angular/core';
/** The style elements attribute name used to set value of `APP_ID` token. */
@ -102,7 +103,7 @@ export function createLinkElement(url: string, doc: Document): HTMLLinkElement {
}
@Injectable()
export class SharedStylesHost implements OnDestroy {
export class SharedStylesHost implements ɵSharedStylesHost, OnDestroy {
/**
* Provides usage information for active inline style content and associated HTML <style> elements.
* Embedded styles typically originate from the `styles` metadata of a rendered component.
@ -132,10 +133,6 @@ export class SharedStylesHost implements OnDestroy {
this.hosts.add(doc.head);
}
/**
* Adds embedded styles to the DOM via HTML `style` elements.
* @param styles An array of style content strings.
*/
addStyles(styles: string[], urls?: string[]): void {
for (const value of styles) {
this.addUsage(value, this.inline, createStyleElement);
@ -206,12 +203,6 @@ export class SharedStylesHost implements OnDestroy {
this.hosts.clear();
}
/**
* Adds a host node to the set of style hosts and adds all existing style usage to
* the newly added host node.
*
* This is currently only used for Shadow DOM encapsulation mode.
*/
addHost(hostNode: Node): void {
this.hosts.add(hostNode);