From cd3901f774f741a15148f6ace8ae5945c394f8d2 Mon Sep 17 00:00:00 2001 From: Dzmitry Shylovich Date: Mon, 2 Jan 2017 15:42:05 +0300 Subject: [PATCH] fix(platform-browser): remove style nodes on destroy (#13744) Closes #11746 PR Close #13744 --- .../platform-browser/src/dom/shared_styles_host.ts | 9 ++++++--- .../platform-browser/test/dom/shared_styles_host_spec.ts | 9 +++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/modules/@angular/platform-browser/src/dom/shared_styles_host.ts b/modules/@angular/platform-browser/src/dom/shared_styles_host.ts index f28f0424f4..097e036fb8 100644 --- a/modules/@angular/platform-browser/src/dom/shared_styles_host.ts +++ b/modules/@angular/platform-browser/src/dom/shared_styles_host.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {Inject, Injectable} from '@angular/core'; +import {Inject, Injectable, OnDestroy} from '@angular/core'; import {getDOM} from './dom_adapter'; import {DOCUMENT} from './dom_tokens'; @@ -32,8 +32,9 @@ export class SharedStylesHost { } @Injectable() -export class DomSharedStylesHost extends SharedStylesHost { +export class DomSharedStylesHost extends SharedStylesHost implements OnDestroy { private _hostNodes = new Set(); + private _styleNodes = new Set(); constructor(@Inject(DOCUMENT) private _doc: any) { super(); this._hostNodes.add(_doc.head); @@ -43,7 +44,7 @@ export class DomSharedStylesHost extends SharedStylesHost { styles.forEach((style: string) => { const styleEl = this._doc.createElement('style'); styleEl.textContent = style; - host.appendChild(styleEl); + this._styleNodes.add(host.appendChild(styleEl)); }); } @@ -57,4 +58,6 @@ export class DomSharedStylesHost extends SharedStylesHost { onStylesAdded(additions: Set): void { this._hostNodes.forEach(hostNode => this._addStylesToHost(additions, hostNode)); } + + ngOnDestroy(): void { this._styleNodes.forEach(styleNode => getDOM().remove(styleNode)); } } diff --git a/modules/@angular/platform-browser/test/dom/shared_styles_host_spec.ts b/modules/@angular/platform-browser/test/dom/shared_styles_host_spec.ts index 91fae3e62e..9305582fbf 100644 --- a/modules/@angular/platform-browser/test/dom/shared_styles_host_spec.ts +++ b/modules/@angular/platform-browser/test/dom/shared_styles_host_spec.ts @@ -46,5 +46,14 @@ export function main() { ssh.addStyles(['a {};', 'b {};']); expect(doc.head).toHaveText('a {};b {};'); }); + + it('should remove style nodes on destroy', () => { + ssh.addStyles(['a {};']); + ssh.addHost(someHost); + expect(getDOM().getInnerHTML(someHost)).toEqual(''); + + ssh.ngOnDestroy(); + expect(getDOM().getInnerHTML(someHost)).toEqual(''); + }); }); }