Repository navigation
feat(core): add support for ShadowDOM v1 #24718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google Inc. All Rights Reserved. | ||
| * | ||
| * 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 | ||
| */ | ||
|
|
||
| import {Component, ViewEncapsulation} from '@angular/core'; | ||
|
|
||
| // #docregion longform | ||
| @Component({ | ||
| selector: 'my-app', | ||
| template: ` | ||
| <h1>Hello World!</h1> | ||
| <span class="red">Shadow DOM Rocks!</span> | ||
| `, | ||
| styles: [` | ||
| :host { | ||
| display: block; | ||
| border: 1px solid black; | ||
| } | ||
| h1 { | ||
| color: blue; | ||
| } | ||
| .red { | ||
| background-color: red; | ||
| } | ||
|
|
||
| `], | ||
| encapsulation: ViewEncapsulation.ShadowDom | ||
| }) | ||
| class MyApp { | ||
| } | ||
| // #enddocregion |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,8 @@ import {NAMESPACE_URIS} from '../../src/dom/dom_renderer'; | |
| beforeEach(() => { | ||
| TestBed.configureTestingModule({ | ||
| declarations: [ | ||
| TestCmp, SomeApp, CmpEncapsulationEmulated, CmpEncapsulationNative, CmpEncapsulationNone | ||
| TestCmp, SomeApp, CmpEncapsulationEmulated, CmpEncapsulationNative, CmpEncapsulationNone, | ||
| CmpEncapsulationNative | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to add more tests demonstrating the full usage
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added a suite |
||
| ] | ||
| }); | ||
| renderer = TestBed.createComponent(TestCmp).componentInstance.renderer; | ||
|
|
@@ -135,6 +136,15 @@ class CmpEncapsulationEmulated { | |
| class CmpEncapsulationNone { | ||
| } | ||
|
|
||
| @Component({ | ||
| selector: 'cmp-shadow', | ||
| template: `<div class="shadow"></div><cmp-emulated></cmp-emulated><cmp-none></cmp-none>`, | ||
| styles: [`.native { color: red; }`], | ||
| encapsulation: ViewEncapsulation.ShadowDom | ||
| }) | ||
| class CmpEncapsulationShadow { | ||
| } | ||
|
|
||
| @Component({ | ||
| selector: 'some-app', | ||
| template: ` | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google Inc. All Rights Reserved. | ||
| * | ||
| * 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 | ||
| */ | ||
|
|
||
| import {Component, EventEmitter, Injector, Input, NgModule, Output, Renderer2, ViewEncapsulation, destroyPlatform} from '@angular/core'; | ||
| import {TestBed} from '@angular/core/testing'; | ||
| import {BrowserModule} from '@angular/platform-browser'; | ||
| import {expect} from '@angular/platform-browser/testing/src/matchers'; | ||
|
|
||
| function supportsShadowDOMV1() { | ||
| const testEl = document.createElement('div'); | ||
| return (typeof customElements !== 'undefined') && (typeof testEl.attachShadow !== 'undefined'); | ||
| } | ||
|
|
||
| if (supportsShadowDOMV1()) { | ||
| describe('ShadowDOM Support', () => { | ||
|
|
||
| let testContainer: HTMLDivElement; | ||
|
|
||
| beforeEach(() => { TestBed.configureTestingModule({imports: [TestModule]}); }); | ||
|
|
||
| it('should attach and use a shadowRoot when ViewEncapsulation.Native is set', () => { | ||
| const compEl = TestBed.createComponent(ShadowComponent).nativeElement; | ||
| expect(compEl.shadowRoot !.textContent).toEqual('Hello World'); | ||
| }); | ||
|
|
||
| it('should use the shadow root to encapsulate styles', () => { | ||
| const compEl = TestBed.createComponent(StyledShadowComponent).nativeElement; | ||
| expect(window.getComputedStyle(compEl).border).toEqual('1px solid rgb(0, 0, 0)'); | ||
| const redDiv = compEl.shadowRoot.querySelector('div.red'); | ||
| expect(window.getComputedStyle(redDiv).border).toEqual('1px solid rgb(255, 0, 0)'); | ||
| }); | ||
|
|
||
| it('should allow the usage of <slot> elements', () => { | ||
| const el = TestBed.createComponent(ShadowSlotComponent).nativeElement; | ||
| const projectedContent = document.createTextNode('Hello Slot!'); | ||
| el.appendChild(projectedContent); | ||
| const slot = el.shadowRoot !.querySelector('slot'); | ||
|
|
||
| expect(slot !.assignedNodes().length).toBe(1); | ||
| expect(slot !.assignedNodes()[0].textContent).toBe('Hello Slot!'); | ||
| }); | ||
|
|
||
| it('should allow the usage of named <slot> elements', () => { | ||
| const el = TestBed.createComponent(ShadowSlotsComponent).nativeElement; | ||
|
|
||
| const headerContent = document.createElement('h1'); | ||
| headerContent.setAttribute('slot', 'header'); | ||
| headerContent.textContent = 'Header Text!'; | ||
|
|
||
| const articleContent = document.createElement('span'); | ||
| articleContent.setAttribute('slot', 'article'); | ||
| articleContent.textContent = 'Article Text!'; | ||
|
|
||
| const articleSubcontent = document.createElement('span'); | ||
| articleSubcontent.setAttribute('slot', 'article'); | ||
| articleSubcontent.textContent = 'Article Subtext!'; | ||
|
|
||
| el.appendChild(headerContent); | ||
| el.appendChild(articleContent); | ||
| el.appendChild(articleSubcontent); | ||
|
|
||
| const headerSlot = el.shadowRoot !.querySelector('slot[name=header]') as HTMLSlotElement; | ||
| const articleSlot = el.shadowRoot !.querySelector('slot[name=article]') as HTMLSlotElement; | ||
|
|
||
| expect(headerSlot !.assignedNodes().length).toBe(1); | ||
| expect(headerSlot !.assignedNodes()[0].textContent).toBe('Header Text!'); | ||
| expect(headerContent.assignedSlot).toBe(headerSlot); | ||
|
|
||
| expect(articleSlot !.assignedNodes().length).toBe(2); | ||
| expect(articleSlot !.assignedNodes()[0].textContent).toBe('Article Text!'); | ||
| expect(articleSlot !.assignedNodes()[1].textContent).toBe('Article Subtext!'); | ||
| expect(articleContent.assignedSlot).toBe(articleSlot); | ||
| expect(articleSubcontent.assignedSlot).toBe(articleSlot); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| @Component( | ||
| {selector: 'shadow-comp', template: 'Hello World', encapsulation: ViewEncapsulation.ShadowDom}) | ||
| class ShadowComponent { | ||
| } | ||
|
|
||
| @Component({ | ||
| selector: 'styled-shadow-comp', | ||
| template: '<div class="red"></div>', | ||
| encapsulation: ViewEncapsulation.ShadowDom, | ||
| styles: [`:host { border: 1px solid black; } .red { border: 1px solid red; }`] | ||
| }) | ||
| class StyledShadowComponent { | ||
| } | ||
|
|
||
| @Component({ | ||
| selector: 'shadow-slot-comp', | ||
| template: '<slot></slot>', | ||
| encapsulation: ViewEncapsulation.ShadowDom | ||
| }) | ||
| class ShadowSlotComponent { | ||
| } | ||
|
|
||
| @Component({ | ||
| selector: 'shadow-slots-comp', | ||
| template: | ||
| '<header><slot name="header"></slot></header><article><slot name="article"></slot></article>', | ||
| encapsulation: ViewEncapsulation.ShadowDom | ||
| }) | ||
| class ShadowSlotsComponent { | ||
| } | ||
|
|
||
| @NgModule({ | ||
| imports: [BrowserModule], | ||
| declarations: [ShadowComponent, ShadowSlotComponent, ShadowSlotsComponent, StyledShadowComponent], | ||
| entryComponents: | ||
| [ShadowComponent, ShadowSlotComponent, ShadowSlotsComponent, StyledShadowComponent], | ||
| }) | ||
| class TestModule { | ||
| ngDoBootstrap() {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We want to make sure that all new documentation is up to new standard. Can you add example of how to use it. Here is an example of how to add an example https://github.com/angular/angular/blob/master/packages/core/src/application_ref.ts#L461
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks kinda funky in the table, but let's go with it https://pr24718-65902db.ngbuilds.io/api/core/ViewEncapsulation