| 
									
										
										
										
											2017-02-06 00:49:01 +03:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							| 
									
										
										
										
											2020-05-19 12:08:49 -07:00
										 |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2017-02-06 00:49:01 +03:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-03-23 22:52:06 +03:00
										 |  |  | import {Component, Renderer2, ViewEncapsulation} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2017-02-06 00:49:01 +03:00
										 |  |  | import {TestBed} from '@angular/core/testing'; | 
					
						
							|  |  |  | import {By} from '@angular/platform-browser/src/dom/debug/by'; | 
					
						
							| 
									
										
										
										
											2018-12-04 15:13:10 +01:00
										 |  |  | import {NAMESPACE_URIS} from '@angular/platform-browser/src/dom/dom_renderer'; | 
					
						
							| 
									
										
										
										
											2017-03-02 12:12:46 -08:00
										 |  |  | import {browserDetection} from '@angular/platform-browser/testing/src/browser_util'; | 
					
						
							|  |  |  | import {expect} from '@angular/platform-browser/testing/src/matchers'; | 
					
						
							| 
									
										
										
										
											2017-02-06 00:49:01 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 14:42:55 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-03-23 22:52:06 +03:00
										 |  |  |   describe('DefaultDomRendererV2', () => { | 
					
						
							| 
									
										
										
										
											2017-12-17 22:18:50 -08:00
										 |  |  |     if (isNode) return; | 
					
						
							| 
									
										
										
										
											2017-03-23 22:52:06 +03:00
										 |  |  |     let renderer: Renderer2; | 
					
						
							| 
									
										
										
										
											2017-02-06 00:49:01 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-23 22:52:06 +03:00
										 |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       TestBed.configureTestingModule({ | 
					
						
							|  |  |  |         declarations: [ | 
					
						
							| 
									
										
										
										
											2020-10-08 16:59:29 +02:00
										 |  |  |           TestCmp, SomeApp, CmpEncapsulationEmulated, CmpEncapsulationShadow, CmpEncapsulationNone, | 
					
						
							|  |  |  |           CmpEncapsulationShadow | 
					
						
							| 
									
										
										
										
											2017-03-23 22:52:06 +03:00
										 |  |  |         ] | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       renderer = TestBed.createComponent(TestCmp).componentInstance.renderer; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('setAttribute', () => { | 
					
						
							|  |  |  |       describe('with namespace', () => { | 
					
						
							| 
									
										
										
										
											2018-06-06 13:38:20 -07:00
										 |  |  |         it('xmlns', () => shouldSetAttributeWithNs('xmlns')); | 
					
						
							| 
									
										
										
										
											2017-03-23 22:52:06 +03:00
										 |  |  |         it('xml', () => shouldSetAttributeWithNs('xml')); | 
					
						
							|  |  |  |         it('svg', () => shouldSetAttributeWithNs('svg')); | 
					
						
							|  |  |  |         it('xhtml', () => shouldSetAttributeWithNs('xhtml')); | 
					
						
							|  |  |  |         it('xlink', () => shouldSetAttributeWithNs('xlink')); | 
					
						
							| 
									
										
										
										
											2018-06-06 13:38:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         it('unknown', () => { | 
					
						
							|  |  |  |           const div = document.createElement('div'); | 
					
						
							|  |  |  |           expect(div.hasAttribute('unknown:name')).toBe(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           renderer.setAttribute(div, 'name', 'value', 'unknown'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           expect(div.getAttribute('unknown:name')).toBe('value'); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-03-23 22:52:06 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         function shouldSetAttributeWithNs(namespace: string): void { | 
					
						
							| 
									
										
										
										
											2018-06-06 13:38:20 -07:00
										 |  |  |           const namespaceUri = NAMESPACE_URIS[namespace]; | 
					
						
							| 
									
										
										
										
											2017-03-23 22:52:06 +03:00
										 |  |  |           const div = document.createElement('div'); | 
					
						
							|  |  |  |           expect(div.hasAttributeNS(namespaceUri, 'name')).toBe(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           renderer.setAttribute(div, 'name', 'value', namespace); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           expect(div.getAttributeNS(namespaceUri, 'name')).toBe('value'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('removeAttribute', () => { | 
					
						
							|  |  |  |       describe('with namespace', () => { | 
					
						
							| 
									
										
										
										
											2018-06-06 13:38:20 -07:00
										 |  |  |         it('xmlns', () => shouldRemoveAttributeWithNs('xmlns')); | 
					
						
							| 
									
										
										
										
											2017-03-23 22:52:06 +03:00
										 |  |  |         it('xml', () => shouldRemoveAttributeWithNs('xml')); | 
					
						
							|  |  |  |         it('svg', () => shouldRemoveAttributeWithNs('svg')); | 
					
						
							|  |  |  |         it('xhtml', () => shouldRemoveAttributeWithNs('xhtml')); | 
					
						
							|  |  |  |         it('xlink', () => shouldRemoveAttributeWithNs('xlink')); | 
					
						
							| 
									
										
										
										
											2018-06-06 13:38:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         it('unknown', () => { | 
					
						
							|  |  |  |           const div = document.createElement('div'); | 
					
						
							|  |  |  |           div.setAttribute('unknown:name', 'value'); | 
					
						
							|  |  |  |           expect(div.hasAttribute('unknown:name')).toBe(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           renderer.removeAttribute(div, 'name', 'unknown'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           expect(div.hasAttribute('unknown:name')).toBe(false); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-03-23 22:52:06 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         function shouldRemoveAttributeWithNs(namespace: string): void { | 
					
						
							| 
									
										
										
										
											2018-06-06 13:38:20 -07:00
										 |  |  |           const namespaceUri = NAMESPACE_URIS[namespace]; | 
					
						
							| 
									
										
										
										
											2017-03-23 22:52:06 +03:00
										 |  |  |           const div = document.createElement('div'); | 
					
						
							| 
									
										
										
										
											2018-06-06 13:38:20 -07:00
										 |  |  |           div.setAttributeNS(namespaceUri, `${namespace}:name`, 'value'); | 
					
						
							| 
									
										
										
										
											2017-03-23 22:52:06 +03:00
										 |  |  |           expect(div.hasAttributeNS(namespaceUri, 'name')).toBe(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           renderer.removeAttribute(div, 'name', namespace); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           expect(div.hasAttributeNS(namespaceUri, 'name')).toBe(false); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-02-06 00:49:01 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-01 15:38:42 -08:00
										 |  |  |     describe('removeChild', () => { | 
					
						
							|  |  |  |       it('should not error when removing a child with a different parent than given', () => { | 
					
						
							|  |  |  |         const savedParent = document.createElement('div'); | 
					
						
							|  |  |  |         const realParent = document.createElement('div'); | 
					
						
							|  |  |  |         const child = document.createElement('div'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         realParent.appendChild(child); | 
					
						
							|  |  |  |         renderer.removeChild(savedParent, child); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-08 16:59:29 +02:00
										 |  |  |     if (browserDetection.supportsShadowDom) { | 
					
						
							| 
									
										
										
										
											2017-02-16 11:56:17 -08:00
										 |  |  |       it('should allow to style components with emulated encapsulation and no encapsulation inside of components with shadow DOM', | 
					
						
							|  |  |  |          () => { | 
					
						
							|  |  |  |            const fixture = TestBed.createComponent(SomeApp); | 
					
						
							| 
									
										
										
										
											2020-10-08 16:59:29 +02:00
										 |  |  |            const cmp = fixture.debugElement.query(By.css('cmp-shadow')).nativeElement; | 
					
						
							|  |  |  |            const shadow = cmp.shadowRoot.querySelector('.shadow'); | 
					
						
							| 
									
										
										
										
											2017-02-16 11:56:17 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-08 16:59:29 +02:00
										 |  |  |            expect(window.getComputedStyle(shadow).color).toEqual('rgb(255, 0, 0)'); | 
					
						
							| 
									
										
										
										
											2017-02-16 11:56:17 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |            const emulated = cmp.shadowRoot.querySelector('.emulated'); | 
					
						
							|  |  |  |            expect(window.getComputedStyle(emulated).color).toEqual('rgb(0, 0, 255)'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            const none = cmp.shadowRoot.querySelector('.none'); | 
					
						
							|  |  |  |            expect(window.getComputedStyle(none).color).toEqual('rgb(0, 255, 0)'); | 
					
						
							|  |  |  |          }); | 
					
						
							| 
									
										
										
										
											2017-02-06 00:49:01 +03:00
										 |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'cmp-emulated', | 
					
						
							| 
									
										
										
										
											2017-02-16 11:56:17 -08:00
										 |  |  |   template: `<div class="emulated"></div>`, | 
					
						
							|  |  |  |   styles: [`.emulated { color: blue; }`], | 
					
						
							| 
									
										
										
										
											2017-02-06 00:49:01 +03:00
										 |  |  |   encapsulation: ViewEncapsulation.Emulated | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | class CmpEncapsulationEmulated { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'cmp-none', | 
					
						
							| 
									
										
										
										
											2017-02-16 11:56:17 -08:00
										 |  |  |   template: `<div class="none"></div>`, | 
					
						
							|  |  |  |   styles: [`.none { color: lime; }`], | 
					
						
							| 
									
										
										
										
											2017-02-06 00:49:01 +03:00
										 |  |  |   encapsulation: ViewEncapsulation.None | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | class CmpEncapsulationNone { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-28 12:42:04 -07:00
										 |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'cmp-shadow', | 
					
						
							|  |  |  |   template: `<div class="shadow"></div><cmp-emulated></cmp-emulated><cmp-none></cmp-none>`, | 
					
						
							| 
									
										
										
										
											2020-10-08 16:59:29 +02:00
										 |  |  |   styles: [`.shadow { color: red; }`], | 
					
						
							| 
									
										
										
										
											2018-06-28 12:42:04 -07:00
										 |  |  |   encapsulation: ViewEncapsulation.ShadowDom | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | class CmpEncapsulationShadow { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-06 00:49:01 +03:00
										 |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'some-app', | 
					
						
							|  |  |  |   template: `
 | 
					
						
							| 
									
										
										
										
											2020-10-08 16:59:29 +02:00
										 |  |  | 	  <cmp-shadow></cmp-shadow> | 
					
						
							| 
									
										
										
										
											2017-02-06 00:49:01 +03:00
										 |  |  | 	  <cmp-emulated></cmp-emulated> | 
					
						
							|  |  |  | 	  <cmp-none></cmp-none> | 
					
						
							|  |  |  |   `,
 | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | export class SomeApp { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-23 22:52:06 +03:00
										 |  |  | @Component({selector: 'test-cmp', template: ''}) | 
					
						
							|  |  |  | class TestCmp { | 
					
						
							|  |  |  |   constructor(public renderer: Renderer2) {} | 
					
						
							| 
									
										
										
										
											2017-02-06 00:49:01 +03:00
										 |  |  | } |