/** * @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.io/license */ import { DOCUMENT, isPlatformServer } from '@angular/common'; import { APP_ID, CSP_NONCE, Inject, Injectable, Optional, PLATFORM_ID } from '@angular/core'; import * as i0 from "@angular/core"; /** The style elements attribute name used to set value of `APP_ID` token. */ const APP_ID_ATTRIBUTE_NAME = 'ng-app-id'; export class SharedStylesHost { constructor(doc, appId, nonce, platformId = {}) { this.doc = doc; this.appId = appId; this.nonce = nonce; this.platformId = platformId; // Maps all registered host nodes to a list of style nodes that have been added to the host node. this.styleRef = new Map(); this.hostNodes = new Set(); this.styleNodesInDOM = this.collectServerRenderedStyles(); this.platformIsServer = isPlatformServer(platformId); this.resetHostNodes(); } addStyles(styles) { for (const style of styles) { const usageCount = this.changeUsageCount(style, 1); if (usageCount === 1) { this.onStyleAdded(style); } } } removeStyles(styles) { for (const style of styles) { const usageCount = this.changeUsageCount(style, -1); if (usageCount <= 0) { this.onStyleRemoved(style); } } } ngOnDestroy() { const styleNodesInDOM = this.styleNodesInDOM; if (styleNodesInDOM) { styleNodesInDOM.forEach((node) => node.remove()); styleNodesInDOM.clear(); } for (const style of this.getAllStyles()) { this.onStyleRemoved(style); } this.resetHostNodes(); } addHost(hostNode) { this.hostNodes.add(hostNode); for (const style of this.getAllStyles()) { this.addStyleToHost(hostNode, style); } } removeHost(hostNode) { this.hostNodes.delete(hostNode); } getAllStyles() { return this.styleRef.keys(); } onStyleAdded(style) { for (const host of this.hostNodes) { this.addStyleToHost(host, style); } } onStyleRemoved(style) { const styleRef = this.styleRef; styleRef.get(style)?.elements?.forEach((node) => node.remove()); styleRef.delete(style); } collectServerRenderedStyles() { const styles = this.doc.head?.querySelectorAll(`style[${APP_ID_ATTRIBUTE_NAME}="${this.appId}"]`); if (styles?.length) { const styleMap = new Map(); styles.forEach((style) => { if (style.textContent != null) { styleMap.set(style.textContent, style); } }); return styleMap; } return null; } changeUsageCount(style, delta) { const map = this.styleRef; if (map.has(style)) { const styleRefValue = map.get(style); styleRefValue.usage += delta; return styleRefValue.usage; } map.set(style, { usage: delta, elements: [] }); return delta; } getStyleElement(host, style) { const styleNodesInDOM = this.styleNodesInDOM; const styleEl = styleNodesInDOM?.get(style); if (styleEl?.parentNode === host) { // `styleNodesInDOM` cannot be undefined due to the above `styleNodesInDOM?.get`. styleNodesInDOM.delete(style); styleEl.removeAttribute(APP_ID_ATTRIBUTE_NAME); if (typeof ngDevMode === 'undefined' || ngDevMode) { // This attribute is solely used for debugging purposes. styleEl.setAttribute('ng-style-reused', ''); } return styleEl; } else { const styleEl = this.doc.createElement('style'); if (this.nonce) { styleEl.setAttribute('nonce', this.nonce); } styleEl.textContent = style; if (this.platformIsServer) { styleEl.setAttribute(APP_ID_ATTRIBUTE_NAME, this.appId); } return styleEl; } } addStyleToHost(host, style) { const styleEl = this.getStyleElement(host, style); host.appendChild(styleEl); const styleRef = this.styleRef; const styleElRef = styleRef.get(style)?.elements; if (styleElRef) { styleElRef.push(styleEl); } else { styleRef.set(style, { elements: [styleEl], usage: 1 }); } } resetHostNodes() { const hostNodes = this.hostNodes; hostNodes.clear(); // Re-add the head element back since this is the default host. hostNodes.add(this.doc.head); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: SharedStylesHost, deps: [{ token: DOCUMENT }, { token: APP_ID }, { token: CSP_NONCE, optional: true }, { token: PLATFORM_ID }], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: SharedStylesHost }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: SharedStylesHost, decorators: [{ type: Injectable }], ctorParameters: function () { return [{ type: Document, decorators: [{ type: Inject, args: [DOCUMENT] }] }, { type: undefined, decorators: [{ type: Inject, args: [APP_ID] }] }, { type: undefined, decorators: [{ type: Inject, args: [CSP_NONCE] }, { type: Optional }] }, { type: undefined, decorators: [{ type: Inject, args: [PLATFORM_ID] }] }]; } }); //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2hhcmVkX3N0eWxlc19ob3N0LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vLi4vLi4vLi4vcGFja2FnZXMvcGxhdGZvcm0tYnJvd3Nlci9zcmMvZG9tL3NoYXJlZF9zdHlsZXNfaG9zdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7Ozs7O0dBTUc7QUFFSCxPQUFPLEVBQUMsUUFBUSxFQUFFLGdCQUFnQixFQUFDLE1BQU0saUJBQWlCLENBQUM7QUFDM0QsT0FBTyxFQUFDLE1BQU0sRUFBRSxTQUFTLEVBQUUsTUFBTSxFQUFFLFVBQVUsRUFBYSxRQUFRLEVBQUUsV0FBVyxFQUFDLE1BQU0sZUFBZSxDQUFDOztBQUV0Ryw2RUFBNkU7QUFDN0UsTUFBTSxxQkFBcUIsR0FBRyxXQUFXLENBQUM7QUFHMUMsTUFBTSxPQUFPLGdCQUFnQjtJQVczQixZQUN1QyxHQUFhLEVBQ2YsS0FBYSxFQUNQLEtBQW1CLEVBQzVCLGFBQXFCLEVBQUU7UUFIbEIsUUFBRyxHQUFILEdBQUcsQ0FBVTtRQUNmLFVBQUssR0FBTCxLQUFLLENBQVE7UUFDUCxVQUFLLEdBQUwsS0FBSyxDQUFjO1FBQzVCLGVBQVUsR0FBVixVQUFVLENBQWE7UUFkekQsaUdBQWlHO1FBQ2hGLGFBQVEsR0FBRyxJQUFJLEdBQUcsRUFJL0IsQ0FBQztRQUNZLGNBQVMsR0FBRyxJQUFJLEdBQUcsRUFBUSxDQUFDO1FBUzNDLElBQUksQ0FBQyxlQUFlLEdBQUcsSUFBSSxDQUFDLDJCQUEyQixFQUFFLENBQUM7UUFDMUQsSUFBSSxDQUF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