fix(platform-browser): allow to mix shadow dom with non shadow dom
Allow to style components that don’t use shadow dom inside of components that do. This reverts 53cf2ec5731af3e6fe700d8db48b15b1e5aa17e3 and adds a test for this case. Related to #7887
This commit is contained in:
		
							parent
							
								
									778ded9fcf
								
							
						
					
					
						commit
						ab26b6518d
					
				| @ -126,6 +126,7 @@ export class DomRenderer implements Renderer { | |||||||
|     let nodesParent: Element|DocumentFragment; |     let nodesParent: Element|DocumentFragment; | ||||||
|     if (this.componentProto.encapsulation === ViewEncapsulation.Native) { |     if (this.componentProto.encapsulation === ViewEncapsulation.Native) { | ||||||
|       nodesParent = (hostElement as any).createShadowRoot(); |       nodesParent = (hostElement as any).createShadowRoot(); | ||||||
|  |       this._rootRenderer.sharedStylesHost.addHost(nodesParent); | ||||||
|       for (let i = 0; i < this._styles.length; i++) { |       for (let i = 0; i < this._styles.length; i++) { | ||||||
|         const styleEl = document.createElement('style'); |         const styleEl = document.createElement('style'); | ||||||
|         styleEl.textContent = this._styles[i]; |         styleEl.textContent = this._styles[i]; | ||||||
|  | |||||||
| @ -8,7 +8,6 @@ | |||||||
| import {CommonModule} from '@angular/common'; | import {CommonModule} from '@angular/common'; | ||||||
| import {Component, NgModule, ViewEncapsulation} from '@angular/core'; | import {Component, NgModule, ViewEncapsulation} from '@angular/core'; | ||||||
| import {TestBed} from '@angular/core/testing'; | import {TestBed} from '@angular/core/testing'; | ||||||
| import {describe, it} from '@angular/core/testing/testing_internal'; |  | ||||||
| import {BrowserModule} from '@angular/platform-browser'; | import {BrowserModule} from '@angular/platform-browser'; | ||||||
| import {By} from '@angular/platform-browser/src/dom/debug/by'; | import {By} from '@angular/platform-browser/src/dom/debug/by'; | ||||||
| import {browserDetection} from '@angular/platform-browser/testing/browser_util'; | import {browserDetection} from '@angular/platform-browser/testing/browser_util'; | ||||||
| @ -21,23 +20,37 @@ export function main() { | |||||||
| 
 | 
 | ||||||
|     // other browsers don't support shadow dom
 |     // other browsers don't support shadow dom
 | ||||||
|     if (browserDetection.isChromeDesktop) { |     if (browserDetection.isChromeDesktop) { | ||||||
|       it('should add only styles with native encapsulation to the shadow DOM', () => { |       it('should allow to style components with emulated encapsulation and no encapsulation inside of components with shadow DOM', | ||||||
|         const fixture = TestBed.createComponent(SomeApp); |          () => { | ||||||
|         fixture.detectChanges(); |            TestBed.overrideComponent(CmpEncapsulationNative, { | ||||||
|  |              set: { | ||||||
|  |                template: | ||||||
|  |                    '<div class="native"></div><cmp-emulated></cmp-emulated><cmp-none></cmp-none>' | ||||||
|  |              } | ||||||
|  |            }); | ||||||
| 
 | 
 | ||||||
|         const cmp = fixture.debugElement.query(By.css('cmp-native')).nativeElement; |            const fixture = TestBed.createComponent(SomeApp); | ||||||
|         const styles = cmp.shadowRoot.querySelectorAll('style'); | 
 | ||||||
|         expect(styles.length).toBe(1); |            const cmp = fixture.debugElement.query(By.css('cmp-native')).nativeElement; | ||||||
|         expect(styles[0]).toHaveText('.cmp-native { color: red; }'); | 
 | ||||||
|       }); | 
 | ||||||
|  |            const native = cmp.shadowRoot.querySelector('.native'); | ||||||
|  |            expect(window.getComputedStyle(native).color).toEqual('rgb(255, 0, 0)'); | ||||||
|  | 
 | ||||||
|  |            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)'); | ||||||
|  |          }); | ||||||
|     } |     } | ||||||
|   }); |   }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @Component({ | @Component({ | ||||||
|   selector: 'cmp-native', |   selector: 'cmp-native', | ||||||
|   template: ``, |   template: `<div class="native"></div>`, | ||||||
|   styles: [`.cmp-native { color: red; }`], |   styles: [`.native { color: red; }`], | ||||||
|   encapsulation: ViewEncapsulation.Native |   encapsulation: ViewEncapsulation.Native | ||||||
| }) | }) | ||||||
| class CmpEncapsulationNative { | class CmpEncapsulationNative { | ||||||
| @ -45,8 +58,8 @@ class CmpEncapsulationNative { | |||||||
| 
 | 
 | ||||||
| @Component({ | @Component({ | ||||||
|   selector: 'cmp-emulated', |   selector: 'cmp-emulated', | ||||||
|   template: ``, |   template: `<div class="emulated"></div>`, | ||||||
|   styles: [`.cmp-emulated { color: blue; }`], |   styles: [`.emulated { color: blue; }`], | ||||||
|   encapsulation: ViewEncapsulation.Emulated |   encapsulation: ViewEncapsulation.Emulated | ||||||
| }) | }) | ||||||
| class CmpEncapsulationEmulated { | class CmpEncapsulationEmulated { | ||||||
| @ -54,8 +67,8 @@ class CmpEncapsulationEmulated { | |||||||
| 
 | 
 | ||||||
| @Component({ | @Component({ | ||||||
|   selector: 'cmp-none', |   selector: 'cmp-none', | ||||||
|   template: ``, |   template: `<div class="none"></div>`, | ||||||
|   styles: [`.cmp-none { color: yellow; }`], |   styles: [`.none { color: lime; }`], | ||||||
|   encapsulation: ViewEncapsulation.None |   encapsulation: ViewEncapsulation.None | ||||||
| }) | }) | ||||||
| class CmpEncapsulationNone { | class CmpEncapsulationNone { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user