2018-06-28 15:42:04 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2018-06-28 15:42:04 -04: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
|
|
|
|
*/
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
import {Component, destroyPlatform, EventEmitter, Injector, Input, NgModule, Output, Renderer2, ViewEncapsulation} from '@angular/core';
|
2018-06-28 15:42:04 -04:00
|
|
|
import {TestBed} from '@angular/core/testing';
|
|
|
|
import {BrowserModule} from '@angular/platform-browser';
|
2018-07-12 18:58:13 -04:00
|
|
|
import {browserDetection} from '@angular/platform-browser/testing/src/browser_util';
|
2018-06-28 15:42:04 -04:00
|
|
|
import {expect} from '@angular/platform-browser/testing/src/matchers';
|
|
|
|
|
|
|
|
|
2018-07-12 18:58:13 -04:00
|
|
|
if (browserDetection.supportsShadowDom) {
|
2018-06-28 15:42:04 -04:00
|
|
|
describe('ShadowDOM Support', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureTestingModule({imports: [TestModule]});
|
|
|
|
});
|
2018-06-28 15:42:04 -04:00
|
|
|
|
2020-10-08 10:59:29 -04:00
|
|
|
it('should attach and use a shadowRoot when ViewEncapsulation.ShadowDom is set', () => {
|
2018-06-28 15:42:04 -04:00
|
|
|
const compEl = TestBed.createComponent(ShadowComponent).nativeElement;
|
2020-04-13 19:40:21 -04:00
|
|
|
expect(compEl.shadowRoot!.textContent).toEqual('Hello World');
|
2018-06-28 15:42:04 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should use the shadow root to encapsulate styles', () => {
|
|
|
|
const compEl = TestBed.createComponent(StyledShadowComponent).nativeElement;
|
2019-03-26 09:24:15 -04:00
|
|
|
// Firefox and Chrome return different computed styles. Chrome supports CSS property
|
|
|
|
// shorthands in the computed style object while Firefox expects explicit CSS properties.
|
|
|
|
// e.g. we can't use the "border" CSS property for this test as "border" is a shorthand
|
|
|
|
// property and therefore would not work within Firefox.
|
|
|
|
expect(window.getComputedStyle(compEl).backgroundColor).toEqual('rgb(0, 0, 0)');
|
2018-06-28 15:42:04 -04:00
|
|
|
const redDiv = compEl.shadowRoot.querySelector('div.red');
|
2019-03-26 09:24:15 -04:00
|
|
|
expect(window.getComputedStyle(redDiv).backgroundColor).toEqual('rgb(255, 0, 0)');
|
2018-06-28 15:42:04 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow the usage of <slot> elements', () => {
|
|
|
|
const el = TestBed.createComponent(ShadowSlotComponent).nativeElement;
|
|
|
|
const projectedContent = document.createTextNode('Hello Slot!');
|
|
|
|
el.appendChild(projectedContent);
|
2020-04-13 19:40:21 -04:00
|
|
|
const slot = el.shadowRoot!.querySelector('slot');
|
2018-06-28 15:42:04 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
expect(slot!.assignedNodes().length).toBe(1);
|
|
|
|
expect(slot!.assignedNodes()[0].textContent).toBe('Hello Slot!');
|
2018-06-28 15:42:04 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
const headerSlot = el.shadowRoot!.querySelector('slot[name=header]') as HTMLSlotElement;
|
|
|
|
const articleSlot = el.shadowRoot!.querySelector('slot[name=article]') as HTMLSlotElement;
|
2018-06-28 15:42:04 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
expect(headerSlot!.assignedNodes().length).toBe(1);
|
|
|
|
expect(headerSlot!.assignedNodes()[0].textContent).toBe('Header Text!');
|
2018-06-28 15:42:04 -04:00
|
|
|
expect(headerContent.assignedSlot).toBe(headerSlot);
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
expect(articleSlot!.assignedNodes().length).toBe(2);
|
|
|
|
expect(articleSlot!.assignedNodes()[0].textContent).toBe('Article Text!');
|
|
|
|
expect(articleSlot!.assignedNodes()[1].textContent).toBe('Article Subtext!');
|
2018-06-28 15:42:04 -04:00
|
|
|
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,
|
2019-03-26 09:24:15 -04:00
|
|
|
styles: [`:host { background: black; } .red { background: red; }`]
|
2018-06-28 15:42:04 -04:00
|
|
|
})
|
|
|
|
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() {}
|
|
|
|
}
|