import { DOCUMENT , ɵgetDOM as getDOM } from '@angular/common' ; import { Inject , Injectable , OnDestroy } from '@angular/core' ; import { SharedStylesHost } from '../shared' ; @ Injectable ( ) export class ServerStylesHost extends SharedStylesHost implements OnDestroy { private hostNodes = new Set < Node > ( ) ; private styleNodes = new Set < Node > ( ) ; constructor ( @ Inject ( DOCUMENT ) private doc ) { super ( ) ; this . hostNodes . add ( doc . head ) ; } private _addStylesToHost ( styles , host ) { styles . forEach ( ( style ) => { const styleEl = this . doc . createElement ( 'style' ) ; styleEl . textContent = style ; this . styleNodes . add ( host . appendChild ( styleEl ) ) ; } ) ; } addHost ( hostNode ) { this . _addStylesToHost ( this . styleNodes as any , hostNode ) ; this . hostNodes . add ( hostNode ) ; } removeHost ( hostNode ) { this . hostNodes . delete ( hostNode ) ; } onStylesAdded ( additions ) { this . hostNodes . forEach ( hostNode => this . _addStylesToHost ( additions , hostNode ) ) ; } ngOnDestroy ( ) { this . styleNodes . forEach ( styleNode => getDOM ( ) . remove ( styleNode ) ) ; } }