refactor(platform-browser): polishing (#13744)

This commit is contained in:
Dzmitry Shylovich 2017-01-02 13:20:32 +03:00 committed by Miško Hevery
parent 863285a4b0
commit a64c9b5d5b
2 changed files with 23 additions and 26 deletions

View File

@ -7,57 +7,54 @@
*/
import {Inject, Injectable} from '@angular/core';
import {getDOM} from './dom_adapter';
import {DOCUMENT} from './dom_tokens';
@Injectable()
export class SharedStylesHost {
/** @internal */
_styles: string[] = [];
/** @internal */
_stylesSet = new Set<string>();
protected _stylesSet = new Set<string>();
constructor() {}
addStyles(styles: string[]) {
const additions: any[] /** TODO #9100 */ = [];
addStyles(styles: string[]): void {
const additions = new Set<string>();
styles.forEach(style => {
if (!this._stylesSet.has(style)) {
this._stylesSet.add(style);
this._styles.push(style);
additions.push(style);
additions.add(style);
}
});
this.onStylesAdded(additions);
}
onStylesAdded(additions: string[]) {}
onStylesAdded(additions: Set<string>): void {}
getAllStyles(): string[] { return this._styles; }
getAllStyles(): string[] { return Array.from(this._stylesSet); }
}
@Injectable()
export class DomSharedStylesHost extends SharedStylesHost {
private _hostNodes = new Set<Node>();
constructor(@Inject(DOCUMENT) doc: any) {
constructor(@Inject(DOCUMENT) private _doc: any) {
super();
this._hostNodes.add(doc.head);
this._hostNodes.add(_doc.head);
}
/** @internal */
_addStylesToHost(styles: string[], host: Node) {
for (let i = 0; i < styles.length; i++) {
const styleEl = document.createElement('style');
styleEl.textContent = styles[i];
private _addStylesToHost(styles: Set<string>, host: Node): void {
styles.forEach((style: string) => {
const styleEl = this._doc.createElement('style');
styleEl.textContent = style;
host.appendChild(styleEl);
}
});
}
addHost(hostNode: Node) {
this._addStylesToHost(this._styles, hostNode);
addHost(hostNode: Node): void {
this._addStylesToHost(this._stylesSet, hostNode);
this._hostNodes.add(hostNode);
}
removeHost(hostNode: Node) { this._hostNodes.delete(hostNode); }
onStylesAdded(additions: string[]) {
this._hostNodes.forEach((hostNode) => { this._addStylesToHost(additions, hostNode); });
removeHost(hostNode: Node): void { this._hostNodes.delete(hostNode); }
onStylesAdded(additions: Set<string>): void {
this._hostNodes.forEach(hostNode => this._addStylesToHost(additions, hostNode));
}
}

View File

@ -13,7 +13,7 @@ import {expect} from '@angular/platform-browser/testing/matchers';
export function main() {
describe('DomSharedStylesHost', () => {
let doc: any /** TODO #9100 */;
let doc: Document;
let ssh: DomSharedStylesHost;
let someHost: Element;
beforeEach(() => {