-
Notifications
You must be signed in to change notification settings - Fork 24.7k
/
shared_styles_host.ts
192 lines (154 loc) Β· 5.26 KB
/
shared_styles_host.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* @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, OnDestroy, Optional, PLATFORM_ID} from '@angular/core';
/** The style elements attribute name used to set value of `APP_ID` token. */
const APP_ID_ATTRIBUTE_NAME = 'ng-app-id';
@Injectable()
export class SharedStylesHost implements OnDestroy {
// Maps all registered host nodes to a list of style nodes that have been added to the host node.
private readonly styleRef = new Map < string /** Style string */, {
elements: HTMLStyleElement[];
usage: number
}
> ();
private readonly hostNodes = new Set<Node>();
private readonly styleNodesInDOM: Map<string, HTMLStyleElement>|null;
private readonly platformIsServer: boolean;
constructor(
@Inject(DOCUMENT) private readonly doc: Document,
@Inject(APP_ID) private readonly appId: string,
@Inject(CSP_NONCE) @Optional() private nonce?: string|null,
@Inject(PLATFORM_ID) readonly platformId: object = {}) {
this.styleNodesInDOM = this.collectServerRenderedStyles();
this.platformIsServer = isPlatformServer(platformId);
this.resetHostNodes();
}
addStyles(styles: string[]): void {
for (const style of styles) {
const usageCount = this.changeUsageCount(style, 1);
if (usageCount === 1) {
this.onStyleAdded(style);
}
}
}
removeStyles(styles: string[]): void {
for (const style of styles) {
const usageCount = this.changeUsageCount(style, -1);
if (usageCount <= 0) {
this.onStyleRemoved(style);
}
}
}
ngOnDestroy(): void {
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: Node): void {
this.hostNodes.add(hostNode);
for (const style of this.getAllStyles()) {
this.addStyleToHost(hostNode, style);
}
}
removeHost(hostNode: Node): void {
this.hostNodes.delete(hostNode);
}
private getAllStyles(): IterableIterator<string> {
return this.styleRef.keys();
}
private onStyleAdded(style: string): void {
for (const host of this.hostNodes) {
this.addStyleToHost(host, style);
}
}
private onStyleRemoved(style: string): void {
const styleRef = this.styleRef;
styleRef.get(style)?.elements?.forEach((node) => node.remove());
styleRef.delete(style);
}
private collectServerRenderedStyles(): Map<string, HTMLStyleElement>|null {
const styles = this.doc.head?.querySelectorAll<HTMLStyleElement>(
`style[${APP_ID_ATTRIBUTE_NAME}="${this.appId}"]`);
if (styles?.length) {
const styleMap = new Map<string, HTMLStyleElement>();
styles.forEach((style) => {
if (style.textContent != null) {
styleMap.set(style.textContent, style);
}
});
return styleMap;
}
return null;
}
private changeUsageCount(style: string, delta: number): number {
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;
}
private getStyleElement(host: Node, style: string): HTMLStyleElement {
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) {
// Uses a keyed write to avoid issues with property minification.
styleEl['nonce'] = this.nonce;
}
styleEl.textContent = style;
if (this.platformIsServer) {
styleEl.setAttribute(APP_ID_ATTRIBUTE_NAME, this.appId);
}
return styleEl;
}
}
private addStyleToHost(host: Node, style: string): void {
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});
}
}
private resetHostNodes(): void {
const hostNodes = this.hostNodes;
hostNodes.clear();
// Re-add the head element back since this is the default host.
hostNodes.add(this.doc.head);
}
}
/**
* Interm G3 workaround for usages of private `DomSharedStylesHost`.
* TODO(alanagius): delete once all usages in G3 are removed.
*/
@Injectable()
export class DomSharedStylesHost extends SharedStylesHost {
}