refactor(platform-browser): polishing (#13744)
This commit is contained in:
parent
863285a4b0
commit
a64c9b5d5b
@ -7,57 +7,54 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import {Inject, Injectable} from '@angular/core';
|
import {Inject, Injectable} from '@angular/core';
|
||||||
|
import {getDOM} from './dom_adapter';
|
||||||
import {DOCUMENT} from './dom_tokens';
|
import {DOCUMENT} from './dom_tokens';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SharedStylesHost {
|
export class SharedStylesHost {
|
||||||
/** @internal */
|
/** @internal */
|
||||||
_styles: string[] = [];
|
protected _stylesSet = new Set<string>();
|
||||||
/** @internal */
|
|
||||||
_stylesSet = new Set<string>();
|
|
||||||
|
|
||||||
constructor() {}
|
addStyles(styles: string[]): void {
|
||||||
|
const additions = new Set<string>();
|
||||||
addStyles(styles: string[]) {
|
|
||||||
const additions: any[] /** TODO #9100 */ = [];
|
|
||||||
styles.forEach(style => {
|
styles.forEach(style => {
|
||||||
if (!this._stylesSet.has(style)) {
|
if (!this._stylesSet.has(style)) {
|
||||||
this._stylesSet.add(style);
|
this._stylesSet.add(style);
|
||||||
this._styles.push(style);
|
additions.add(style);
|
||||||
additions.push(style);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
this.onStylesAdded(additions);
|
this.onStylesAdded(additions);
|
||||||
}
|
}
|
||||||
|
|
||||||
onStylesAdded(additions: string[]) {}
|
onStylesAdded(additions: Set<string>): void {}
|
||||||
|
|
||||||
getAllStyles(): string[] { return this._styles; }
|
getAllStyles(): string[] { return Array.from(this._stylesSet); }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DomSharedStylesHost extends SharedStylesHost {
|
export class DomSharedStylesHost extends SharedStylesHost {
|
||||||
private _hostNodes = new Set<Node>();
|
private _hostNodes = new Set<Node>();
|
||||||
constructor(@Inject(DOCUMENT) doc: any) {
|
constructor(@Inject(DOCUMENT) private _doc: any) {
|
||||||
super();
|
super();
|
||||||
this._hostNodes.add(doc.head);
|
this._hostNodes.add(_doc.head);
|
||||||
}
|
}
|
||||||
/** @internal */
|
|
||||||
_addStylesToHost(styles: string[], host: Node) {
|
private _addStylesToHost(styles: Set<string>, host: Node): void {
|
||||||
for (let i = 0; i < styles.length; i++) {
|
styles.forEach((style: string) => {
|
||||||
const styleEl = document.createElement('style');
|
const styleEl = this._doc.createElement('style');
|
||||||
styleEl.textContent = styles[i];
|
styleEl.textContent = style;
|
||||||
host.appendChild(styleEl);
|
host.appendChild(styleEl);
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
addHost(hostNode: Node) {
|
|
||||||
this._addStylesToHost(this._styles, hostNode);
|
addHost(hostNode: Node): void {
|
||||||
|
this._addStylesToHost(this._stylesSet, hostNode);
|
||||||
this._hostNodes.add(hostNode);
|
this._hostNodes.add(hostNode);
|
||||||
}
|
}
|
||||||
removeHost(hostNode: Node) { this._hostNodes.delete(hostNode); }
|
|
||||||
|
|
||||||
onStylesAdded(additions: string[]) {
|
removeHost(hostNode: Node): void { this._hostNodes.delete(hostNode); }
|
||||||
this._hostNodes.forEach((hostNode) => { this._addStylesToHost(additions, hostNode); });
|
|
||||||
|
onStylesAdded(additions: Set<string>): void {
|
||||||
|
this._hostNodes.forEach(hostNode => this._addStylesToHost(additions, hostNode));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ import {expect} from '@angular/platform-browser/testing/matchers';
|
|||||||
|
|
||||||
export function main() {
|
export function main() {
|
||||||
describe('DomSharedStylesHost', () => {
|
describe('DomSharedStylesHost', () => {
|
||||||
let doc: any /** TODO #9100 */;
|
let doc: Document;
|
||||||
let ssh: DomSharedStylesHost;
|
let ssh: DomSharedStylesHost;
|
||||||
let someHost: Element;
|
let someHost: Element;
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user