diff --git a/packages/core/src/core_private_export.ts b/packages/core/src/core_private_export.ts index 2a0a0341a98..45af6a26158 100644 --- a/packages/core/src/core_private_export.ts +++ b/packages/core/src/core_private_export.ts @@ -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, diff --git a/packages/core/src/render3/interfaces/shared_styles_host.ts b/packages/core/src/render3/interfaces/shared_styles_host.ts new file mode 100644 index 00000000000..11734f9e0ec --- /dev/null +++ b/packages/core/src/render3/interfaces/shared_styles_host.ts @@ -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( + 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; +} diff --git a/packages/core/test/bundling/animations-standalone/bundle.golden_symbols.json b/packages/core/test/bundling/animations-standalone/bundle.golden_symbols.json index 3e96d72de0c..64c87af1499 100644 --- a/packages/core/test/bundling/animations-standalone/bundle.golden_symbols.json +++ b/packages/core/test/bundling/animations-standalone/bundle.golden_symbols.json @@ -226,6 +226,7 @@ "SELF_TOKEN", "SELF_TOKEN_REGEX", "SHARED_ANIMATION_PROVIDERS", + "SHARED_STYLES_HOST", "SIGNAL", "SIMPLE_CHANGES_STORE", "STABILITY_WARNING_THRESHOLD", diff --git a/packages/core/test/bundling/create_component/bundle.golden_symbols.json b/packages/core/test/bundling/create_component/bundle.golden_symbols.json index 71f716d8bd2..3b9d74f8f7c 100644 --- a/packages/core/test/bundling/create_component/bundle.golden_symbols.json +++ b/packages/core/test/bundling/create_component/bundle.golden_symbols.json @@ -173,6 +173,7 @@ "RuntimeError", "SCHEDULE_IN_ROOT_ZONE", "SCHEDULE_IN_ROOT_ZONE_DEFAULT", + "SHARED_STYLES_HOST", "SIGNAL", "SIGNAL_NODE", "SIMPLE_CHANGES_STORE", diff --git a/packages/core/test/bundling/defer/bundle.golden_symbols.json b/packages/core/test/bundling/defer/bundle.golden_symbols.json index 855e509d2c6..94a2c7f791a 100644 --- a/packages/core/test/bundling/defer/bundle.golden_symbols.json +++ b/packages/core/test/bundling/defer/bundle.golden_symbols.json @@ -216,6 +216,7 @@ "RuntimeError", "SCHEDULE_IN_ROOT_ZONE", "SCHEDULE_IN_ROOT_ZONE_DEFAULT", + "SHARED_STYLES_HOST", "SIGNAL", "SIMPLE_CHANGES_STORE", "SSR_BLOCK_STATE", diff --git a/packages/core/test/bundling/forms_reactive/bundle.golden_symbols.json b/packages/core/test/bundling/forms_reactive/bundle.golden_symbols.json index 1103cff66cd..43bae523a21 100644 --- a/packages/core/test/bundling/forms_reactive/bundle.golden_symbols.json +++ b/packages/core/test/bundling/forms_reactive/bundle.golden_symbols.json @@ -238,6 +238,7 @@ "RuntimeError", "SCHEDULE_IN_ROOT_ZONE", "SCHEDULE_IN_ROOT_ZONE_DEFAULT", + "SHARED_STYLES_HOST", "SIGNAL", "SIGNAL_NODE", "SIMPLE_CHANGES_STORE", diff --git a/packages/core/test/bundling/forms_template_driven/bundle.golden_symbols.json b/packages/core/test/bundling/forms_template_driven/bundle.golden_symbols.json index 5a640619902..ec5ddd4234f 100644 --- a/packages/core/test/bundling/forms_template_driven/bundle.golden_symbols.json +++ b/packages/core/test/bundling/forms_template_driven/bundle.golden_symbols.json @@ -230,6 +230,7 @@ "RuntimeError", "SCHEDULE_IN_ROOT_ZONE", "SCHEDULE_IN_ROOT_ZONE_DEFAULT", + "SHARED_STYLES_HOST", "SIGNAL", "SIGNAL_NODE", "SIMPLE_CHANGES_STORE", diff --git a/packages/core/test/bundling/hydration/bundle.golden_symbols.json b/packages/core/test/bundling/hydration/bundle.golden_symbols.json index f72dfa68985..325dc198695 100644 --- a/packages/core/test/bundling/hydration/bundle.golden_symbols.json +++ b/packages/core/test/bundling/hydration/bundle.golden_symbols.json @@ -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", diff --git a/packages/core/test/bundling/router/bundle.golden_symbols.json b/packages/core/test/bundling/router/bundle.golden_symbols.json index 492583542de..0b31a71df39 100644 --- a/packages/core/test/bundling/router/bundle.golden_symbols.json +++ b/packages/core/test/bundling/router/bundle.golden_symbols.json @@ -276,6 +276,7 @@ "SCHEDULE_IN_ROOT_ZONE", "SCHEDULE_IN_ROOT_ZONE_DEFAULT", "SEGMENT_RE", + "SHARED_STYLES_HOST", "SIGNAL", "SIGNAL_NODE", "SIMPLE_CHANGES_STORE", diff --git a/packages/core/test/bundling/standalone_bootstrap/bundle.golden_symbols.json b/packages/core/test/bundling/standalone_bootstrap/bundle.golden_symbols.json index 822f337967e..a623c48b385 100644 --- a/packages/core/test/bundling/standalone_bootstrap/bundle.golden_symbols.json +++ b/packages/core/test/bundling/standalone_bootstrap/bundle.golden_symbols.json @@ -165,6 +165,7 @@ "RuntimeError", "SCHEDULE_IN_ROOT_ZONE", "SCHEDULE_IN_ROOT_ZONE_DEFAULT", + "SHARED_STYLES_HOST", "SIGNAL", "SIMPLE_CHANGES_STORE", "STABILITY_WARNING_THRESHOLD", diff --git a/packages/platform-browser/src/browser.ts b/packages/platform-browser/src/browser.ts index d38a7465ec9..c362100f623 100644 --- a/packages/platform-browser/src/browser.ts +++ b/packages/platform-browser/src/browser.ts @@ -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}, diff --git a/packages/platform-browser/src/dom/dom_renderer.ts b/packages/platform-browser/src/dom/dom_renderer.ts index 87b51bc0e26..c4463bc6a7d 100644 --- a/packages/platform-browser/src/dom/dom_renderer.ts +++ b/packages/platform-browser/src/dom/dom_renderer.ts @@ -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, diff --git a/packages/platform-browser/src/dom/shared_styles_host.ts b/packages/platform-browser/src/dom/shared_styles_host.ts index d4dd57d584e..d829ebdbbeb 100644 --- a/packages/platform-browser/src/dom/shared_styles_host.ts +++ b/packages/platform-browser/src/dom/shared_styles_host.ts @@ -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