Skip to content

Commit

Permalink
refactor(platform-browser): polishing (angular#13744)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzmitry Shylovich authored and juleskremer committed Aug 24, 2017
1 parent 0b60a7f commit e9dfec9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
47 changes: 22 additions & 25 deletions modules/@angular/platform-browser/src/dom/shared_styles_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down

0 comments on commit e9dfec9

Please sign in to comment.